Solve AOC day 12 pt 1

This commit is contained in:
2020-12-12 15:12:16 -07:00
parent fed69f1142
commit 9600766b22
5 changed files with 835 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
(defprop &mut x 0)
(defprop &mut y 0)
// 0 is east
// 1 is south
// 2 is west
// 3 is north
(defprop &mut facing 0)
(defun fixFacing [f]
(Math.floor (% (if (> 0 f) (+ 4 f) f) 4)))
(defmethod N [num]
(set y (+ y num)))
(defmethod S [num]
(set y (- y num)))
(defmethod E [num]
(set x (+ x num)))
(defmethod W [num]
(set x (- x num)))
(defmethod R [angle]
(set facing (fixFacing (+ facing (/ angle 90)))))
(defmethod L [angle]
(set facing (fixFacing (- facing (/ angle 90)))))
(defmethod F [num]
(case facing
(0 (E num))
(1 (S num))
(2 (W num))
(3 (N num))
(otherwise (throw (+ "Bad facing" facing)))))
(defreadermacro "" [stream]
(stream.dropWhitespace)
(if (stream.isEmpty)
null
`(,(ReaderExp.Symbol
(stream.expect "a ship command" (lambda [] (stream.takeChars 1))))
,(ReaderExp.Symbol (stream.expect "a number argument" (lambda [] (stream.takeUntilAndDrop #|"\n"|#)))))))