Skip to content

Commit

Permalink
Day 06
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimitroulas committed Dec 6, 2023
1 parent 58dac8d commit 8f6f87e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions advent-of-code2023.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ library
Solutions.Day03
Solutions.Day04
Solutions.Day05
Solutions.Day06
other-modules:
Lib.AOC
Lib.List
Lib.Matrix
Lib.Parsing
Main
Solutions.DayX
Paths_advent_of_code2023
autogen-modules:
Paths_advent_of_code2023
Expand Down Expand Up @@ -83,6 +85,8 @@ executable advent-of-code2023
Solutions.Day03
Solutions.Day04
Solutions.Day05
Solutions.Day06
Solutions.DayX
Paths_advent_of_code2023
autogen-modules:
Paths_advent_of_code2023
Expand Down Expand Up @@ -132,6 +136,7 @@ test-suite unit-test
Day03
Day04
Day05
Day06
Paths_advent_of_code2023
autogen-modules:
Paths_advent_of_code2023
Expand Down
2 changes: 2 additions & 0 deletions data/day06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 46 68 98 66
Distance: 358 1054 1807 1080
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ library:
- Solutions.Day03
- Solutions.Day04
- Solutions.Day05
- Solutions.Day06

tests:
unit-test:
Expand Down
2 changes: 2 additions & 0 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Solutions.Day02 (day02)
import Solutions.Day03 (day03)
import Solutions.Day04 (day04)
import Solutions.Day05 (day05)
import Solutions.Day06 (day06)

main :: IO ()
main = do
Expand All @@ -12,3 +13,4 @@ main = do
day03
day04
day05
day06
28 changes: 28 additions & 0 deletions src/Solutions/Day06.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Solutions.Day06 (day06, parser, part1, part2) where

import Lib.AOC (runSolution)
import Data.Foldable (Foldable(foldl'))

type Input = [(Int, Int)]

parser :: String -> Input
parser = (\[l1,l2] -> zip l1 l2) . map (map read . drop 1 . words) . lines

generateHistogram :: Int -> [Int]
generateHistogram time = map (\x -> x * (time - x)) [0 .. time]

numOfWinningOptions :: (Int, Int) -> Int
numOfWinningOptions (time, recordDistance) = length $ filter (> recordDistance) (generateHistogram time)

part1 :: Input -> Int
part1 = product . map numOfWinningOptions

part2 :: Input -> Int
part2 input = numOfWinningOptions $ fixInput input
where
combineNumbers n1 n2 = read (show n1 ++ show n2)

fixInput = foldl' (\(totalT, totalD) (t, d) -> (combineNumbers totalT t, combineNumbers totalD d)) (0, 0)

day06 :: IO ()
day06 = runSolution "06" parser part1 part2
17 changes: 17 additions & 0 deletions src/Solutions/DayX.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Solutions.DayX (dayX, parser, part1, part2) where

import Lib.AOC (runSolution)

type Input = String

parser :: String -> Input
parser = id

part1 :: Input -> String
part1 = id

part2 :: Input -> String
part2 = id

dayX :: IO ()
dayX = runSolution "X" parser part1 part2
32 changes: 32 additions & 0 deletions test/Day06.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{-# OPTIONS_GHC -Wno-missing-export-lists #-}
module Day06 where

import Test.Tasty
import Test.Tasty.HUnit
import Solutions.Day06 (parser, part1, part2)

exampleInput :: String
exampleInput = "Time: 7 15 30\n\
\Distance: 9 40 200"

test_day06 :: TestTree
test_day06 = testGroup "Day06"
[
testCase "parser" $ do
parser exampleInput @?= [(7, 9), (15, 40), (30, 200)],

testCase "part 1 - example input" $ do
part1 (parser exampleInput) @?= 288,

testCase "part 1" $ do
input <- parser <$> readFile "data/day06.txt"
part1 input @?= 138915,

testCase "part 2 - example input" $ do
part2 (parser exampleInput) @?= 71503,

testCase "part 2" $ do
input <- parser <$> readFile "data/day06.txt"
part2 input @?= 27340847
]

0 comments on commit 8f6f87e

Please sign in to comment.