diff --git a/progress.png b/progress.png index 9660044..78f6fd4 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index 89cd022..1be811c 100644 --- a/progress.yaml +++ b/progress.yaml @@ -23,4 +23,6 @@ 12: stars: 2 13: - stars: 2 \ No newline at end of file + stars: 2 +14: + stars: 1 \ No newline at end of file diff --git a/res/examples/day14.txt b/res/examples/day14.txt new file mode 100644 index 0000000..72a324a --- /dev/null +++ b/res/examples/day14.txt @@ -0,0 +1,12 @@ +p=0,4 v=3,-3 +p=6,3 v=-1,-3 +p=10,3 v=-1,2 +p=2,0 v=2,-1 +p=0,0 v=1,3 +p=3,0 v=-2,-2 +p=7,6 v=-1,-3 +p=3,0 v=-1,-2 +p=9,3 v=2,3 +p=7,3 v=-1,2 +p=2,4 v=2,-3 +p=9,5 v=-3,-3 \ No newline at end of file diff --git a/src/day14/puzzle1.typ b/src/day14/puzzle1.typ new file mode 100644 index 0000000..7dc18bf --- /dev/null +++ b/src/day14/puzzle1.typ @@ -0,0 +1,61 @@ +#import "/src/utils.typ": * + +#let regexp = regex("^p=(.*?),(.*?) v=(.*?),(.*?)$") + +#let simulate(v0, dv, max: 1, steps: 1) = { + return calc.rem-euclid(v0 + dv * steps, max) +} + +#let solve(w: 0, h: 0, steps: 100, input) = { + assert(w != 0, message: "Width must be != 0") + assert(h != 0, message: "Height must be != 0") + + let bots = input.split("\n").map(b => { + let m = b.match(regexp) + return ( + pos: ( + x: int(m.captures.at(0)), + y: int(m.captures.at(1)), + ), + vel: ( + x: int(m.captures.at(2)), + y: int(m.captures.at(3)) + ) + ) + }) + + let quadrants = ( + tl: 0, + tr: 0, + bl: 0, + br: 0 + ) + let half-w = calc.div-euclid(w, 2) + let half-h = calc.div-euclid(h, 2) + + let sim-x = simulate.with(max: w, steps: steps) + let sim-y = simulate.with(max: h, steps: steps) + for bot in bots { + let x2 = sim-x(bot.pos.x, bot.vel.x) + let y2 = sim-y(bot.pos.y, bot.vel.y) + + if x2 == half-w or y2 == half-h { + continue + } + let quadrant = ( + (if y2 < half-h {"t"} else {"b"}) + + (if x2 < half-w {"l"} else {"r"}) + ) + quadrants.at(quadrant) += 1 + } + + return quadrants.values().product() +} + +#show-puzzle( + 14, 1, + solve.with(w: 101, h: 103), + example: ( + (result: 12, args: (w: 11, h: 7)), + ) +) \ No newline at end of file diff --git a/src/day14/puzzle2.typ b/src/day14/puzzle2.typ new file mode 100644 index 0000000..e69de29 diff --git a/src/main.pdf b/src/main.pdf index 0b91fb5..6f65c38 100644 Binary files a/src/main.pdf and b/src/main.pdf differ diff --git a/src/utils.typ b/src/utils.typ index 6546890..2dccec4 100644 --- a/src/utils.typ +++ b/src/utils.typ @@ -81,6 +81,12 @@ for (suffix, result) in example.pairs() { check-example(day, func, result, suffix: suffix) } + } else if type(example) == array { + for ex in example { + let suffix = ex.at("suffix", default: none) + let args = ex.at("args", default: (:)) + check-example(day, func.with(..args), ex.result, suffix: suffix) + } } else { check-example(day, func, example) }