From cee23e50347edef5812c32c72edc643c6b7ed2d4 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 9 Dec 2024 20:35:35 +0100 Subject: [PATCH] added visualization for day 9 puzzle 1 --- src/day9/puzzle1.typ | 109 +++++++++++++++++++++++++++++++++++-------- src/day9/puzzle2.typ | 10 ++-- src/main.pdf | Bin 152251 -> 153648 bytes 3 files changed, 96 insertions(+), 23 deletions(-) diff --git a/src/day9/puzzle1.typ b/src/day9/puzzle1.typ index 4c0a000..9f7ab9d 100644 --- a/src/day9/puzzle1.typ +++ b/src/day9/puzzle1.typ @@ -1,5 +1,28 @@ #import "/src/utils.typ": * +#let parse-input(input) = { + let blocks = () + let holes = () + + let block-i = 0 + let is-block = true + let pos = 0 + for c in input { + let block = (pos, int(c)) + if is-block { + block.push(block-i) + block-i += 1 + blocks.push(block) + } else { + holes.push(block) + } + pos += int(c) + is-block = not is-block + } + + return (blocks, holes) +} + #let compute-checksum(blocks) = { let total = 0 for (i0, l, id) in blocks { @@ -20,24 +43,7 @@ } #let solve(input) = { - let blocks = () - let holes = () - - let block-i = 0 - let is-block = true - let pos = 0 - for c in input { - let block = (pos, int(c)) - if is-block { - block.push(block-i) - block-i += 1 - blocks.push(block) - } else { - holes.push(block) - } - pos += int(c) - is-block = not is-block - } + let (blocks, holes) = parse-input(input) for (hi, hl) in holes { while hl > 0 { @@ -66,6 +72,70 @@ return compute-checksum(blocks) } +#let visualize(input) = { + let (blocks, holes) = parse-input(input) + let max-id = blocks.last().last() + + let col-gradient = gradient.linear(red, orange, yellow, green, aqua, blue, purple) + + let show-fs(size, blocks) = { + let cells = () + for (bi, bl, bid) in blocks { + cells.push( + grid.cell( + x: bi, + colspan: bl, + fill: col-gradient.sample(bid * 100% / max-id), + str(bid) + ) + ) + } + grid( + columns: (1fr,) * size, + align: center + horizon, + stroke: black, + inset: 0.3em, + ..cells + ) + } + + let last-block = blocks.last() + let last-holes = holes.last() + let show-fs = show-fs.with( + calc.max( + last-block.first() + last-block.at(1), + last-holes.first() + last-holes.last() + ) + ) + let steps = () + steps.push(show-fs(blocks)) + + for (hi, hl) in holes { + while hl > 0 { + if blocks.last().first() < hi + hl { + break + } + let (bi, bl, bid) = blocks.pop() + let len = calc.min(hl, bl) + blocks.insert(0, (hi, len, bid)) + if len < bl { + blocks = insert(blocks, (bi, bl - len, bid)) + } + hl -= len + hi += len + } + if hl > 0 { + break + } + steps.push(show-fs(blocks)) + } + + stack( + spacing: 0.5em, + ..steps + ) +} + #show-puzzle( 9, 1, solve, @@ -73,7 +143,8 @@ "1": 60, "2": 1928 ), - only-example: true + only-example: true, + visualize: visualize ) // Too long to recompile everytime diff --git a/src/day9/puzzle2.typ b/src/day9/puzzle2.typ index 75198ee..2048d90 100644 --- a/src/day9/puzzle2.typ +++ b/src/day9/puzzle2.typ @@ -27,13 +27,12 @@ let is-block = true let pos = 0 for c in input { - let block = (pos, int(c)) + let size = int(c) if is-block { - block.push(block-i) + blocks.push((pos, size, block-i)) block-i += 1 - blocks.push(block) } else { - holes.push(block) + holes.push((pos, size)) } pos += int(c) is-block = not is-block @@ -46,6 +45,9 @@ bi = hi holes.at(i).first() += bl holes.at(i).last() -= bl + if bl == hl { + holes.remove(i) + } break } } diff --git a/src/main.pdf b/src/main.pdf index f9b932a419f560314b1938e1e4cc81d16726781b..7a62a2f3426e8d716154c94dedcb8ab13e260cae 100644 GIT binary patch delta 3049 zcmZuzc{G%Z8)obyrLiw#i;$(6`98B)F2gm1k|k?G)-+kBAuSBi%*|ewp-_w^n&#rt z^s{9P-HVu8s0c|SOOa%|-Tdx7r;fSbKi@ge`@YZfKF@o;bH0XBk*X?@xLV#K1t9>3 z(g)CBrrDY_y;*^`@kfA7SL7wF@uLt3938L2t3f$#8zyNb-n{s0Ze!M$jIz7+>yctM zyRX4SCOpT*RrZ_5Ntz`sEiSM0eDaG5nn%jI&2=vIkq;kRVVv-Oa-$4CIc?QuCGM!& zfiRA#+X%9nhtD(bN1MD`h}pZQWg2ev1YLJ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