feat: solve day6, part 1

For the first part of day 6 we parse each word on each line as either
a number or as an operator (multiplication or addition). We then order
them all by column and calculate the final value once we have all
numbers and the operator.

Most of the work takes place in the parsing function, the rest is just
summing it all up.
This commit is contained in:
Jacob Jonsson 2025-12-06 08:23:12 +01:00
parent 7af90f9e9c
commit 02927269a8
2 changed files with 58 additions and 0 deletions

53
app/Day6.hs Normal file
View file

@ -0,0 +1,53 @@
module Main where
import Data.Either (lefts, rights)
import Data.IntMap (IntMap)
import qualified Data.IntMap as M
import Data.List (foldl')
import System.Environment (getArgs)
main :: IO ()
main = do
input <- readFile . head =<< getArgs
putStrLn $ unwords ["Part 1:", show $ part1 input]
putStrLn $ unwords ["Part 2:", show $ part2 input]
part1 :: String -> Int
part1 = M.foldl' (+) 0 . parse
data Oper = Mult | Add
deriving (Show)
-- Parse each space-delimited value as either a number or an
-- operator. Then combine the operator and the operands into a
-- calculation.
parse :: String -> IntMap Int
parse = M.map complete . foldl' insertPartial mempty . concat . map parseLine . lines
where
parseLine :: String -> [(Int, Either Oper Int)]
parseLine = zip [0 ..] . map parseWord . words
parseWord :: String -> Either Oper Int
parseWord "*" = Left Mult
parseWord "+" = Left Add
parseWord w = pure $ read w
insertPartial :: IntMap [Either Oper Int] -> (Int, Either Oper Int) -> IntMap [Either Oper Int]
insertPartial m (column, oper) = M.insertWith ((<>)) column [oper] m
complete :: [Either Oper Int] -> Int
complete m = case (rights m, head (lefts m)) of
(nums, Mult) -> product nums
(nums, Add) -> sum nums
part2 :: String -> Int
part2 = error "Not implemented"
testInput :: String
testInput =
unlines
[ "123 328 51 64",
" 45 64 387 23",
" 6 98 215 314",
"* + * + "
]