For Day 4, I though about using a graph to do all the work. Luckily 8th supports graph but I think, I found a bug with 'gr:connect' word. It works fine with smaller amount of data but crashes with the actual puzzle input.
Idea is simple, build and traverse a graph. Part 2 can be solved by marking nodes for paper roll removal when traversing and removing paper rolls from marked nodes afterwards. Just repeat this until no more paper rolls can be removed.
Idea is simple, build and traverse a graph. Part 2 can be solved by marking nodes for paper roll removal when traversing and removing paper rolls from marked nodes afterwards. Just repeat this until no more paper rolls can be removed.
Code:
137 constant COLSa:new "4.txt" f:slurp ( "." "0" s:replace! "@" "1" s:replace! a:push ) s:eachline "" a:join "" s:/ ' >n a:map constant data: index> \ n1 n2 -- [row,col] n:/mod swap 2 a:close ;: build-graph \ a cols -- gr >r a:new 2 a:close ["nodes", "edges"] swap m:zip gr:new \ connect and filter edges gr:connect gr:edges ( [0,1] a:_@ ( r@ index> ) a:map a:open ( n:- n:abs 2 n:< ) a:2map a:open and ) a:filter rdrop gr:edges! ;: app:main data COLS build-graph 0 >r false 0 ( swap if gr:neighbors data swap a:_@ ' n:+ 0 a:reduce 4 n:< if 1 n:r+ then else drop then ) gr:traverse r> . cr ; Statistics: Posted by jalih — Thu Dec 04, 2025 11:54 am