-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58dac8d
commit 8f6f87e
Showing
7 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time: 46 68 98 66 | ||
Distance: 358 1054 1807 1080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ library: | |
- Solutions.Day03 | ||
- Solutions.Day04 | ||
- Solutions.Day05 | ||
- Solutions.Day06 | ||
|
||
tests: | ||
unit-test: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
|