Finish day 10
This commit is contained in:
parent
0c00d7f3d4
commit
5ecfd58ef1
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
.stack-work/
|
example.txt
|
||||||
*~
|
example*.txt
|
||||||
|
|||||||
@ -25,6 +25,7 @@ source-repository head
|
|||||||
|
|
||||||
library
|
library
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
|
Day10Lib
|
||||||
Day1Lib
|
Day1Lib
|
||||||
Day2Lib
|
Day2Lib
|
||||||
Day3Lib
|
Day3Lib
|
||||||
@ -45,6 +46,30 @@ library
|
|||||||
, split
|
, split
|
||||||
default-language: Haskell2010
|
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
|
executable Day1Part1
|
||||||
main-is: Day1Part1.hs
|
main-is: Day1Part1.hs
|
||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
|
|||||||
11
day10/Day10Part1.hs
Normal file
11
day10/Day10Part1.hs
Normal file
@ -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
|
||||||
8
day10/Day10Part2.hs
Normal file
8
day10/Day10Part2.hs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module Day10Part2 (main) where
|
||||||
|
|
||||||
|
import Day10Lib
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
input <- getContents
|
||||||
|
putStrLn $ day10' input
|
||||||
146
day10/input
Normal file
146
day10/input
Normal file
@ -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
|
||||||
24
package.yaml
24
package.yaml
@ -255,6 +255,30 @@ executables:
|
|||||||
dependencies:
|
dependencies:
|
||||||
- aoc2022
|
- 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:
|
tests:
|
||||||
Day1-test:
|
Day1-test:
|
||||||
main: Day1.hs
|
main: Day1.hs
|
||||||
|
|||||||
93
src/Day10Lib.hs
Normal file
93
src/Day10Lib.hs
Normal file
@ -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]
|
||||||
@ -11,6 +11,13 @@ packages:
|
|||||||
size: 439
|
size: 439
|
||||||
original:
|
original:
|
||||||
hackage: split-0.2.3.5
|
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:
|
snapshots:
|
||||||
- completed:
|
- completed:
|
||||||
sha256: 6d1532d40621957a25bad5195bfca7938e8a06d923c91bc52aa0f3c41181f2d4
|
sha256: 6d1532d40621957a25bad5195bfca7938e8a06d923c91bc52aa0f3c41181f2d4
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user