day 2 puzzle 2

This commit is contained in:
Louis Heredero 2024-12-02 10:58:40 +01:00
parent 7a8faae694
commit 8b4120c53f
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 99 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 89 KiB

View File

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

View File

@ -0,0 +1,98 @@
#import "/src/utils.typ": *
#let is-safe(levels) = {
let increasing
let safe = true
for i in range(levels.len() - 1) {
let d = levels.at(i + 1) - levels.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
}
}
return safe
}
#let solve-bruteforce(input) = {
let safe-cnt = 0
for line in input.split("\n") {
let nums = line.split(" ").map(n => int(n))
if is-safe(nums) {
safe-cnt += 1
} else {
for i in range(nums.len()) {
if is-safe(nums.slice(0, i) + nums.slice(i+1)) {
safe-cnt += 1
break
}
}
}
}
return safe-cnt
}
#let visualize(input) = {
let safe-cnt = 0
for line in input.split("\n") {
let nums = line.split(" ").map(n => int(n))
let remove-i = none
if not is-safe(nums) {
for i in range(nums.len()) {
if is-safe(nums.slice(0, i) + nums.slice(i+1)) {
remove-i = i
break
}
}
}
let cells = ()
cells += nums.enumerate().map(((i, n)) => grid.cell(
colspan: 3,
fill: if i == remove-i {gray.transparentize(40%)},
str(n)
))
cells += (none,none,)
cells += range(nums.len() - 1).map(i => nums.at(i + 1) - nums.at(i))
.map(n => {
let col = if calc.abs(n) in (1,2,3) {green} else {red}
grid.cell(
colspan: 2,
fill: gradient.linear(
angle: if n >= 0 {0deg} else {180deg},
(white, 0%),
(white, 50%),
(col, 50%),
(col, 100%)
),
str(n)
)
})
.intersperse(none)
grid(
columns: (1fr,) * 3 * nums.len(),
inset: (x: 0.2em, y: 0.4em),
stroke: (x, y) => {
if y == 0 or (calc.rem(x, 3) == 2 and x != nums.len() * 3 - 1) {
black + .5pt
}
},
..cells
)
}
}
#show-puzzle(
2, 2,
solve-bruteforce,
example: 4,
visualize: visualize
)

Binary file not shown.