This commit is contained in:
Gattix 2022-12-10 07:32:31 +01:00
parent 8d23ac258a
commit 5e188c8ba0
2 changed files with 173 additions and 0 deletions

28
2022/day10/day10.hs Normal file

@ -0,0 +1,28 @@
import Data.List.Split
data OpCode = Noop | AddX { value :: Int } deriving (Show,Eq)
main :: IO ()
main = do
input <- parse <$> lines <$> readFile "input"
let c = 1:1:1:cycles 1 input
let vals = [20,60,100,140,180,220]
print $ sum [i * c !! i | i<-vals]
putStrLn $ printscr (tail c)
parse :: [String] -> [OpCode]
parse (l:ls)
| l == "noop" = Noop : parse ls
| head len2 == "addx" = AddX (read $ len2 !! 1) : parse ls
where len2 = words l
parse _ = []
cycles :: Int -> [OpCode] -> [Int]
cycles x (Noop:cs) = x : cycles x cs
cycles x (AddX y:cs) = (x+y) : (x+y) : cycles (x+y) cs
cycles _ _ = []
printscr :: [Int] -> String
printscr pxs = concat [zipWith f (ls !! x) [0..] ++ "\n" | x<-[0..5]]
where f x y = if abs (x - y) <= 1 then '#' else '.'
ls = chunksOf 40 pxs

145
2022/day10/input Normal file

@ -0,0 +1,145 @@
noop
noop
noop
addx 3
addx 7
noop
noop
noop
noop
addx 6
noop
addx -1
noop
addx 5
addx 1
noop
addx 4
noop
noop
noop
noop
addx 6
addx -1
noop
addx 3
addx -13
addx -22
noop
noop
addx 3
addx 2
addx 11
addx -4
addx 11
addx -10
addx 2
addx 5
addx 2
addx -2
noop
addx 7
addx 3
addx -2
addx 2
addx 5
addx 2
addx -2
addx -8
addx -27
addx 5
addx 2
addx 21
addx -21
addx 3
addx 5
addx 2
addx -3
addx 4
addx 3
addx 1
addx 5
noop
noop
noop
noop
addx 3
addx 1
addx 6
addx -31
noop
addx -4
noop
noop
noop
noop
addx 3
addx 7
noop
addx -1
addx 1
addx 5
noop
addx 1
noop
addx 2
addx -8
addx 15
addx 3
noop
addx 2
addx 5
noop
noop
noop
addx -28
addx 11
addx -20
noop
addx 7
addx -2
addx 7
noop
addx -2
noop
addx -6
addx 11
noop
addx 3
addx 2
noop
noop
addx 7
addx 3
addx -2
addx 2
addx 5
addx 2
addx -16
addx -10
addx -11
addx 27
addx -20
noop
addx 2
addx 3
addx 5
noop
noop
noop
addx 3
addx -2
addx 2
noop
addx -14
addx 21
noop
addx -6
addx 12
noop
addx -21
addx 24
addx 2
noop
noop
noop