day 10 (ft. readme amendment. thanks day 10)

This commit is contained in:
Jill 2022-12-10 12:10:25 +03:00
parent 6a31e36b72
commit 6b1ab660ac
3 changed files with 69 additions and 1 deletions

29
10-a.hs Normal file
View File

@ -0,0 +1,29 @@
data Instruction = Add Int | Noop
deriving (Eq, Show)
getCyclesAt = [20, 60 .. 220]
parseInstruction :: String -> Instruction
parseInstruction s = case head split of
"addx" -> Add (read $ split !! 1)
"noop" -> Noop
instr -> error $ "unknown instruction " ++ instr
where split = words s
parse :: String -> [Instruction]
parse = map parseInstruction . lines
executeProgram :: [Instruction] -> [Int]
executeProgram = concat . scanl exec [1]
where exec :: [Int] -> Instruction -> [Int]
exec xs instr = case instr of
(Add y) -> [x, x + y]
Noop -> [x]
where x = last xs
main = interact $
show
. sum
. (\cycles -> map (\i -> i * cycles !! (i-1)) getCyclesAt)
. executeProgram
. parse

39
10-b.hs Normal file
View File

@ -0,0 +1,39 @@
import GHC.Utils.Misc (chunkList)
data Instruction = Add Int | Noop
deriving (Eq, Show)
crtWidth = 40
crtHeight = 6
parseInstruction :: String -> Instruction
parseInstruction s = case head split of
"addx" -> Add (read $ split !! 1)
"noop" -> Noop
instr -> error $ "unknown instruction " ++ instr
where split = words s
parse :: String -> [Instruction]
parse = map parseInstruction . lines
executeProgram :: [Instruction] -> [Int]
executeProgram = concat . scanl exec [1]
where exec :: [Int] -> Instruction -> [Int]
exec xs instr = case instr of
(Add y) -> [x, x + y]
Noop -> [x]
where x = last xs
drawScreen :: [Int] -> String
drawScreen cycles = unlines $ chunkList crtWidth $ map drawChar [0 .. (crtWidth * crtHeight - 1)]
where drawChar :: Int -> Char
drawChar i = if xDist <= 1 then '#' else '.'
where xScan = i `mod` crtWidth
xSprite = cycles !! (i)
xDist = abs (xScan - xSprite)
main = interact $
(++ "\ni hope it is understandable for this to be an exception to the \"program must output something that's copypastable into aoc's output field\" rule that i've set for myself. read!!!!\n")
. drawScreen
. executeProgram
. parse

View File

@ -10,4 +10,4 @@ you shouldn't; but for documentation purposes it's
$ ghc 1-a.hs && cat 1_input | ./1-a
```
you'll need to retrieve `1_input` from your aoc, but all programs will strictly take in the aoc input and output what can be pasted into the anwser field
you'll need to retrieve `1_input` from your aoc, but all programs will strictly take in the aoc input and ~~output what can be pasted into the anwser field~~ (thanks, day 10 part 2!)