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

Commit da000e7

Browse files
committed
raw day 15
1 parent 02ddef2 commit da000e7

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

β€Žsrc/AOC/Challenge/Day15.hsβ€Ž

Lines changed: 36 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.Day15 (
25-
-- day15a
26-
-- , day15b
25+
day15a
26+
, day15b
2727
) where
2828

2929
import AOC.Prelude
@@ -44,17 +44,48 @@ import qualified Linear as L
4444
import qualified Text.Megaparsec as P
4545
import qualified Text.Megaparsec.Char as P
4646
import qualified Text.Megaparsec.Char.Lexer as PP
47+
import AOC.Common.Search
4748

4849
day15a :: _ :~> _
4950
day15a = MkSol
50-
{ sParse = Just . lines
51+
{ sParse = Just . parseAsciiMap digitToIntSafe
5152
, sShow = show
52-
, sSolve = Just
53+
, sSolve = \mp ->
54+
let targ = fst $ M.findMax mp
55+
in fst <$> aStar
56+
(mannDist targ)
57+
(M.fromSet (\x -> mp M.! x) . (`S.intersection` M.keysSet mp) . cardinalNeighbsSet)
58+
0
59+
(== targ)
5360
}
5461

62+
-- aStar
63+
-- :: forall n p. (Ord n, Ord p, Num p)
64+
-- => (n -> p) -- ^ heuristic
65+
-- -> (n -> Map n p) -- ^ neighborhood
66+
-- -> n -- ^ start
67+
-- -> (n -> Bool) -- ^ target
68+
-- -> Maybe (p, [n]) -- ^ the shortest path, if it exists, and its cost
69+
5570
day15b :: _ :~> _
5671
day15b = MkSol
5772
{ sParse = sParse day15a
5873
, sShow = show
59-
, sSolve = Just
74+
, sSolve = \mp0 ->
75+
let mp = M.fromList
76+
[ (k', v')
77+
| (k, v) <- M.toList mp0
78+
, dx <- [0,1,2,3,4]
79+
, dy <- [0,1,2,3,4]
80+
, let k' = k + V2 (shifter * dx) (shifter * dy)
81+
, let v' = ((v - 1 + dx+dy) `mod` 9) + 1
82+
]
83+
corner = fst $ M.findMax mp0
84+
V2 shifter _ = corner + 1
85+
targ = fst $ M.findMax mp
86+
in fst <$> aStar
87+
(mannDist targ)
88+
(M.fromSet (\x -> mp M.! x) . (`S.intersection` M.keysSet mp) . cardinalNeighbsSet)
89+
0
90+
(== targ)
6091
}

β€Žtest-data/2021/14b.txtβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
NNCB
2+
3+
CH -> B
4+
HH -> N
5+
CB -> H
6+
NH -> C
7+
HB -> C
8+
HC -> B
9+
HN -> C
10+
NN -> C
11+
BH -> H
12+
NC -> B
13+
NB -> B
14+
BN -> B
15+
BB -> N
16+
BC -> B
17+
CC -> N
18+
CN -> C
19+
>>> 2188189693529

β€Žtest-data/2021/15a.txtβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
1163751742
2+
1381373672
3+
2136511328
4+
3694931569
5+
7463417111
6+
1319128137
7+
1359912421
8+
3125421639
9+
1293138521
10+
2311944581
11+
>>> 40

β€Žtest-data/2021/15b.txtβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
1163751742
2+
1381373672
3+
2136511328
4+
3694931569
5+
7463417111
6+
1319128137
7+
1359912421
8+
3125421639
9+
1293138521
10+
2311944581
11+
>>> 315

0 commit comments

Comments
Β (0)