day 7 puzzle 1

This commit is contained in:
Louis Heredero 2024-12-07 12:05:05 +01:00
parent 6071366ed6
commit acdb212ddd
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 53 additions and 0 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 112 KiB

After

Width:  |  Height:  |  Size: 112 KiB

View File

@ -9,4 +9,6 @@
5:
stars: 2
6:
stars: 1
7:
stars: 1

9
res/examples/day7.txt Normal file
View File

@ -0,0 +1,9 @@
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20

42
src/day7/puzzle1.typ Normal file
View File

@ -0,0 +1,42 @@
#import "/src/utils.typ": *
#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
}
}
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, 1,
solve,
example: 3749
)

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

Binary file not shown.