Solve 2024-02
This commit is contained in:
parent
3f5e878958
commit
656f4d82d8
3 changed files with 1054 additions and 4 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,56 @@
|
||||||
module AoC.Y2024.D02 (solve) where
|
module AoC.Y2024.D02 (solve, example) where
|
||||||
|
|
||||||
import AoC.Riddle
|
import AoC.Riddle
|
||||||
|
|
||||||
|
type Puzzle = [Report]
|
||||||
|
type Report = [Level]
|
||||||
|
type Level = Int
|
||||||
|
|
||||||
|
-- | example data
|
||||||
|
--
|
||||||
|
-- >>> part1 example
|
||||||
|
-- 2
|
||||||
|
-- >>> part2 example
|
||||||
|
-- 4
|
||||||
|
example :: Text
|
||||||
|
example =
|
||||||
|
unlines
|
||||||
|
[ "7 6 4 2 1"
|
||||||
|
, "1 2 7 8 9"
|
||||||
|
, "9 7 6 2 1"
|
||||||
|
, "1 3 2 4 5"
|
||||||
|
, "8 6 4 4 1"
|
||||||
|
, "1 3 6 7 9"
|
||||||
|
]
|
||||||
|
|
||||||
|
parse :: Riddle -> Puzzle
|
||||||
|
parse riddle = mapMaybe (readMaybe . toString) . words <$> lines riddle
|
||||||
|
|
||||||
|
isSafe :: Report -> Bool
|
||||||
|
isSafe report = check report || check (reverse report)
|
||||||
|
where
|
||||||
|
check :: Report -> Bool
|
||||||
|
check [] = True
|
||||||
|
check [_] = True
|
||||||
|
check (x0 : x1 : xs) = x1 > x0 && (x1 - x0 <= 3) && check (x1 : xs)
|
||||||
|
|
||||||
|
isSafeWithDampener :: Report -> Bool
|
||||||
|
isSafeWithDampener report = any isSafe reports
|
||||||
|
where
|
||||||
|
reports :: [Report]
|
||||||
|
reports = [reportWithoutPosition x | x <- [0 .. length report]]
|
||||||
|
|
||||||
|
reportWithoutPosition i =
|
||||||
|
let
|
||||||
|
(start, end) = splitAt i report
|
||||||
|
in
|
||||||
|
start ++ drop 1 end
|
||||||
|
|
||||||
|
part1 :: Riddle -> Int
|
||||||
|
part1 riddle = length $ filter isSafe $ parse riddle
|
||||||
|
|
||||||
|
part2 :: Riddle -> Int
|
||||||
|
part2 riddle = length $ filter isSafeWithDampener $ parse riddle
|
||||||
|
|
||||||
solve :: (MonadIO m) => Text -> m (Either Text Solution)
|
solve :: (MonadIO m) => Text -> m (Either Text Solution)
|
||||||
solve _ = pure $ Left "not yet implemented"
|
solve riddle = pure $ Right [fromIntegral $ part1 riddle, fromIntegral $ part2 riddle]
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,11 @@ spec = do
|
||||||
it "calculates correctly" do
|
it "calculates correctly" do
|
||||||
runAoC Y2024 D01 `shouldReturn` Right [2580760, 25358365]
|
runAoC Y2024 D01 `shouldReturn` Right [2580760, 25358365]
|
||||||
|
|
||||||
{-
|
|
||||||
describe "Day 2" do
|
describe "Day 2" do
|
||||||
it "calculates correctly" do
|
it "calculates correctly" do
|
||||||
runAoC Y2024 D02 `shouldReturn` Right [2369, 66363]
|
runAoC Y2024 D02 `shouldReturn` Right [332, 398]
|
||||||
|
|
||||||
|
{-
|
||||||
describe "Day 3" do
|
describe "Day 3" do
|
||||||
it "calculates correctly" do
|
it "calculates correctly" do
|
||||||
runAoC Y2023 D03 `shouldReturn` Right [528799, 84907174]
|
runAoC Y2023 D03 `shouldReturn` Right [528799, 84907174]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue