~shunter/advent2019

6d94704202aedefbc6dc269ba280ff0130c1905f — Samuel Hunter 4 years ago 5559eb4 master
Solve Day 16 Part 1
2 files changed, 42 insertions(+), 2 deletions(-)

M input/day16.txt
M src/day16.lisp
M input/day16.txt => input/day16.txt +1 -1
@@ 1,1 1,1 @@
()
"59727310424796235189476878806940387435291429226818921130171187957262146115559932358924341808253400617220924411865224341744614706346865536561788244183609411225788501102400269978290670307147139438239865673058478091682748114942700860895620690690625512670966265975462089087644554004423208369517716075591723905075838513598360188150158989179151879406086757964381549720210763972463291801513250953430219653258827586382953297392567981587028568433943223260723561880121205475323894070000380258122357270847092900809245133752093782889315244091880516672127950518799757198383131025701009960944008679555864631340867924665650332161673274408001712152664733237178121872"

M src/day16.lisp => src/day16.lisp +41 -1
@@ 7,6 7,46 @@

(defparameter *input* (read-day 16))

(defun solve-part1 ())
(defun to-signal (input)
  (loop :with result := (make-array (length input))
        :for c :across input
        :for i :from 0
        :do (setf (aref result i) (- (char-code c) (char-code #\0)))
        :finally (return result)))

(defun element-pattern (n)
  (loop :with length := (* 4 (1+ n))
        :with result := (make-array length :initial-element 100)
        :for i :below (1+ n)

        :do (setf (aref result (+ i 0)) 0
                  (aref result (+ i (1+ n))) 1
                  (aref result (+ i (1+ n) (1+ n))) 0
                  (aref result (+ i (1+ n) (1+ n) (1+ n))) -1)
        :finally (return result)))

(defun next-element (phase n)
  (loop :with element-pattern := (element-pattern n)
        :for v :across phase
        :for epi := 1 :then (mod (1+ epi) (length element-pattern))
        :for ep := (aref element-pattern epi)
        :sum (* v ep) :into result
        :finally (return (mod (abs result) 10))))

(defun next-phase (phase)
  (loop :with result-phase := (make-array (length phase))
        :for i :below (length phase)
        :do (setf (aref result-phase i)
                  (next-element phase i))
        :finally (return result-phase)))

(defun phases (phase n)
  (loop :for current := phase :then (next-phase current)
        :repeat n
        :finally (return current)))

(defun solve-part1 ()
  (subseq (phases (to-signal *input*) 100)
          0 8))

(defun solve-part2 ())