summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2023-01-01 14:51:09 +0100
committerAndreas Grois <andi@grois.info>2023-01-01 14:51:09 +0100
commit8955b281804938c1203c1856a762300eca4eb159 (patch)
tree2a96bcbf9f210b37e7d39c2572aac468ae17ddf1
parentc53e03ec092f131a11acf9d1cd01a51d0594aea4 (diff)
Day10, Part1
-rw-r--r--Day10/.testinput.un~bin0 -> 523 bytes
-rw-r--r--Day10/CHANGELOG.md5
-rw-r--r--Day10/Day10.cabal34
-rw-r--r--Day10/app/Main.hs67
-rw-r--r--Day10/input144
-rw-r--r--Day10/testinput146
6 files changed, 396 insertions, 0 deletions
diff --git a/Day10/.testinput.un~ b/Day10/.testinput.un~
new file mode 100644
index 0000000..1b39d53
--- /dev/null
+++ b/Day10/.testinput.un~
Binary files differ
diff --git a/Day10/CHANGELOG.md b/Day10/CHANGELOG.md
new file mode 100644
index 0000000..3890b1a
--- /dev/null
+++ b/Day10/CHANGELOG.md
@@ -0,0 +1,5 @@
+# Revision history for Day10
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/Day10/Day10.cabal b/Day10/Day10.cabal
new file mode 100644
index 0000000..49c21e3
--- /dev/null
+++ b/Day10/Day10.cabal
@@ -0,0 +1,34 @@
+cabal-version: 2.4
+name: Day10
+version: 0.1.0.0
+
+-- A short (one-line) description of the package.
+-- synopsis:
+
+-- A longer description of the package.
+-- description:
+
+-- A URL where users can report bugs.
+-- bug-reports:
+
+-- The license under which the package is released.
+-- license:
+author: Andreas Grois
+maintainer: andi@grois.info
+
+-- A copyright notice.
+-- copyright:
+-- category:
+extra-source-files: CHANGELOG.md
+
+executable Day10
+ main-is: Main.hs
+
+ -- Modules included in this executable, other than Main.
+ -- other-modules:
+
+ -- LANGUAGE extensions used by modules in this package.
+ -- other-extensions:
+ build-depends: base ^>=4.16.3.0
+ hs-source-dirs: app
+ default-language: Haskell2010
diff --git a/Day10/app/Main.hs b/Day10/app/Main.hs
new file mode 100644
index 0000000..5647726
--- /dev/null
+++ b/Day10/app/Main.hs
@@ -0,0 +1,67 @@
+module Main (main) where
+
+import System.Environment ( getArgs )
+import Text.Read (readMaybe)
+import Data.Bifunctor (bimap)
+
+main :: IO ()
+main = getArgs >>= readFile . head >>= putStr . solveDay10
+
+solveDay10 :: String -> String
+solveDay10 = formatDay10Result . fmap solveDay10Parts . parseDay10Input
+
+formatDay10Result :: Either String (Int, String) -> String
+formatDay10Result (Left s) = s
+formatDay10Result (Right (p1, p2)) = "Part 1: " ++ show p1 ++ "\n\nPart 2:\n" ++ p2
+
+data Command = NoOp | AddX Int
+
+parseDay10Input :: String -> Either String [Command]
+parseDay10Input = mapM parseCommand . lines
+
+parseCommand :: String -> Either String Command
+parseCommand "noop" = Right NoOp
+parseCommand ('a':'d':'d':'x':' ':ss) = maybe (Left "Invalid parameter for addx instruction") (Right . AddX) $ readMaybe ss
+parseCommand x = Left $ "Unknown instruction: " ++ x
+
+solveDay10Parts :: [Command] -> (Int, String)
+solveDay10Parts = bimap solveDay9Part1 solveDay9Part2 . dup
+ where dup x = (x,x)
+
+newtype CycleCount = CycleCount Int deriving (Eq, Ord)
+newtype RegisterValue = RegisterValue Int
+newtype SignalStrength = SignalStrength Int
+newtype CpuState = CpuState (CycleCount, RegisterValue)
+
+unSignalStrength :: SignalStrength -> Int
+unSignalStrength (SignalStrength a) = a
+
+signalStrength :: CycleCount -> RegisterValue -> SignalStrength
+signalStrength (CycleCount c) (RegisterValue r) = SignalStrength (c*r)
+
+getRegisterValue :: CpuState -> RegisterValue
+getRegisterValue (CpuState (_,r)) = r
+
+solveDay9Part1 :: [Command] -> Int
+solveDay9Part1 = getSumOfRelevantSignalStrengths . cpuStates
+
+cpuStates :: [Command] -> [CpuState] -- this could be solved using scanl, but the problem is easier to solve using a dedicated worker function.
+cpuStates = cpuStatesWorker (CpuState (CycleCount 1, RegisterValue 1))
+
+cpuStatesWorker :: CpuState -> [Command] -> [CpuState]
+cpuStatesWorker c [] = return c
+cpuStatesWorker (CpuState (CycleCount c, r)) (NoOp:cs) = cpuStatesWorker (CpuState (CycleCount (c+1), r)) cs
+cpuStatesWorker (CpuState (CycleCount c, RegisterValue r)) ((AddX x):cs) = newState:cpuStatesWorker newState cs
+ where newState = CpuState (CycleCount (c+2), RegisterValue (r+x))
+
+getSumOfRelevantSignalStrengths :: [CpuState] -> Int
+getSumOfRelevantSignalStrengths s = sum $ map (unSignalStrength . getSignalStrengthAtCycleCount s . CycleCount) $ take 6 $ iterate (+40) 20
+
+getSignalStrengthAtCycleCount :: [CpuState] -> CycleCount -> SignalStrength
+getSignalStrengthAtCycleCount s c = signalStrength c $ getRegisterAtCycleCount s c
+
+getRegisterAtCycleCount :: [CpuState] -> CycleCount -> RegisterValue
+getRegisterAtCycleCount s c = getRegisterValue $ last $ takeWhile (\(CpuState (cc,_)) -> cc <= c) s
+
+solveDay9Part2 :: [Command] -> String
+solveDay9Part2 c = "Not yet done, sorry.\n" \ No newline at end of file
diff --git a/Day10/input b/Day10/input
new file mode 100644
index 0000000..0957ac2
--- /dev/null
+++ b/Day10/input
@@ -0,0 +1,144 @@
+addx 1
+noop
+addx 29
+addx -24
+addx 4
+addx 3
+addx -2
+addx 3
+addx 1
+addx 5
+addx 3
+addx -2
+addx 2
+noop
+noop
+addx 7
+noop
+noop
+noop
+addx 5
+addx 1
+noop
+addx -38
+addx 21
+addx 8
+noop
+addx -19
+addx -2
+addx 2
+addx 5
+addx 2
+addx -12
+addx 13
+addx 2
+addx 5
+addx 2
+addx -18
+addx 23
+noop
+addx -15
+addx 16
+addx 7
+noop
+noop
+addx -38
+noop
+noop
+noop
+noop
+noop
+noop
+addx 8
+addx 2
+addx 3
+addx -2
+addx 4
+noop
+noop
+addx 5
+addx 3
+noop
+addx 2
+addx 5
+noop
+noop
+addx -2
+noop
+addx 3
+addx 6
+noop
+addx -38
+addx -1
+addx 35
+addx -6
+addx -19
+addx -2
+addx 2
+addx 5
+addx 2
+addx 3
+noop
+addx 2
+addx 3
+addx -2
+addx 2
+noop
+addx -9
+addx 16
+noop
+addx 9
+addx -3
+addx -36
+addx -2
+addx 11
+addx 22
+addx -28
+noop
+addx 3
+addx 2
+addx 5
+addx 2
+addx 3
+addx -2
+addx 2
+noop
+addx 3
+addx 2
+noop
+addx -11
+addx 16
+addx 2
+addx 5
+addx -31
+noop
+addx -6
+noop
+noop
+noop
+noop
+noop
+addx 7
+addx 30
+addx -24
+addx -1
+addx 5
+noop
+noop
+noop
+noop
+noop
+addx 5
+noop
+addx 5
+noop
+addx 1
+noop
+addx 2
+addx 5
+addx 2
+addx 1
+noop
+noop
+noop
+noop
diff --git a/Day10/testinput b/Day10/testinput
new file mode 100644
index 0000000..37ee8ee
--- /dev/null
+++ b/Day10/testinput
@@ -0,0 +1,146 @@
+addx 15
+addx -11
+addx 6
+addx -3
+addx 5
+addx -1
+addx -8
+addx 13
+addx 4
+noop
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx -35
+addx 1
+addx 24
+addx -19
+addx 1
+addx 16
+addx -11
+noop
+noop
+addx 21
+addx -15
+noop
+noop
+addx -3
+addx 9
+addx 1
+addx -3
+addx 8
+addx 1
+addx 5
+noop
+noop
+noop
+noop
+noop
+addx -36
+noop
+addx 1
+addx 7
+noop
+noop
+noop
+addx 2
+addx 6
+noop
+noop
+noop
+noop
+noop
+addx 1
+noop
+noop
+addx 7
+addx 1
+noop
+addx -13
+addx 13
+addx 7
+noop
+addx 1
+addx -33
+noop
+noop
+noop
+addx 2
+noop
+noop
+noop
+addx 8
+noop
+addx -1
+addx 2
+addx 1
+noop
+addx 17
+addx -9
+addx 1
+addx 1
+addx -3
+addx 11
+noop
+noop
+addx 1
+noop
+addx 1
+noop
+noop
+addx -13
+addx -19
+addx 1
+addx 3
+addx 26
+addx -30
+addx 12
+addx -1
+addx 3
+addx 1
+noop
+noop
+noop
+addx -9
+addx 18
+addx 1
+addx 2
+noop
+noop
+addx 9
+noop
+noop
+noop
+addx -1
+addx 2
+addx -37
+addx 1
+addx 3
+noop
+addx 15
+addx -21
+addx 22
+addx -6
+addx 1
+noop
+addx 2
+addx 1
+noop
+addx -10
+noop
+noop
+addx 20
+addx 1
+addx 2
+addx 2
+addx -6
+addx -11
+noop
+noop
+noop