diff --git a/day7.hs b/day7.hs index d3fb642..45ce054 100644 --- a/day7.hs +++ b/day7.hs @@ -36,10 +36,10 @@ prepareAmps (p1:p2:p3:p4:p5:_) software = do part2 :: [Amplifier] -> Int part2 amps = do let ampA = link (amps!!4) (amps!!0) - let ampB = link (amps!!0) (amps!!1) - let ampC = link (amps!!1) (amps!!2) - let ampD = link (amps!!2) (amps!!3) - let ampE = link (amps!!3) (amps!!4) + let ampB = link (ampA) (amps!!1) + let ampC = link (ampB) (amps!!2) + let ampD = link (ampC) (amps!!3) + let ampE = link (ampD) (amps!!4) if state ampE == [88] then head (output (amps!!4)) else part2 [ampA,ampB,ampC,ampD,ampE] @@ -64,40 +64,42 @@ calcthrusters software (p1:p2:p3:p4:p5:_) start = do operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> Amplifier -operation (99:_) state index input output = - Amplifier state index input output -operation (op:x:y:z:_) state index input output +operation (99:_) state i input output = + Amplifier state i input [] +operation (op:x:y:z:_) state i input output | last (digits op) == 1 = do - let newindex = index + 4 + let newindex = i + 4 let newstate = add (fillup (revertdigs op) 5) x y z state operation (drop newindex newstate) newstate newindex input output | last (digits op) == 2 = do - let newindex = index + 4 + let newindex = i + 4 let newstate = mult (fillup (revertdigs op) 5) x y z state - Amplifier state newindex input output - --operation (drop newindex newstate) newstate newindex input output + operation (drop newindex newstate) newstate newindex input output | last (digits op) == 3 = do - let newindex = index + 2 - let newstate = put (fillup (revertdigs op) 3) x (head input) state - let newinput = drop 1 input - operation (drop newindex newstate) newstate newindex newinput output + if length input == 0 + then Amplifier state i input output + else do + let newindex = i + 2 + let newstate = put (fillup (revertdigs op) 3) x (head input) state + let newinput = drop 1 input + operation (drop newindex newstate) newstate newindex newinput output | last (digits op) == 4 = do - let newindex = index + 2 + let newindex = i + 2 let newoutput = out (fillup (revertdigs op) 3) output x state let newinput = drop 1 input operation (drop newindex state) state newindex input newoutput | (last (digits op) == 5 ) = do - let newindex = jumpif (fillup (revertdigs op) 4) x y index state + let newindex = jumpif (fillup (revertdigs op) 4) x y i state operation (drop newindex state) state newindex input output | (last (digits op) == 6 ) = do - let newindex = jumpifnot (fillup (revertdigs op) 4) x y index state + let newindex = jumpifnot (fillup (revertdigs op) 4) x y i state operation (drop newindex state) state newindex input output | (last (digits op) == 7 ) = do - let newindex = index + 4 + let newindex = i + 4 let newstate = lessthan (fillup (revertdigs op) 5) x y z state operation (drop newindex newstate) newstate newindex input output | (last (digits op) == 8 ) = do - let newindex = index + 4 + let newindex = i + 4 let newstate = equal (fillup (revertdigs op) 5) x y z state operation (drop newindex newstate) newstate newindex input output