Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 Commits
nujel / tests / slow / day5.2.nuj
#!/usr/bin/env nujel

(require :array/2d)

(defn array/2d/new (width height val)
       (def ret (:alloc Array width))
       (def x 0)
       (while (< x width)
	      (set! ret x (array/fill! (:alloc Array height) val))
	      (inc! x))
       ret)

(defn parse/point (text)
       (tree/zip '(:x :y) (map (split text ",") read/single)))

(defn line/dir (start end)
       (cond ((= start end) 0)
	     ((< start end) 1)
	     (#t -1)))

(defn 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 (not= x end-x) (not= y end-y))
	      (array/++ (ref vents x) y 1)
	      (inc! x dir-x)
	      (inc! y dir-y))
       (array/++ (ref vents x) y 1))

(defn line/draw-vents (line)
       (line/draw (ref (ref line :a) :x) (ref (ref line :a) :y) (ref (ref line :b) :x) (ref (ref line :b) :y)))

(defn points/count (v num)
       (reduce v (fn (a b) (+ a (reduce b (fn (a b) (+ a (if (> b num) 1 0))) 0))) 0))

(defn 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 (not= (points/count vents 1) 21305)
      (throw (list :wrong-result "Wrong result" result)))

(return :success)