From 5ecfd58ef179b226bb5aed4bec05ab11ab897755 Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Sat, 10 Dec 2022 11:30:41 -0500 Subject: [PATCH] Finish day 10 --- .gitignore | 4 +- aoc2022.cabal | 25 ++++++++ day10/Day10Part1.hs | 11 ++++ day10/Day10Part2.hs | 8 +++ day10/input | 146 ++++++++++++++++++++++++++++++++++++++++++++ package.yaml | 24 ++++++++ src/Day10Lib.hs | 93 ++++++++++++++++++++++++++++ stack.yaml.lock | 7 +++ 8 files changed, 316 insertions(+), 2 deletions(-) create mode 100644 day10/Day10Part1.hs create mode 100644 day10/Day10Part2.hs create mode 100644 day10/input create mode 100644 src/Day10Lib.hs diff --git a/.gitignore b/.gitignore index c368d45..6e0162c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -.stack-work/ -*~ \ No newline at end of file +example.txt +example*.txt diff --git a/aoc2022.cabal b/aoc2022.cabal index 15516ce..844adb6 100644 --- a/aoc2022.cabal +++ b/aoc2022.cabal @@ -25,6 +25,7 @@ source-repository head library exposed-modules: + Day10Lib Day1Lib Day2Lib Day3Lib @@ -45,6 +46,30 @@ library , split default-language: Haskell2010 +executable Day10Part1 + main-is: Day10Part1.hs + hs-source-dirs: + day10 + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N -main-is Day10Part1 + build-depends: + aoc2022 + , base >=4.7 && <5 + , containers + , split + default-language: Haskell2010 + +executable Day10Part2 + main-is: Day10Part2.hs + hs-source-dirs: + day10 + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N -main-is Day10Part2 + build-depends: + aoc2022 + , base >=4.7 && <5 + , containers + , split + default-language: Haskell2010 + executable Day1Part1 main-is: Day1Part1.hs hs-source-dirs: diff --git a/day10/Day10Part1.hs b/day10/Day10Part1.hs new file mode 100644 index 0000000..9a47fde --- /dev/null +++ b/day10/Day10Part1.hs @@ -0,0 +1,11 @@ +module Day10Part1 (main) where + +import Day10Lib + +convertToString :: Int -> String +convertToString = show + +main :: IO () +main = do + input <- getContents + putStrLn $ convertToString $ day10 input diff --git a/day10/Day10Part2.hs b/day10/Day10Part2.hs new file mode 100644 index 0000000..e39ff58 --- /dev/null +++ b/day10/Day10Part2.hs @@ -0,0 +1,8 @@ +module Day10Part2 (main) where + +import Day10Lib + +main :: IO () +main = do + input <- getContents + putStrLn $ day10' input diff --git a/day10/input b/day10/input new file mode 100644 index 0000000..1b68dd1 --- /dev/null +++ b/day10/input @@ -0,0 +1,146 @@ +noop +addx 5 +addx -2 +noop +noop +addx 7 +addx 15 +addx -14 +addx 2 +addx 7 +noop +addx -2 +noop +addx 3 +addx 4 +noop +noop +addx 5 +noop +noop +addx 1 +addx 2 +addx 5 +addx -40 +noop +addx 5 +addx 2 +addx 15 +noop +addx -10 +addx 3 +noop +addx 2 +addx -15 +addx 20 +addx -2 +addx 2 +addx 5 +addx 3 +addx -2 +noop +noop +noop +addx 5 +addx 2 +addx 5 +addx -38 +addx 3 +noop +addx 2 +addx 5 +noop +noop +addx -2 +addx 5 +addx 2 +addx -2 +noop +addx 7 +noop +addx 10 +addx -5 +noop +noop +noop +addx -15 +addx 22 +addx 3 +noop +noop +addx 2 +addx -37 +noop +noop +addx 13 +addx -10 +noop +addx -5 +addx 10 +addx 5 +addx 2 +addx -6 +addx 11 +addx -2 +addx 2 +addx 5 +addx 3 +noop +addx 3 +addx -2 +noop +addx 6 +addx -22 +addx 23 +addx -38 +noop +addx 7 +noop +addx 5 +noop +noop +noop +addx 9 +addx -8 +addx 2 +addx 7 +noop +noop +addx 2 +addx -4 +addx 5 +addx 5 +addx 2 +addx -26 +addx 31 +noop +addx 3 +noop +addx -40 +addx 7 +noop +noop +noop +noop +addx 2 +addx 4 +noop +addx -1 +addx 5 +noop +addx 1 +noop +addx 2 +addx 5 +addx 2 +noop +noop +noop +addx 5 +addx 1 +noop +addx 4 +addx 3 +noop +addx -24 +noop diff --git a/package.yaml b/package.yaml index 1820d1f..243b5db 100644 --- a/package.yaml +++ b/package.yaml @@ -255,6 +255,30 @@ executables: dependencies: - aoc2022 + Day10Part1: + main: Day10Part1.hs + other-modules: [] + source-dirs: day10 + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N + - -main-is Day10Part1 + dependencies: + - aoc2022 + + Day10Part2: + main: Day10Part2.hs + other-modules: [] + source-dirs: day10 + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N + - -main-is Day10Part2 + dependencies: + - aoc2022 + tests: Day1-test: main: Day1.hs diff --git a/src/Day10Lib.hs b/src/Day10Lib.hs new file mode 100644 index 0000000..00cab3f --- /dev/null +++ b/src/Day10Lib.hs @@ -0,0 +1,93 @@ +module Day10Lib where + +import Data.List (transpose) + +data Instruction = Noop | Addx Int deriving (Show) +type Register = Int +type Cycle = Int + +type Computer = (Register, Cycle) + +processInstruction :: Computer -> Instruction -> [Computer] +processInstruction (r, c) (Addx x) = [(r,c + 1), (r + x, c + 2)] +processInstruction (r, c) Noop = [(r,c + 1)] + +processInstructions :: Computer -> [Instruction] -> [Computer] +processInstructions comp@(r,c) (ins:inss) = processedComps ++ processInstructions processedComp inss + -- | c == 220 = [processedComp] + -- -- | c == 20 = processedComp : r * c + processInstructions processedComp inss + -- -- | c `mod` 40 == 0 = r * c + processInstructions processedComp inss + -- | otherwise = processedComp : processInstructions processedComp inss + where + processedComps = processInstruction comp ins + processedComp = last processedComps + +-- PARSING +convertToInt :: String -> Int +convertToInt s = read s :: Int + +parseInstruction :: String -> Instruction +parseInstruction s = case firstPart of + "noop" -> Noop + "addx" -> Addx lastPart + where + asWords = words s + firstPart = head asWords + lastPart = convertToInt $ last asWords + +parseInput :: String -> [Instruction] +parseInput i = map parseInstruction $ lines i + +-- Increment cycle by 1 to account for offset of looking at previous cycle to determine during cycle value +accountForDuringCycleOffset :: Computer -> Computer +accountForDuringCycleOffset (r, c) = (r, c + 1) + +getSignalStrength :: Computer -> Int +getSignalStrength (r, c) = r * c + +day10 :: String -> Int +day10 input = registerSum + where + -- There are less instructions than cycles we are interested in + -- The problem isn't clear about this but I believe the program loops + instructions = cycle $ parseInput input + -- Infinite computer list + startingState = (1,0) + icl = processInstructions (1, 0) instructions + -- -2 on all indexs, one for zero indexing the other for being about the value _during_ the cycle + cyclesOfInterest = [icl !! 18, icl !! 58, icl !! 98, icl !! 138, icl !! 178, icl !! 218] + registers = map getSignalStrength $ map accountForDuringCycleOffset cyclesOfInterest + registerSum = sum registers + +type Pixel = Bool + +drawPixel :: Pixel -> Char +drawPixel False = '.' +drawPixel True = '#' + +drawPixelGrid :: [[Pixel]] -> String +drawPixelGrid grid = unlines $ map (map drawPixel) grid + +rowifyPixelList :: [Pixel] -> [[Pixel]] +rowifyPixelList pl = [r1, r2, r3, r4, r5, r6] + where + r1 = take 40 pl + r2 = take 40 $ drop 40 pl + r3 = take 40 $ drop 80 pl + r4 = take 40 $ drop 120 pl + r5 = take 40 $ drop 160 pl + r6 = take 40 $ drop 200 pl + +zipPixelListWithComputerList :: Computer -> Int -> Pixel +zipPixelListWithComputerList (r, c) pindex = if r == horizontalPostion || r - horizontalPostion == 1 || r - horizontalPostion == -1 + then True + else False + where horizontalPostion = pindex `mod` 40 + +day10' :: String -> String +day10' input = drawPixelGrid $ rowifyPixelList $ zipWith zipPixelListWithComputerList icl pixelgrid + where + instructions = cycle $ parseInput input + startingState = (1,0) + icl = startingState : processInstructions (1, 0) instructions + pixelgrid = [0..239] diff --git a/stack.yaml.lock b/stack.yaml.lock index 481127e..eba7789 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -11,6 +11,13 @@ packages: size: 439 original: hackage: split-0.2.3.5 +- completed: + hackage: containers-0.6.4.1@sha256:e9ca297369c207ff40ed561877c15928292b957a01c8551a7cbd50d665a03429,2520 + pantry-tree: + sha256: cd4689c9caa5b9774b3ec4603c3b2c06eb04b184db590afa6d107441e25cc634 + size: 2823 + original: + hackage: containers-0.6.4.1 snapshots: - completed: sha256: 6d1532d40621957a25bad5195bfca7938e8a06d923c91bc52aa0f3c41181f2d4