-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
48 lines (40 loc) · 1.05 KB
/
run.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
import AoC.Draw.Chars (readLetters)
import Data.List.Split (chunksOf)
parse :: [String] -> [Int]
parse =
\case ["addx", d] -> [0, read @Int d]
_ -> [0]
parseAll :: String -> [Int]
parseAll = concatMap (parse . words) . lines
part1 :: [Int] -> Int
part1 input =
let values = scanl (+) 1 input
ixs = [20, 60, 100, 140, 180, 220]
in
sum $ map (\ix -> ix * values !! (ix - 1)) ixs
part2 :: [Int] -> Either String String
part2 input =
let spritePos = init $ scanl (+) 1 input
pos = cycle [0..39]
draw p sp
| abs (p - sp) <= 1 = '#'
| otherwise = '.'
in
readLetters
. unlines
. chunksOf 40
$ zipWith draw pos spritePos
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
case part2 input of
Right x -> putStrLn x
Left e -> putStrLn e