day 7 puzzle 2

This commit is contained in:
Louis Heredero 2024-12-07 12:12:09 +01:00
parent acdb212ddd
commit c97fd6e8af
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 61 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 112 KiB

After

Width:  |  Height:  |  Size: 113 KiB

View File

@ -11,4 +11,4 @@
6: 6:
stars: 1 stars: 1
7: 7:
stars: 1 stars: 2

View File

@ -0,0 +1,60 @@
#import "/src/utils.typ": *
#let concat(a, b) = {
return int(str(a) + str(b))
}
#let solveable(values, target) = {
if values.len() == 1 {
return values.last() == target
}
let values = values
let v = values.pop()
if calc.rem(target, v) == 0 {
if solveable(values, target / v) {
return true
}
}
let str-target = str(target)
let str-v = str(v)
if str-target == str-v {
return false
}
if str-target.ends-with(str-v) {
let target2 = str-target.slice(
0,
str-target.len() - str-v.len()
)
if solveable(values, int(target2)) {
return true
}
}
if v > target {
return false
}
return solveable(values, target - v)
}
#let solve(input) = {
let equations = input.split("\n")
let total = 0
for equation in equations {
let (target, values) = equation.split(": ")
target = int(target)
values = values.split(" ").map(int)
if solveable(values, target) {
total += target
}
}
return total
}
#show-puzzle(
7, 2,
solve,
example: 11387
)

Binary file not shown.