day 3 puzzle 2
This commit is contained in:
parent
a8611658a2
commit
95981a219a
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 89 KiB After Width: | Height: | Size: 90 KiB |
@ -3,4 +3,4 @@
|
|||||||
2:
|
2:
|
||||||
stars: 2
|
stars: 2
|
||||||
3:
|
3:
|
||||||
stars: 1
|
stars: 2
|
1
res/examples/day3_2.txt
Normal file
1
res/examples/day3_2.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
|
@ -12,5 +12,5 @@
|
|||||||
#show-puzzle(
|
#show-puzzle(
|
||||||
3, 1,
|
3, 1,
|
||||||
solve,
|
solve,
|
||||||
example: 161
|
example: ("1": 161)
|
||||||
)
|
)
|
@ -0,0 +1,31 @@
|
|||||||
|
#import "/src/utils.typ": *
|
||||||
|
|
||||||
|
#let solve(input) = {
|
||||||
|
let dos = input.matches(regex("do\(\)"))
|
||||||
|
let donts = input.matches(regex("don't\(\)"))
|
||||||
|
let toggles = dos.map(m => (m.end, true))
|
||||||
|
toggles += donts.map(m => (m.end, false))
|
||||||
|
toggles = toggles.sorted(key: t => t.first())
|
||||||
|
|
||||||
|
let is-enabled(i) = {
|
||||||
|
let t = toggles.rev().find(t => t.first() <= i)
|
||||||
|
return t == none or t.last()
|
||||||
|
}
|
||||||
|
|
||||||
|
let matches = input.matches(regex("mul\((\d{1,3}),(\d{1,3})\)"))
|
||||||
|
let total = matches.map(m => {
|
||||||
|
if is-enabled(m.start) {
|
||||||
|
m.captures.map(int)
|
||||||
|
.product()
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}).sum()
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
#show-puzzle(
|
||||||
|
3, 2,
|
||||||
|
solve,
|
||||||
|
example: ("2": 48)
|
||||||
|
)
|
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
@ -67,7 +67,14 @@
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#let show-puzzle(day, puzzle, func, example: none, visualize: none) = {
|
#let show-puzzle(
|
||||||
|
day,
|
||||||
|
puzzle,
|
||||||
|
func,
|
||||||
|
example: none,
|
||||||
|
visualize: none,
|
||||||
|
only-example: false
|
||||||
|
) = {
|
||||||
let check-example = check-example.with(visualize: visualize)
|
let check-example = check-example.with(visualize: visualize)
|
||||||
if example != none {
|
if example != none {
|
||||||
if type(example) == dictionary {
|
if type(example) == dictionary {
|
||||||
@ -80,9 +87,11 @@
|
|||||||
linebreak()
|
linebreak()
|
||||||
}
|
}
|
||||||
|
|
||||||
let input = get-input(day)
|
if not only-example {
|
||||||
let result = (func)(input)
|
let input = get-input(day)
|
||||||
show-result(result)
|
let result = (func)(input)
|
||||||
|
show-result(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#let day-template(day, puzzle1, puzzle2, stars: 0) = {
|
#let day-template(day, puzzle1, puzzle2, stars: 0) = {
|
||||||
|
Loading…
Reference in New Issue
Block a user