XBFD2LCETFWTGOBVX4XVCYDCFJTR6NRF3LXPAJYPJKI6WCZECIMQC
noop
noop
noop
noop
addx 5
addx 5
noop
addx 3
noop
addx 2
addx 1
noop
noop
noop
addx 4
addx -4
addx 7
addx 7
noop
addx -2
addx 5
addx -23
addx 26
addx -38
noop
noop
noop
addx 3
addx 2
addx 5
addx 2
addx 9
addx -8
addx 2
addx 16
addx -9
addx 3
addx -2
addx 2
noop
addx 7
addx -2
addx 5
addx 2
addx 3
noop
addx -40
addx 5
noop
addx 2
addx -6
addx 11
addx -1
addx 3
addx 3
noop
noop
noop
addx 5
addx -2
noop
addx 7
addx 8
addx -2
addx -3
addx 5
addx 2
addx -10
addx -26
addx 1
noop
addx 8
addx -5
addx 4
addx 3
addx -3
addx 4
addx 2
addx -9
addx 16
addx 2
noop
addx 3
addx 3
addx 2
addx -2
addx 5
addx 2
addx 2
noop
addx -38
addx 34
addx -28
addx -2
addx 5
addx 2
addx 3
addx -2
addx 2
addx 7
noop
noop
addx -4
addx 5
addx 2
addx 15
addx -8
addx 3
noop
addx 2
addx -8
addx 9
addx -38
addx 26
noop
addx -18
noop
noop
addx 4
addx 4
addx -3
addx 2
addx 20
addx -12
noop
noop
noop
addx 4
addx 1
noop
addx 5
noop
noop
addx 5
noop
noop
noop
noop
noop
noop
noop
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop
module Main where
import Data.Char
import Data.List
import Data.Foldable
import Data.Functor.Identity
import Control.Monad.State
import Text.ParserCombinators.ReadP hiding (get)
import Aoc
main :: IO ()
main = do
content <- lines <$> input 10
print $ part1 content
where
state = flip evalState (0, 1) . sequence . map instruction . parse
part1 = sum . calculateSignalStrength . state
calculateSignalStrength :: [(Cycle, RegX)] -> [Int]
calculateSignalStrength = go 20
where
go :: Int -> [(Cycle, RegX)] -> [Int]
go 220 xs = [(*220) . snd . last . fst $ every20th 220 xs]
go cycle xs = ((*cycle) . snd . last . fst $ every20th cycle xs) : (go (cycle + 40) . snd $ every20th cycle xs)
every20th cycle = span ((<cycle) . fst)
data Instruction = AddX Int | Noop
cycles :: Instruction -> Int
cycles (AddX _) = 2
cycles Noop = 1
type Cycle = Int
type RegX = Int
instruction :: Instruction -> StateT (Cycle, RegX) Identity (Cycle, RegX)
instruction x = do
(cycle, regx) <- get
let newCycle = cycle + cycles x
let newRegX = regx + (case x of
Noop -> 0
(AddX a) -> a
)
put (newCycle, newRegX)
pure (newCycle, newRegX)
parseNoop :: ReadP Instruction
parseNoop = do
string "noop"
pure Noop
parseAddX :: ReadP Instruction
parseAddX = do
string "addx"
skipSpaces
n <- read <$> munch1 num
pure $ AddX n
where
num c = isDigit c || c == '-'
parseInstruction :: ReadP Instruction
parseInstruction = choice [parseNoop, parseAddX]
parse :: [String] -> [Instruction]
parse = map (fst . head . readP_to_S parseInstruction)