Solve 2024-01
This commit is contained in:
parent
d5d6949a95
commit
3f5e878958
6 changed files with 1070 additions and 2 deletions
|
|
@ -163,6 +163,7 @@ test-suite spec
|
||||||
other-modules:
|
other-modules:
|
||||||
AoCSpec.UtilSpec
|
AoCSpec.UtilSpec
|
||||||
AoCSpec.Y2023Spec
|
AoCSpec.Y2023Spec
|
||||||
|
AoCSpec.Y2024Spec
|
||||||
SpecHook
|
SpecHook
|
||||||
Paths_aoc
|
Paths_aoc
|
||||||
autogen-modules:
|
autogen-modules:
|
||||||
|
|
|
||||||
6
data/Y2024/D01/example
Normal file
6
data/Y2024/D01/example
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -41,4 +41,4 @@ printResult (Left e) = putTextLn e
|
||||||
printResult (Right s) = print s
|
printResult (Right s) = print s
|
||||||
|
|
||||||
defaultMain :: IO ()
|
defaultMain :: IO ()
|
||||||
defaultMain = Exception.catch (runAoC Y2023 D05 >>= printResult) handleError
|
defaultMain = Exception.catch (runAoC Y2024 D01 >>= printResult) handleError
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,39 @@
|
||||||
module AoC.Y2024.D01 (solve) where
|
module AoC.Y2024.D01 (solve) where
|
||||||
|
|
||||||
import AoC.Riddle
|
import AoC.Riddle
|
||||||
|
import Data.IntMap qualified as M
|
||||||
|
|
||||||
|
parse :: Text -> ([Int], [Int])
|
||||||
|
parse riddle =
|
||||||
|
let
|
||||||
|
lists = transpose $ mapMaybe (readMaybe . toString) . words <$> lines riddle
|
||||||
|
in
|
||||||
|
case lists of
|
||||||
|
[as, bs] -> (as, bs)
|
||||||
|
_ -> ([], [])
|
||||||
|
|
||||||
|
part1 :: Riddle -> Int
|
||||||
|
part1 riddle =
|
||||||
|
let
|
||||||
|
(as, bs) = parse riddle
|
||||||
|
as' = sort as
|
||||||
|
bs' = sort bs
|
||||||
|
distances = zipWith (\a b -> if a > b then a - b else b - a) as' bs'
|
||||||
|
in
|
||||||
|
sum distances
|
||||||
|
|
||||||
|
mkOccurrences :: [Int] -> IntMap Int
|
||||||
|
mkOccurrences = M.unionsWith (+) . fmap (\i -> one (i, 1))
|
||||||
|
|
||||||
|
part2 :: Riddle -> Int
|
||||||
|
part2 riddle =
|
||||||
|
let
|
||||||
|
(as, bs) = parse riddle
|
||||||
|
occurrences = mkOccurrences bs
|
||||||
|
step a total = total + a * fromMaybe 0 (M.lookup a occurrences)
|
||||||
|
in
|
||||||
|
foldr step 0 as
|
||||||
|
|
||||||
solve :: (MonadIO m) => Text -> m (Either Text Solution)
|
solve :: (MonadIO m) => Text -> m (Either Text Solution)
|
||||||
solve _ = pure $ Left "not yet implemented"
|
solve riddle = do
|
||||||
|
pure $ Right [fromIntegral $ part1 riddle, fromIntegral $ part2 riddle]
|
||||||
|
|
|
||||||
28
test/spec/AoCSpec/Y2024Spec.hs
Normal file
28
test/spec/AoCSpec/Y2024Spec.hs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
module AoCSpec.Y2024Spec (spec) where
|
||||||
|
|
||||||
|
import AoC
|
||||||
|
import Test.Hspec
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = do
|
||||||
|
describe "Day 1" do
|
||||||
|
it "calculates correctly" do
|
||||||
|
runAoC Y2024 D01 `shouldReturn` Right [2580760, 25358365]
|
||||||
|
|
||||||
|
{-
|
||||||
|
describe "Day 2" do
|
||||||
|
it "calculates correctly" do
|
||||||
|
runAoC Y2024 D02 `shouldReturn` Right [2369, 66363]
|
||||||
|
|
||||||
|
describe "Day 3" do
|
||||||
|
it "calculates correctly" do
|
||||||
|
runAoC Y2023 D03 `shouldReturn` Right [528799, 84907174]
|
||||||
|
|
||||||
|
describe "Day 4" do
|
||||||
|
it "calculates correctly" do
|
||||||
|
runAoC Y2023 D04 `shouldReturn` Right [24542, 8736438]
|
||||||
|
|
||||||
|
describe "Day 5" do
|
||||||
|
it "calculates the example correctly" do
|
||||||
|
runAoCExample Y2023 D05 `shouldReturn` Right [35]
|
||||||
|
-}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue