Safe Haskell | None |
---|
Day12
Description
General - Another "execute instructions" problem. If there is a finite set of instructions you can iterate/fold over the instructions to produce the result. If there are jumps and loops and the end of the program is determined by another condition you probably need to go with a recursion.
Part 1 - This time around we can build a list of instructions. And can just fold the instructions into a/the result. The fold will start with a position of (0, 0) and when it is done we just need to calc the manhatten distance from the final position.
Part 2 - Almost the same as part 1, but ... taking the waypoint logic into consideration.
Synopsis
- type Argument = Int
- type Position = (Int, Int)
- data Direction
- type Waypoint = (Int, Int)
- type State = (Position, Direction)
- type State' = (Position, Waypoint)
- data Operation
- data Instruction = Instruction Operation Argument
- makeOp :: Char -> Operation
- input :: String -> [Instruction]
- manhatten :: Position -> Position -> Int
- execute :: State -> Instruction -> State
- part1 :: [Instruction] -> Int
- execute' :: State' -> Instruction -> State'
- part2 :: [Instruction] -> Int
Documentation
All of the directions.
All of the operations.
data Instruction #
The instruction.
Constructors
Instruction Operation Argument |
Instances
Eq Instruction # | |
Defined in Day12 | |
Show Instruction # | |
Defined in Day12 Methods showsPrec :: Int -> Instruction -> ShowS show :: Instruction -> String showList :: [Instruction] -> ShowS |
input :: String -> [Instruction] #
Read the input file.
execute :: State -> Instruction -> State #
Execute the instruction and return the next state (part1).
part1 :: [Instruction] -> Int #
Solve part1.
execute' :: State' -> Instruction -> State' #
Execute the instruction and return the next state (part2).
part2 :: [Instruction] -> Int #
Solve part2.