Skip to content
This repository was archived by the owner on Nov 17, 2024. It is now read-only.

Commit ae2f35e

Browse files
committed
raw solve
1 parent 1b1a145 commit ae2f35e

File tree

3 files changed

+121
-5
lines changed

3 files changed

+121
-5
lines changed

src/AOC/Challenge/Day12.hs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
-- will recommend what should go in place of the underscores.
2323

2424
module AOC.Challenge.Day12 (
25-
-- day12a
26-
-- , day12b
25+
day12a
26+
, day12b
2727
) where
2828

2929
import AOC.Prelude
@@ -47,14 +47,54 @@ import qualified Text.Megaparsec.Char.Lexer as PP
4747

4848
day12a :: _ :~> _
4949
day12a = MkSol
50-
{ sParse = Just . lines
50+
{ sParse = mapMaybeLinesJust $ \xs -> case splitOn "-" xs of
51+
[a,b] -> Just (a, b)
5152
, sShow = show
52-
, sSolve = Just
53+
, sSolve = \xs -> Just
54+
let allPaths = M.fromListWith (++) $ concatMap (\(a,b) -> [(a,[b]),(b,[a])]) xs
55+
in S.size . S.fromList $ findPaths allPaths
5356
}
5457

58+
findPaths :: Map String [String] -> [[String]]
59+
findPaths mp = go S.empty "start"
60+
where
61+
go seen currPos
62+
| currPos == "end" = [["end"]]
63+
| otherwise = do
64+
nextBranch <- mp M.! currPos
65+
guard $ nextBranch /= "start"
66+
guard $ if isSmall nextBranch
67+
then nextBranch `S.notMember` seen
68+
else True
69+
(currPos:) <$> go (S.insert nextBranch seen) nextBranch
70+
71+
72+
isSmall = all isLower
73+
74+
75+
5576
day12b :: _ :~> _
5677
day12b = MkSol
5778
{ sParse = sParse day12a
5879
, sShow = show
59-
, sSolve = Just
80+
, sSolve = \xs -> Just
81+
let allPaths = M.fromListWith (++) $ concatMap (\(a,b) -> [(a,[b]),(b,[a])]) xs
82+
in S.size . S.fromList $ findPaths2 allPaths
6083
}
84+
85+
findPaths2 :: Map String [String] -> [[String]]
86+
findPaths2 mp = go S.empty Nothing "start"
87+
where
88+
go seen seenTwice currPos
89+
| currPos == "end" = [["end"]]
90+
| otherwise = do
91+
nextBranch <- mp M.! currPos
92+
guard $ nextBranch /= "start"
93+
newSeenTwice <- if isSmall nextBranch
94+
then if nextBranch `S.member` seen
95+
then case seenTwice of
96+
Nothing -> [Just nextBranch]
97+
Just _ -> []
98+
else [seenTwice]
99+
else [seenTwice]
100+
(currPos:) <$> go (S.insert nextBranch seen) newSeenTwice nextBranch

test-data/2021/12a.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
start-A
2+
start-b
3+
A-c
4+
A-b
5+
b-d
6+
A-end
7+
b-end
8+
>>> 10
9+
dc-end
10+
HN-start
11+
start-kj
12+
dc-start
13+
dc-HN
14+
LN-dc
15+
HN-end
16+
kj-sa
17+
kj-HN
18+
kj-dc
19+
>>> 19
20+
fs-end
21+
he-DX
22+
fs-he
23+
start-DX
24+
pj-DX
25+
end-zg
26+
zg-sl
27+
zg-pj
28+
pj-he
29+
RW-he
30+
fs-DX
31+
pj-RW
32+
zg-RW
33+
start-pj
34+
he-WI
35+
zg-he
36+
pj-fs
37+
start-RW
38+
>>> 226

test-data/2021/12b.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
start-A
2+
start-b
3+
A-c
4+
A-b
5+
b-d
6+
A-end
7+
b-end
8+
>>> 36
9+
dc-end
10+
HN-start
11+
start-kj
12+
dc-start
13+
dc-HN
14+
LN-dc
15+
HN-end
16+
kj-sa
17+
kj-HN
18+
kj-dc
19+
>>> 103
20+
fs-end
21+
he-DX
22+
fs-he
23+
start-DX
24+
pj-DX
25+
end-zg
26+
zg-sl
27+
zg-pj
28+
pj-he
29+
RW-he
30+
fs-DX
31+
pj-RW
32+
zg-RW
33+
start-pj
34+
he-WI
35+
zg-he
36+
pj-fs
37+
start-RW
38+
>>> 3509

0 commit comments

Comments
 (0)