From 6b1ab660ac9a092573d5f26a14516f22360dc379 Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Sat, 10 Dec 2022 12:10:25 +0300 Subject: [PATCH] day 10 (ft. readme amendment. thanks day 10) --- 10-a.hs | 29 +++++++++++++++++++++++++++++ 10-b.hs | 39 +++++++++++++++++++++++++++++++++++++++ README.md | 2 +- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 10-a.hs create mode 100644 10-b.hs diff --git a/10-a.hs b/10-a.hs new file mode 100644 index 0000000..f1b4eea --- /dev/null +++ b/10-a.hs @@ -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 \ No newline at end of file diff --git a/10-b.hs b/10-b.hs new file mode 100644 index 0000000..02219a5 --- /dev/null +++ b/10-b.hs @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index af4f866..d1af586 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file +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!) \ No newline at end of file