From 8dddcd6224647993c75de5d9a103b6c062771df1 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 11 Dec 2024 07:36:20 +0100 Subject: [PATCH] added visualization for day 10 puzzle 1 --- src/day10/puzzle1.typ | 77 +++++++++++++++++++++++++++++++++++++++++- src/main.pdf | Bin 159033 -> 163604 bytes 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/day10/puzzle1.typ b/src/day10/puzzle1.typ index dd378b2..7f48df0 100644 --- a/src/day10/puzzle1.typ +++ b/src/day10/puzzle1.typ @@ -1,4 +1,5 @@ #import "/src/utils.typ": * +#import "@preview/cetz:0.3.1": canvas, draw, matrix #let offsets = ( (-1, 0), @@ -41,6 +42,38 @@ return tails.len() } +#let get-paths(grid, w, h, ox, oy) = { + let paths = () + let tails = () + let in-grid = in-grid.with(w, h) + + let to-visit = ((ox, oy),) + while to-visit.len() != 0 { + let (x, y) = to-visit.remove(0) + let v = grid.at(y).at(x) + for (dx, dy) in offsets { + let (x2, y2) = (x + dx, y + dy) + if not in-grid(x2, y2) { + continue + } + let v2 = grid.at(y2).at(x2) + if v2 == v + 1 { + let pos2 = (x2, y2) + if v2 == 9 { + if pos2 not in tails { + tails.push(pos2) + paths.push(((ox, oy), (x2, y2))) + } + } else { + to-visit.push(pos2) + } + } + } + } + + return paths +} + #let solve(input) = { let grid = input.split("\n").map(l => l.clusters().map(int)) @@ -61,8 +94,50 @@ return total } +#let visualize(input) = { + let grid = input.split("\n").map(l => l.clusters().map(int)) + + let w = grid.first().len() + let h = grid.len() + + let count-paths = count-paths.with(grid, w, h) + let get-paths = get-paths.with(grid, w, h) + + let total = 0 + canvas({ + let starts = () + let c + for y in range(h) { + for x in range(w) { + c = grid.at(y).at(x) + draw.rect( + (x, -y), + (x + 1, -y - 1), + fill: black.lighten((9 - c) / 9 * 90% + 10%) + ) + if c == 0 { + starts.push((x, y)) + } + } + } + for (ox, oy) in starts { + let paths = get-paths(ox, oy) + for path in paths { + draw.line( + ..path.map( + ((x, y)) => (x + .5, -y - .5) + ), + stroke: red, + mark: (end: ">") + ) + } + } + }) +} + #show-puzzle( 10, 1, solve, - example: 36 + example: 36, + visualize: visualize ) \ No newline at end of file diff --git a/src/main.pdf b/src/main.pdf index 063ee1ffe77170d804bc75c2d65af8e2cdddbe27..46198a102770cc636c42b602b2ddd426fc8c10de 100644 GIT binary patch delta 6666 zcmZvBcQl+^+;tG6*C2vui5g*M%#0aPqsNHe+bD?^o#+{z=mZlbYD6bQZ&9NY(YtVU zk%$sT$ve64z3crh-~2Oc?dNy)IeY)gv(|a)XGjt+Nc^kt`o7dclpi>JT~;<2ak_uSj<1It>#x{usiHPz=2J|*U z{|O-1_Yrmv_LA-5M{eRz1skIm<1Sv`_lwEpOp^l8_wUI|>!>wFKA=ZQGW{UEZGh*Cz zl|hjjn_0c%ScH~;xbu6_wpFY6oohC-ipTNGOU9hsWO3i*)tkhLyL?x4x3hplWfw?Vs+|;M7tZ`fpip9^sJ+5IJIs7D&Jz@i|j!mGw*a zjs|}%MSTBw;sL}u)?q0xeit8q<#~UmKh^z_1Zk1_yDu!}v*(gTb{#9B-=q&T0=vFC z&ui%LTb?7*BeQc-O$vH5?k5krE#HSLY=}x?^LkmCMQ(xc6K9p_%2Bd%{f)34!qlz1 z$Bm|Z3T{D=-eA$|xiTR0x(>8zVOG5Y9dPMGPj}3 z2GK?{^ao>BaDKgpnwp(ta8{EY3iwtxrLykbvxMel7dkN$HJ44@>e@w5+Et_W*b4af zqan7jqM;@*^Dmo($&wu+^!Z!H0?DoV294!~d!n<>y1HxM8prheQ)6n3B;JLJHY}&u zV~ZbJ{ zEn%DS%@|^hE8Zp7+n!shTUSt6Q-;CBWf(R*2`Zw4uW_xqzG89EM~|MQ^>>e{y~c=r zC@dG1cW3HudQ0M{F>!x;N7uBdy}guP$&kz(ZKxcfhl_Q7&gubj(5_m7@GMpCln-+;_PH}Za+W8ofYHoTcWmXtDox5wCvV2le~@$M_wtdwif+pZ}oOyH9yN0_r(P{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