Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 18: RAM Run #119

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Development occurs in language-specific directories:
|[Day15.hs](hs/src/Day15.hs)|[Day15.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day15.kt)|[day15.py](py/aoc2024/day15.py)|[day15.rs](rs/src/day15.rs)|
|[Day16.hs](hs/src/Day16.hs)|[Day16.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day16.kt)|[day16.py](py/aoc2024/day16.py)|[day16.rs](rs/src/day16.rs)|
|[Day17.hs](hs/src/Day17.hs)|[Day17.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day17.kt)|[day17.py](py/aoc2024/day17.py)|[day17.rs](rs/src/day17.rs)|
|[Day18.hs](hs/src/Day18.hs)||||
2 changes: 2 additions & 0 deletions hs/aoc2024.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ library
Day15
Day16
Day17
Day18
Day2
Day3
Day4
Expand Down Expand Up @@ -87,6 +88,7 @@ test-suite aoc2024-test
Day15Spec
Day16Spec
Day17Spec
Day18Spec
Day1Spec
Day2Spec
Day3Spec
Expand Down
2 changes: 2 additions & 0 deletions hs/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Day14 qualified (part1, part2)
import Day15 qualified (part1, part2)
import Day16 qualified (part1, part2)
import Day17 qualified (part1, part2)
import Day18 qualified (part1, part2)
import Day2 qualified (part1, part2)
import Day3 qualified (part1, part2)
import Day4 qualified (part1, part2)
Expand Down Expand Up @@ -65,3 +66,4 @@ main = do
run 15 (either fail print) [Day15.part1, Day15.part2]
run 16 (maybe (fail "error") print) [Day16.part1, Day16.part2]
run 17 (either (fail . errorBundlePretty) $ putStrLn . intercalate "," . map show) [Day17.part1, fmap (: []) . Day17.part2]
run 18 (either fail putStrLn) [fmap show . Day18.part1, fmap (uncurry $ (. (',' :) . show) . shows) . Day18.part2]
7 changes: 7 additions & 0 deletions hs/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Day14 qualified (part1, part2)
import Day15 qualified (part1, part2)
import Day16 qualified (part1, part2)
import Day17 qualified (part1, part2)
import Day18 qualified (part1, part2)
import Day2 qualified (part1, part2)
import Day3 qualified (part1, part2)
import Day4 qualified (part1, part2)
Expand Down Expand Up @@ -141,5 +142,11 @@ main =
"Day 17"
[ bench "part 1" $ nf Day17.part1 input,
bench "part 2" $ nf Day17.part2 input
],
env (getDayInput 18) $ \input ->
bgroup
"Day 18"
[ bench "part 1" $ nf Day18.part1 input,
bench "part 2" $ nf Day18.part2 input
]
]
66 changes: 66 additions & 0 deletions hs/src/Day18.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module: Day18
-- Description: <https://adventofcode.com/2024/day/18 Day 18: RAM Run>
module Day18 (part1, part1', part2, part2') where

import Common (readEntire)
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.List.NonEmpty qualified as NonEmpty (cons, toList)
import Data.Set (Set)
import Data.Set qualified as Set (empty, fromList, insert, member, notMember)
import Data.Text (Text)
import Data.Text qualified as T (lines, stripPrefix)
import Data.Text.Read (Reader)
import Data.Text.Read qualified as T (decimal)

coord :: (Integral a) => Reader (a, a)
coord input = do
(x, input1) <- T.decimal input
input2 <- maybe (Left "missing comma") Right $ T.stripPrefix "," input1
(y, input3) <- T.decimal input2
pure ((x, y), input3)

part1 :: Text -> Either String Int
part1 = part1' 70 1024

part1' :: Int -> Int -> Text -> Either String Int
part1' size n input = do
coords <- mapM (readEntire coord) . take n $ T.lines input
case go size $ Set.fromList coords of
Just path -> Right $ length path - 1
Nothing -> Left "no solution"

go :: Int -> Set (Int, Int) -> Maybe (NonEmpty (Int, Int))
go size visited = go' visited [(0, 0) :| []] []
where
go' visited' (path@(pos@(x, y) :| _) : queue1) queue2
| pos `Set.member` visited' = go' visited' queue1 queue2
| pos == (size, size) = Just path
| otherwise =
go' (Set.insert pos visited') queue1 $
[ NonEmpty.cons pos' path
| pos'@(x', y') <- [(x - 1, y), (x, y - 1), (x, y + 1), (x + 1, y)],
0 <= x' && x' <= size && 0 <= y' && y' <= size
]
++ queue2
go' _ _ [] = Nothing
go' visited' [] queue2 = go' visited' (reverse queue2) []

part2 :: Text -> Either String (Int, Int)
part2 = part2' 70

part2' :: Int -> Text -> Either String (Int, Int)
part2' size input = mapM (readEntire coord) (T.lines input) >>= go' Set.empty
where
go' visited (candidate : rest) =
case go size visited' of
Just path ->
let path' = Set.fromList $ NonEmpty.toList path
(skip, rest') = span (`Set.notMember` path') rest
in go' (visited' <> Set.fromList skip) rest'
Nothing -> Right candidate
where
visited' = Set.insert candidate visited
go' _ _ = Left "no solution"
47 changes: 47 additions & 0 deletions hs/test/Day18Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE OverloadedStrings #-}

module Day18Spec (spec) where

import Data.Text (Text)
import Data.Text qualified as T (unlines)
import Day18 (part1', part2')
import Test.Hspec (Spec, describe, it, shouldBe)

example :: Text
example =
T.unlines
[ "5,4",
"4,2",
"4,5",
"3,0",
"2,1",
"6,3",
"2,4",
"1,5",
"0,6",
"3,3",
"2,6",
"5,1",
"1,2",
"5,5",
"2,5",
"6,5",
"1,4",
"0,4",
"6,4",
"1,1",
"6,1",
"1,0",
"0,5",
"1,6",
"2,0"
]

spec :: Spec
spec = do
describe "part 1" $ do
it "examples" $ do
part1' 6 12 example `shouldBe` Right 22
describe "part 2" $ do
it "examples" $ do
part2' 6 example `shouldBe` Right (6, 1)
Loading