day 6: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2025-12-06 19:34:16 +05:30
parent f40d46adbb
commit ba485f4cd3
3 changed files with 54 additions and 1 deletions

View File

@@ -55,3 +55,8 @@ executable day05
hs-source-dirs: src hs-source-dirs: src
main-is: Day05.hs main-is: Day05.hs
build-depends: libaoc build-depends: libaoc
executable day06
import: common
hs-source-dirs: src
main-is: Day06.hs

View File

@@ -45,7 +45,7 @@
name = n: "day${(if n < 10 then "0" else "") + toString n}"; name = n: "day${(if n < 10 then "0" else "") + toString n}";
in in
with pkgs.lib; with pkgs.lib;
genAttrs (map name (range 1 5)) (n: { genAttrs (map name (range 1 6)) (n: {
type = "app"; type = "app";
program = "${self'.packages.aoc2025}/bin/${n}"; program = "${self'.packages.aoc2025}/bin/${n}";
}); });

48
src/Day06.hs Normal file
View File

@@ -0,0 +1,48 @@
import Data.Bifunctor (Bifunctor (bimap))
import Data.List (transpose)
data Operation = Add | Multiply deriving (Show)
type ProblemRaw = ([String], Operation)
type Problem = ([Int], Operation)
-- We are not going to use Parsec this time, so let's just group up the columns manually based on the
-- operator's index then we can simply use transpose later for part2 as well
parse :: String -> [ProblemRaw]
parse raw =
let ls = lines raw
opLine = last ls
numLines = init ls
ops :: [(Operation, Int)]
ops =
[ (if c == '+' then Add else Multiply, i)
| (c, i) <- zip opLine [0 ..],
c /= ' '
]
starts = map snd ops
slices line =
[ take (next - start - 1) (drop start line)
| (start, next) <- zip starts (drop 1 starts ++ [length line + 1])
]
in zip (transpose (map slices numLines)) (map fst ops)
solve :: [Problem] -> Int
solve = sum . map solveProblem
where
solveProblem :: Problem -> Int
solveProblem (nums, Multiply) = product nums
solveProblem (nums, Add) = sum nums
main :: IO ()
main =
do
raw <- readFile "./inputs/day6.in"
let rawProblems = parse raw
problems1 = map (bimap (map read) id) rawProblems
problems2 = map (bimap (map read . transpose) id) rawProblems
putStr "Part 1: " >> print (solve $ problems1)
putStr "Part 2: " >> print (solve $ problems2)