Skip to content

Commit acf2b3b

Browse files
committed
day10 part two WIP
1 parent 9c88c55 commit acf2b3b

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

aoc2024.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ source-repository head
2525

2626
library
2727
exposed-modules:
28+
Data.HashSet.Extra
2829
Data.List.Extra
2930
Data.Matrix
3031
Data.Point
@@ -59,6 +60,7 @@ test-suite aoc2024-test
5960
type: exitcode-stdio-1.0
6061
main-is: Spec.hs
6162
other-modules:
63+
Day10Spec
6264
Day1Spec
6365
Day2Spec
6466
Day3Spec

src/Data/HashSet/Extra.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Data.HashSet.Extra (unionMap) where
2+
3+
import Data.HashSet (HashSet)
4+
import qualified Data.HashSet as HashSet
5+
6+
unionMap :: (Eq b) => (a -> HashSet b) -> HashSet a -> HashSet b
7+
unionMap f set = HashSet.unions $ map f (HashSet.toList set)

src/Day10.hs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,29 @@ import qualified Data.Matrix as M
77
import Data.Point (Point)
88

99
partOne :: String -> Int
10-
partOne input = length $ filter (uncurry (isReachable matrix)) candidates
10+
partOne input = length $ filter ((> 0) . uncurry (rating matrix)) candidates
1111
where
1212
matrix = parseMatrix input
13-
trailHeads = M.points $ M.filter (== 0) matrix
14-
trailEnds = M.points $ M.filter (== 9) matrix
15-
candidates = concatMap (zip trailHeads . repeat) trailEnds
13+
candidates = possibleTrails matrix
1614

17-
isReachable :: Matrix Int -> Point -> Point -> Bool
18-
isReachable matrix x y = go (getCandidates 1 x) [y] 1
15+
partTwo :: String -> Int
16+
partTwo input = sum $ map (uncurry (rating matrix)) candidates
1917
where
20-
go :: [Point] -> [Point] -> Int -> Bool
21-
go _ _ 9 = False
18+
matrix = parseMatrix input
19+
candidates = possibleTrails matrix
20+
21+
rating :: Matrix Int -> Point -> Point -> Int
22+
rating matrix x y = go (getCandidates 1 x) [y] 1
23+
where
24+
go :: [Point] -> [Point] -> Int -> Int
25+
go _ _ 6 = 0
2226
go leftFront rightFront step =
2327
let leftFront' = concatMap (getCandidates (1 + step)) leftFront
2428
rightFront' = concatMap (getCandidates (9 - step)) rightFront
25-
in not (null (leftFront `intersect` rightFront)) || go leftFront' rightFront' (step + 1)
29+
intersection = intersect leftFront rightFront
30+
in if not (null intersection)
31+
then length intersection
32+
else go leftFront' rightFront' (step + 1)
2633

2734
getCandidates :: Int -> Point -> [Point]
2835
getCandidates value (row, col) =
@@ -36,8 +43,11 @@ isReachable matrix x y = go (getCandidates 1 x) [y] 1
3643
)
3744
$ zip positions values
3845

39-
partTwo :: String -> Int
40-
partTwo input = 0
46+
possibleTrails :: Matrix Int -> [(Point, Point)]
47+
possibleTrails matrix =
48+
let trailHeads = M.points $ M.filter (== 0) matrix
49+
trailEnds = M.points $ M.filter (== 9) matrix
50+
in concatMap (zip trailHeads . repeat) trailEnds
4151

4252
parseMatrix :: String -> Matrix Int
4353
parseMatrix = M.buildMatrix . map (map digitToInt) . lines

test/Day10Spec.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Day10Spec (spec) where
2+
3+
import Day10 (partOne, partTwo)
4+
import Test.Hspec
5+
6+
spec :: Spec
7+
spec = do
8+
describe "PartOne" $ do
9+
it "works" $ do
10+
partOne input `shouldBe` 36
11+
describe "PartTwo" $ do
12+
it "works" $ do
13+
partTwo input `shouldBe` 81
14+
where
15+
input =
16+
"89010123\n\
17+
\78121874\n\
18+
\87430965\n\
19+
\96549874\n\
20+
\45678903\n\
21+
\32019012\n\
22+
\01329801\n\
23+
\10456732\n"

0 commit comments

Comments
 (0)