Compare commits
3 Commits
8828c9fe7f
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b4873f9a9
|
||
|
|
d57a39fb48
|
||
|
|
934a2d437f
|
@@ -6,5 +6,5 @@
|
||||
|02 |★ ★ |08 | |
|
||||
|03 |★ ★ |09 | |
|
||||
|04 |★ ★ |10 | |
|
||||
|05 |★ |11 | |
|
||||
|06 | |12 | |
|
||||
|05 |★ ★ |11 | |
|
||||
|06 |★ ★ |12 | |
|
||||
|
||||
@@ -35,3 +35,4 @@ executable adventofcode2025
|
||||
Day03
|
||||
Day04
|
||||
Day05
|
||||
Day06
|
||||
|
||||
@@ -42,5 +42,5 @@ day02_2 = do
|
||||
values <- parseInput
|
||||
let result = sumInvalid' values
|
||||
putStrLn $
|
||||
"Day 2, Puzzle 1 solution: "
|
||||
"Day 2, Puzzle 2 solution: "
|
||||
++ show result
|
||||
|
||||
19
src/Day05.hs
19
src/Day05.hs
@@ -2,9 +2,11 @@
|
||||
|
||||
module Day05
|
||||
( day05_1,
|
||||
day05_2,
|
||||
)
|
||||
where
|
||||
|
||||
import Data.List (sortOn)
|
||||
import Data.List.Split (splitOn)
|
||||
|
||||
getRange :: String -> (Int, Int)
|
||||
@@ -20,6 +22,14 @@ isFresh ingredient (r : rs)
|
||||
| ingredient >= fst r && ingredient <= snd r = True
|
||||
| otherwise = isFresh ingredient rs
|
||||
|
||||
countIngredients :: [(Int, Int)] -> Int -> Int
|
||||
countIngredients [] tot = tot
|
||||
countIngredients [r] tot = snd r - fst r + 1 + tot
|
||||
countIngredients (r : s : rs) tot
|
||||
| snd r >= snd s = countIngredients (r : rs) tot
|
||||
| snd r >= fst s = countIngredients ((fst r, fst s - 1) : s : rs) tot
|
||||
| otherwise = countIngredients (s : rs) (snd r - fst r + 1 + tot)
|
||||
|
||||
parseInput :: IO [String]
|
||||
parseInput = do
|
||||
lines <$> readFile "input/day5.txt"
|
||||
@@ -32,3 +42,12 @@ day05_1 = do
|
||||
putStrLn $
|
||||
"Day 5, Puzzle 1 solution: "
|
||||
++ show (length freshIngredients)
|
||||
|
||||
day05_2 :: IO ()
|
||||
day05_2 = do
|
||||
input <- parseInput
|
||||
let ranges = sortOn fst . sortOn snd . map getRange $ takeWhile (/= "") input
|
||||
result = countIngredients ranges 0
|
||||
putStrLn $
|
||||
"Day 5, Puzzle 2 solution: "
|
||||
++ show result
|
||||
|
||||
53
src/Day06.hs
Normal file
53
src/Day06.hs
Normal file
@@ -0,0 +1,53 @@
|
||||
module Day06
|
||||
( day06_1,
|
||||
day06_2,
|
||||
)
|
||||
where
|
||||
|
||||
import Data.List (groupBy, transpose)
|
||||
|
||||
solveProblem :: [String] -> Int
|
||||
solveProblem problem =
|
||||
let op = last problem
|
||||
values = map read $ init problem
|
||||
in case op of
|
||||
"+" -> sum values
|
||||
"*" -> product values
|
||||
_ -> 0
|
||||
|
||||
solveProblem' :: [String] -> Int
|
||||
solveProblem' [] = 0
|
||||
solveProblem' (x : xs) =
|
||||
let op = last x
|
||||
v1 = read $ init x
|
||||
values = v1 : [read v | v <- xs]
|
||||
in case op of
|
||||
'+' -> sum values
|
||||
'*' -> product values
|
||||
_ -> 0
|
||||
|
||||
parseInput :: IO [[String]]
|
||||
parseInput = do
|
||||
transpose . map words . lines <$> readFile "input/day6.txt"
|
||||
|
||||
parseInput' :: IO [[String]]
|
||||
parseInput' = do
|
||||
input <- transpose . lines <$> readFile "input/day6.txt"
|
||||
let problems = map (filter (not . all (== ' '))) $ groupBy (\_ y -> not (all (== ' ') y)) input
|
||||
return problems
|
||||
|
||||
day06_1 :: IO ()
|
||||
day06_1 = do
|
||||
input <- parseInput
|
||||
let result = sum $ map solveProblem input
|
||||
putStrLn $
|
||||
"Day 6, Puzzle 1 solution: "
|
||||
++ show result
|
||||
|
||||
day06_2 :: IO ()
|
||||
day06_2 = do
|
||||
input <- parseInput'
|
||||
let result = sum $ map solveProblem' input
|
||||
putStrLn $
|
||||
"Day 6, Puzzle 2 solution: "
|
||||
++ show result
|
||||
13
src/Main.hs
13
src/Main.hs
@@ -4,7 +4,8 @@ import Day01 (day01_1, day01_2)
|
||||
import Day02 (day02_1, day02_2)
|
||||
import Day03 (day03_1, day03_2)
|
||||
import Day04 (day04_1, day04_2)
|
||||
import Day05 (day05_1)
|
||||
import Day05 (day05_1, day05_2)
|
||||
import Day06 (day06_1, day06_2)
|
||||
import System.Environment (getArgs)
|
||||
|
||||
main :: IO ()
|
||||
@@ -32,8 +33,15 @@ main = do
|
||||
day04_1
|
||||
day04_2
|
||||
"5" : "1" : _ -> day05_1
|
||||
"5" : "2" : _ -> day05_2
|
||||
"5" : _ -> do
|
||||
day05_1
|
||||
day05_2
|
||||
"6" : "1" : _ -> day06_1
|
||||
"6" : "2" : _ -> day06_2
|
||||
"6" : _ -> do
|
||||
day06_1
|
||||
day06_2
|
||||
"all" : _ -> do
|
||||
day01_1
|
||||
day01_2
|
||||
@@ -44,4 +52,7 @@ main = do
|
||||
day04_1
|
||||
day04_2
|
||||
day05_1
|
||||
day05_2
|
||||
day06_1
|
||||
day06_2
|
||||
_ -> error "Not implemented"
|
||||
|
||||
Reference in New Issue
Block a user