Login
7 branches 0 tags
Ben (RPi4) Some improvements to the stdlib 5c55298 4 years ago 224 Commits
nujel / tests / day2-2.nuj
#!/usr/bin/env nujel

[def command-map-1 @[forward [\ [state val] [tree/set! state :horiz [+ [state :horiz] val]]]
                     up      [\ [state val] [tree/set! state :depth [- [state :depth] val]]]
                     down    [\ [state val] [tree/set! state :depth [+ [state :depth] val]]]
]]

[defun do-command [name val state command-map]
       [defun cmd [command-map name]]
       [if cmd
           [cmd state val]
           [println "Unknown command"]]
]

[defun step [l state command-map]
       [if [and l [cdr l]]
           [step [cddr l] [do-command [car l] [cadr l] state command-map]]
           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] command-map-1]]
[println [ansi-yellow "Now for the real deal!"]]
[print-result [step [read [file/read "day2.input"]] @[:depth 0 :horiz 0] command-map-1]]
[if [eq? result 1561344]
    [exit 0]
    [exit 1]]