Login
7 branches 0 tags
Ben (X13/Arch) Some convenience functions for todays AoC a6192c3 4 years ago 228 Commits
nujel / tests / day2.nuj
#!/usr/bin/env nujel

[defun tree/+= [tree key inc]
       [tree/set! tree key [+ tree key inc]]
       tree
]
[defun do-command [name val state]
       [println [cat "Name: " name " Value: " val " State: " [str/write state]]]
       [case name
             [['forward]  [tree/+= state :horiz  val]]
             [['up]       [tree/+= state :depth -val]]
             [['down]     [tree/+= state :depth  val]]
             [otherwise   [println [cat "Unknown Command " name ", continuing."]]]
       ]
       [println [cat "Name: " name " Value: " val " State: " [str/write state]]]
       state
]

[defun step [l state]
       [println [cat "State: " [str/write state]]]
       [if [and l [cdr l]]
           [step [cddr l] [do-command [car l] [cadr l] state]]
           state]
]

[def result 0]
[defun print-result [state]
       [println [str/write state]]
       [set! result [* [state :depth] [state :horiz]]]
       [println [cat "Resulting in: " [ansi-green result]]]
]

[println "First the example:"]
[print-result [step '[forward 5 down 5 forward 8 up 3 down 8 forward 2] @[:depth 0 :horiz 0]]]
[println [ansi-yellow "Now for the real deal!"]]
[print-result [step [read [file/read "day2.input"]] @[:depth 0 :horiz 0]]]
[if [eq? result 1561344]
    [exit 0]
    [exit 1]]