day 2 puzzle 1

This commit is contained in:
Louis Heredero 2024-12-02 08:01:48 +01:00
parent b1a59d3cf5
commit 7a8faae694
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 44 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 86 KiB

After

Width:  |  Height:  |  Size: 89 KiB

View File

@ -1,2 +1,4 @@
1:
stars: 2
stars: 2
2:
stars: 1

6
res/examples/day2.txt Normal file
View File

@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

35
src/day2/puzzle1.typ Normal file
View File

@ -0,0 +1,35 @@
#import "/src/utils.typ": *
#let solve(input) = {
let safe-cnt = 0
for line in input.split("\n") {
let nums = line.split(" ").map(n => int(n))
let increasing
let safe = true
for i in range(nums.len() - 1) {
let d = nums.at(i + 1) - nums.at(i)
let abs-d = calc.abs(d)
if abs-d < 1 or abs-d > 3 {
safe = false
break
}
if i == 0 {
increasing = d > 0
} else if (d > 0) != increasing {
safe = false
break
}
}
if safe {
safe-cnt += 1
}
}
return safe-cnt
}
#show-puzzle(
2, 1,
solve,
example: 2
)

0
src/day2/puzzle2.typ Normal file
View File

Binary file not shown.