diff --git a/src/day5/puzzle1.typ b/src/day5/puzzle1.typ index 5a5f9d9..665454c 100644 --- a/src/day5/puzzle1.typ +++ b/src/day5/puzzle1.typ @@ -1,4 +1,5 @@ #import "/src/utils.typ": * +#import "@preview/cetz:0.3.1": canvas, draw #let make-rules-dict(rules) = { let dict = (:) @@ -45,8 +46,82 @@ return total } +#let visualize(input) = { + let (rules, updates) = input.split("\n\n") + rules = rules.split("\n").map(l => l.split("|").map(int)) + updates = updates.split("\n").map(l => l.split(",").map(int)) + + let total = 0 + //let rules-dict = make-rules-dict(rules) + + let diags = () + for update in updates { + let diag = canvas(length: 3em, { + for (x, n) in update.enumerate() { + draw.circle( + (x, 0), + radius: 0.4, + name: str(x) + ) + draw.content( + (x, 0), + str(n) + ) + } + + let flip = false + let c = 1 + for (a, b) in rules { + let i = update.position(n => n == a) + let j = update.position(n => n == b) + if i == none or j == none { + continue + } + let anchor = if flip {".south"} else {".north"} + let pt-i = str(i) + anchor + let pt-j = str(j) + anchor + let col = if j < i {red} else {green} + + draw.arc-through( + pt-i, + ( + rel: (0, if flip {-c / 10} else {c / 10}), + to: (pt-i, 50%, pt-j) + ), + pt-j, + mark: (end: ">", fill: col), + stroke: col + ) + + flip = not flip + c += 1 + } + }) + + diags.push(diag) + + /*if is-update-valid(rules-dict, update) { + total += update.at(calc.div-euclid(update.len(), 2)) + }*/ + } + + diags.last() = grid.cell( + colspan: 2 - calc.rem(diags.len() - 1, 2), + diags.last() + ) + + grid( + columns: 2, + stroke: (paint: black, dash: "dashed"), + align: center + horizon, + inset: 0.4em, + ..diags + ) +} + #show-puzzle( 5, 1, solve, - example: 143 + example: 143, + visualize: visualize ) \ No newline at end of file diff --git a/src/main.pdf b/src/main.pdf index 5c95bab..9aae509 100644 Binary files a/src/main.pdf and b/src/main.pdf differ