Login
7 branches 0 tags
Ben (X220/Parabola) Put some common Make vars in a shared file cc8f273 3 years ago 467 Commits
nujel / tests / slow / day5.2.nuj
#!/usr/bin/env nujel

[defun array/2d/new [width height val]
       [def ret [array/allocate width]]
       [def x 0]
       [while [< x width]
	      [array/set! ret x [array/fill! [array/allocate height] val]]
	      [++ x]]
       ret]

[defun parse/point [text]
       [tree/zip '[:x :y] [map [split text ","] read/single]]]

[defun line/dir [start end]
       [cond [[== start end] 0]
	     [[< start end] 1]
	     [#t -1]]]

[defun line/draw [x y end-x end-y]
       [def dir-x [line/dir x end-x]]
       [def dir-y [line/dir y end-y]]
       [while [or [!= x end-x] [!= y end-y]]
	      [array/++ [ref vents x] y 1]
	      [+= x dir-x]
	      [+= y dir-y]]
       [array/++ [ref vents x] y 1]]

[defun line/draw-vents [line]
       [line/draw [tree/get [tree/get line :a] :x] [tree/get [tree/get line :a] :y] [tree/get [tree/get line :b] :x] [tree/get [tree/get line :b] :y]]]

[defun points/count [v num]
       [reduce v [\ [a b] [+ a [reduce b [\ [a b] [+ a [if [> b num] 1 0]]] 0]]] 0]]

[defun parse/line [line]
       [line/draw-vents [tree/zip '[:b :a] [map [split line "->"] parse/point]]]]

[def vents [array/2d/new 1000 1000 0]]
[def lines [split [file/read "tests/slow/day5.input"] "\n"]]
[for-each lines parse/line]
[when [!= [points/count vents 1] 21305]
      [throw [list :wrong-result "Wrong result" result]]]