diff --git a/progress.png b/progress.png index 458d07a..73dceb6 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index c105224..f00fe45 100644 --- a/progress.yaml +++ b/progress.yaml @@ -13,4 +13,6 @@ 7: stars: 2 8: - stars: 2 \ No newline at end of file + stars: 2 +9: + stars: 1 \ No newline at end of file diff --git a/res/examples/day9_1.txt b/res/examples/day9_1.txt new file mode 100644 index 0000000..bd41cba --- /dev/null +++ b/res/examples/day9_1.txt @@ -0,0 +1 @@ +12345 \ No newline at end of file diff --git a/res/examples/day9_2.txt b/res/examples/day9_2.txt new file mode 100644 index 0000000..5ff5aae --- /dev/null +++ b/res/examples/day9_2.txt @@ -0,0 +1 @@ +2333133121414131402 \ No newline at end of file diff --git a/src/day9/puzzle1.typ b/src/day9/puzzle1.typ new file mode 100644 index 0000000..4c0a000 --- /dev/null +++ b/src/day9/puzzle1.typ @@ -0,0 +1,80 @@ +#import "/src/utils.typ": * + +#let compute-checksum(blocks) = { + let total = 0 + for (i0, l, id) in blocks { + total += id * range(i0, i0 + l).sum() + } + return total +} + +#let insert(list, elmt) = { + for (i, elmt2) in list.enumerate() { + if elmt.first() < elmt2.first() { + list.insert(i, elmt) + return list + } + } + list.push(elmt) + return list +} + +#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 + } + + 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 + } + } + + return compute-checksum(blocks) +} + +#show-puzzle( + 9, 1, + solve, + example: ( + "1": 60, + "2": 1928 + ), + only-example: true +) + +// Too long to recompile everytime +#show-result(6390180901651) \ No newline at end of file diff --git a/src/day9/puzzle2.typ b/src/day9/puzzle2.typ new file mode 100644 index 0000000..e69de29 diff --git a/src/main.pdf b/src/main.pdf index a92d614..2485346 100644 Binary files a/src/main.pdf and b/src/main.pdf differ