Browse Source

Day 12

main
Jens Kadenbach 2 years ago
parent
commit
26e6851eec
6 changed files with 211 additions and 1 deletions
  1. 8
    0
      aoc2022.cabal
  2. 2
    0
      package.yaml
  3. 82
    0
      ressources/day12-input
  4. 87
    0
      src/Day12.hs
  5. 2
    1
      src/Lib.hs
  6. 30
    0
      test/Day12Spec.hs

+ 8
- 0
aoc2022.cabal View File

30
       Day10
30
       Day10
31
       Day11
31
       Day11
32
       Day11.Parser
32
       Day11.Parser
33
+      Day12
33
       Day2
34
       Day2
34
       Day2.Part1
35
       Day2.Part1
35
       Day2.Part2
36
       Day2.Part2
67
     , hspec
68
     , hspec
68
     , hspec-megaparsec
69
     , hspec-megaparsec
69
     , lens
70
     , lens
71
+    , linear
70
     , matrix
72
     , matrix
71
     , megaparsec
73
     , megaparsec
72
     , mtl
74
     , mtl
73
     , parsec
75
     , parsec
76
+    , search-algorithms
74
     , sort
77
     , sort
75
     , split
78
     , split
76
     , text
79
     , text
94
     , hspec
97
     , hspec
95
     , hspec-megaparsec
98
     , hspec-megaparsec
96
     , lens
99
     , lens
100
+    , linear
97
     , matrix
101
     , matrix
98
     , megaparsec
102
     , megaparsec
99
     , mtl
103
     , mtl
100
     , parsec
104
     , parsec
105
+    , search-algorithms
101
     , sort
106
     , sort
102
     , split
107
     , split
103
     , text
108
     , text
111
   other-modules:
116
   other-modules:
112
       Day10Spec
117
       Day10Spec
113
       Day11Spec
118
       Day11Spec
119
+      Day12Spec
114
       Day1Spec
120
       Day1Spec
115
       Day2Spec
121
       Day2Spec
116
       Day3Spec
122
       Day3Spec
133
     , hspec
139
     , hspec
134
     , hspec-megaparsec
140
     , hspec-megaparsec
135
     , lens
141
     , lens
142
+    , linear
136
     , matrix
143
     , matrix
137
     , megaparsec
144
     , megaparsec
138
     , mtl
145
     , mtl
139
     , parsec
146
     , parsec
147
+    , search-algorithms
140
     , sort
148
     , sort
141
     , split
149
     , split
142
     , text
150
     , text

+ 2
- 0
package.yaml View File

37
 - hspec-megaparsec
37
 - hspec-megaparsec
38
 - mtl
38
 - mtl
39
 - sort
39
 - sort
40
+- search-algorithms
41
+- linear
40
 
42
 
41
 ghc-options:
43
 ghc-options:
42
 - -Wall
44
 - -Wall

+ 82
- 0
ressources/day12-input View File

1
+abaaacccccccccaaaaaaccccccccccccccccaacccccccccccaacaaaaaaaaaaaaaaaaaccaaaaacccaaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaa
2
+abaaacccccccccaaaaaacccccccccccccccaaaaccccccccccaaaaaaaacaaaaaaaaaaaccaaaaaaccaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa
3
+abaaaccccccccccaaaaacccccccccccccccaaaacccccccccccaaaaacccaaaaaaaaaacccaaaaaacccaaccccccccccccccaaaaacccccccccccccccccccccccccccccccccccccaaaaaa
4
+abccccaaccccccaaaaacccccccccaaaaaccaaaaccccccccccccaaaaacaaaaaaaaacccccaaaaaccccccccccccccccccccaaaaacccccccccccccccccaaaccccaaaccccccccccaaacaa
5
+abcccaaaacccccaaaaacccccccccaaaaacccccccccccccccccaaacaaaaaaaaaacccccccaaaaacccccccccccccccccccaaaaaacccccccccccccccccaaaaccaaaaccccccccccccccaa
6
+abcccaaaaacacccccccccccccccaaaaaaccccccccccccccccccaaccaaaaacaaaaccccccccccccccccccccccccccccccaaaaaaccccccccccccccccccaaaaaaaacccccccccccccccaa
7
+abaaaaaaaaaacccccccccccccccaaaaaaccccccccccccccccccccccaaaacccaaaccccccccccccccccccccccccccccccaaaaaacccccccccccccccciiiiijaaaaccccccccccccccccc
8
+abaaaaaaaaaacccccccccccccccaaaaaacccccccccccccccccccccccccccccaaacccccccccccccccccccccccccccccccaaaccccccccccccccccciiiiiijjjaccccccccaaaccccccc
9
+abccaaaaaaccccccccccccccccccaaaccccccccccccccccccccccccccccccccacccccccccccaacccccccccccccccccccccccccccccccccccccciiiiioijjjjaaccccccaaaaaacccc
10
+abccaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaacccccccccccccccccccccccccccccccccccciiinnooojjjjjaaccaaaaaaaacccc
11
+abccaaaaaacccccccccccccccccccccccccccccccccccccaacccccaacccccccccccccccccaaaaaacccccccccccccccccccccccccccaaaccccciiinnnoooojjjjjjkkkaaaaaaacccc
12
+abcaaaaaaaaccccccccccccccccccccccccccccccccccccaaaccaaaaaaccccaaacccccccccaaaacccccccccccccccccccccccccccccaaaaccciiinnnouooojjjjkkkkkaaaaaccccc
13
+abccaccccccccccccccccccaaccccccaccccccccccccaaaaaaaaaaaaaacccaaaacccccccccaaaacccccccccccccccccccccccccccaaaaaacchhinnnttuuooooookkkkkkkaaaccccc
14
+abccccccccccccccccccaacaaaccccaaaaaaaaccccccaaaaaaaacaaaaacccaaaacccccccccaccacccccccccccccccccccccccccccaaaaacchhhhnntttuuuooooppppkkkkcaaacccc
15
+abccccccccaaacccccccaaaaaccccccaaaaaaccccccccaaaaaacaaaaaccccaaaaccccccccccccccccccccccccccccaccccccccccccaaaaahhhhnnntttxuuuooppppppkkkcccccccc
16
+abccccccccaaaacccccccaaaaaaccccaaaaaaccaaacccaaaaaacaaaaaccccccccccccccaaccccccccccccccaaaaaaaacccccccccccaachhhhhnnnntttxxuuuuuuuupppkkkccccccc
17
+abccccccccaaaacccccaaaaaaaacccaaaaaaaacaaacacaaaaaaccccccccccccccccccccaacccccccccccccccaaaaaacccccccccccccchhhhmnnnntttxxxxuuuuuuupppkkcccccccc
18
+abacccccccaaaacccccaaaaacaaccaaaaaaaaaaaaaaaaaaccaacccccccccccccccccaaaaaaaaccccccccccccaaaaaaccccccccccccchhhhmmmntttttxxxxuyyyuvvpppklcccccccc
19
+abacccccccccccccccccacaaaccaaaaaaaaaaaaaaaaaaaccccccccccccccccccccccaaaaaaaacccccccccccaaaaaaaaccccccccccccgghmmmtttttxxxxxxyyyyvvvpplllcccccccc
20
+abaccccccccaacccccccccaaaccaaaaaaaacaaaaaaaaaaccccccccccccccccccccccccaaaaccccccccccccaaaaaaaaaaccccccaccccgggmmmtttxxxxxxxyyyyyvvppplllcccccccc
21
+SbaaaccccccaaacaaaccccccccaaaaaaaaacaaaaaaaaacccccccccccccccccccccccccaaaaacccccccccccaaaaaaaaaaaaacaaaccaagggmmmtttxxxEzzzzyyyvvppplllccccccccc
22
+abaacccccccaaaaaaacccccccaaaaaaacaaccaaaaaaaccccccccccccccaaaccccccccaaaaaacccccccccccacacaaacccaaaaaaacaaagggmmmsssxxxxxyyyyyvvvqqqlllccccccccc
23
+abaccccccccaaaaaaccacccaaaaaaaaacccccccaaaaaaccccccccccccaaaaccccccccaaccaacccccccccccccccaaaccccaaaaaaccaagggmmmssssxxwwyyyyyyvvqqqlllccccccccc
24
+abaccccccaaaaaaaaccaacaaaccaaaaaacccccaaaaaaaccccccccccccaaaaccccccccccaacccccccccccccccccaacccccaaaaaaaaaaggggmmmssssswwyywyyyyvvqqlllccccccccc
25
+abaccccccaaaaaaaaacaaaaacccaaaaaacccccaaacaaaccccccccccccaaaaccccccccaaaaaaccccccccccccaacccccccaaaaaaaaaaaaggggmmmossswwyywwyyvvvqqqllccccccccc
26
+abcccccccaaaaaaaaaacaaaaaacaaccccccccaaacccccccccccccccccccccccccccccaaaaaaccccccccccccaaaaacccaaaaaaaaaaaaaaggggoooosswwywwwwvvvvqqqmlccccccccc
27
+abccccccccccaaacaaaaaaaaaacccccccccccaaacaccccccccccccccccccccccccccccaaaaccccccccccccaaaaaccccaaacaaacccaaacagggfooosswwwwwrvvvvqqqqmmccccccccc
28
+abccccccccccaaacccaaaaaaaacccccccccaacaaaaacccccccccccccccccccccccccccaaaaccccccccccccaaaaaacccccccaaacccaaccccfffooosswwwwrrrrrqqqqqmmccccccccc
29
+abccccccccccaacccccccaaccccccccccccaaaaaaaacccccccccccccaaccccccccccccaccaccccccccccccccaaaacccccccaacccccccccccfffoossrwrrrrrrrqqqqmmmccccccccc
30
+abccaaaccccccccccccccaacccccccccccccaaaaaccccccccccccaacaacccccccaaaaacccccccccccccccccaacccccccccccccccccccccccfffoossrrrrrnnnmqqmmmmmccccccccc
31
+abcaaaaccccccccccccccccccccccccccccccaaaaacccccccccccaaaaacccccccaaaaacccaaaccccccccccccccccccccccccccccccccccccfffooorrrrrnnnnmmmmmmmccccaacccc
32
+abcaaaacccccccccccccccccccccccccccccaaacaaccccacccccccaaaaaaccccaaaaaaccccaaaccacccccccccccccccccccccccccccccccccffoooonnnnnnnnmmmmmmccccaaacccc
33
+abccaaacccccccccccccccccccccaaaaaccccaaccccaaaacccccaaaaaaaaccccaaaaaaccccaaaaaaaccccccccccccccccaccaccccccccccccfffooonnnnnnddddddddcccaaaccccc
34
+abccccccccccccccccccccccccccaaaaaccccccccccaaaaaacccaaaaacaacccaaaaaaaccaaaaaaaacccccccccccccccccaaaaccccccccccccfffeonnnnneddddddddddcaaacccccc
35
+abccccccccccaaaccccccccccccaaaaaacccccccccccaaaacccccacaaacccccaacaacccaaaaaaaaacccccccccccccccccaaaacccccccccccccffeeeeeeeeddddddddcccaaacccccc
36
+abcccccccccaaaaccccacccccccaaaaaaccccccccccaaaaacccccccaaaacccaaacaccccaaaaaaaaaccccccccccccccccaaaaaaccccccccccccceeeeeeeeedacccccccccccccccccc
37
+abaccccccccaaaaccccaaacaaacaaaaaaccccccccccaacaaccccccccaaaacaaaacaaacaaaaaaaaaacccccccccccccaacaaaaaacccccccccccccceeeeeeeaaacccccccccccccccaaa
38
+abaaacccccccaaaccccaaaaaaaccaaaccccccccaaacccccccccccccccaaaaaaaacaaaaaaaaaaaaaaacacaaccaaacaaacccaacccccccccccccccccaacccaaaacccccccccccccccaaa
39
+abaaaccccccccccccccaaaaaaccccccccccccccaaacccccccccccccccaaaaaaaccaaaaaaccaacccaccaaaaccaaaaaaaccccccccaaccccccccccccccccccaaacccccccccccccccaaa
40
+abaaccccccccccccccaaaaaaacccccccccccaaaaaaaaccccccccccccccaaaaaaaaaaaaaacaaaccccccaaaaaccaaaaaaccccccaaaaccccccccccccccccccaaaccccccccccccaaaaaa
41
+abaaaccccccccccccaaaaaaaaaacccccccccaaaaaaaacccccccccccaaaaaaaaaaaaaaaaaaacccccccaaaaaacaaaaaaaaaccccaaaaaacccccccccccccccccccccccccccccccaaaaaa
42
+abaaacccccccccaaaaaaccccccccccccccccaacccccccccccaacaaaaaaaaaaaaaaaaaccaaaaacccaaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaa
43
+abaaacccccccccaaaaaacccccccccccccccaaaaccccccccccaaaaaaaacaaaaaaaaaaaccaaaaaaccaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa
44
+abaaaccccccccccaaaaacccccccccccccccaaaacccccccccccaaaaacccaaaaaaaaaacccaaaaaacccaaccccccccccccccaaaaacccccccccccccccccccccccccccccccccccccaaaaaa
45
+abccccaaccccccaaaaacccccccccaaaaaccaaaaccccccccccccaaaaacaaaaaaaaacccccaaaaaccccccccccccccccccccaaaaacccccccccccccccccaaaccccaaaccccccccccaaacaa
46
+abcccaaaacccccaaaaacccccccccaaaaacccccccccccccccccaaacaaaaaaaaaacccccccaaaaacccccccccccccccccccaaaaaacccccccccccccccccaaaaccaaaaccccccccccccccaa
47
+abcccaaaaacacccccccccccccccaaaaaaccccccccccccccccccaaccaaaaacaaaaccccccccccccccccccccccccccccccaaaaaaccccccccccccccccccaaaaaaaacccccccccccccccaa
48
+abaaaaaaaaaacccccccccccccccaaaaaaccccccccccccccccccccccaaaacccaaaccccccccccccccccccccccccccccccaaaaaacccccccccccccccciiiiijaaaaccccccccccccccccc
49
+abaaaaaaaaaacccccccccccccccaaaaaacccccccccccccccccccccccccccccaaacccccccccccccccccccccccccccccccaaaccccccccccccccccciiiiiijjjaccccccccaaaccccccc
50
+abccaaaaaaccccccccccccccccccaaaccccccccccccccccccccccccccccccccacccccccccccaacccccccccccccccccccccccccccccccccccccciiiiioijjjjaaccccccaaaaaacccc
51
+abccaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaacccccccccccccccccccccccccccccccccccciiinnooojjjjjaaccaaaaaaaacccc
52
+abccaaaaaacccccccccccccccccccccccccccccccccccccaacccccaacccccccccccccccccaaaaaacccccccccccccccccccccccccccaaaccccciiinnnoooojjjjjjkkkaaaaaaacccc
53
+abcaaaaaaaaccccccccccccccccccccccccccccccccccccaaaccaaaaaaccccaaacccccccccaaaacccccccccccccccccccccccccccccaaaaccciiinnnouooojjjjkkkkkaaaaaccccc
54
+abccaccccccccccccccccccaaccccccaccccccccccccaaaaaaaaaaaaaacccaaaacccccccccaaaacccccccccccccccccccccccccccaaaaaacchhinnnttuuooooookkkkkkkaaaccccc
55
+abccccccccccccccccccaacaaaccccaaaaaaaaccccccaaaaaaaacaaaaacccaaaacccccccccaccacccccccccccccccccccccccccccaaaaacchhhhnntttuuuooooppppkkkkcaaacccc
56
+abccccccccaaacccccccaaaaaccccccaaaaaaccccccccaaaaaacaaaaaccccaaaaccccccccccccccccccccccccccccaccccccccccccaaaaahhhhnnntttxuuuooppppppkkkcccccccc
57
+abccccccccaaaacccccccaaaaaaccccaaaaaaccaaacccaaaaaacaaaaaccccccccccccccaaccccccccccccccaaaaaaaacccccccccccaachhhhhnnnntttxxuuuuuuuupppkkkccccccc
58
+abccccccccaaaacccccaaaaaaaacccaaaaaaaacaaacacaaaaaaccccccccccccccccccccaacccccccccccccccaaaaaacccccccccccccchhhhmnnnntttxxxxuuuuuuupppkkcccccccc
59
+abacccccccaaaacccccaaaaacaaccaaaaaaaaaaaaaaaaaaccaacccccccccccccccccaaaaaaaaccccccccccccaaaaaaccccccccccccchhhhmmmntttttxxxxuyyyuvvpppklcccccccc
60
+abacccccccccccccccccacaaaccaaaaaaaaaaaaaaaaaaaccccccccccccccccccccccaaaaaaaacccccccccccaaaaaaaaccccccccccccgghmmmtttttxxxxxxyyyyvvvpplllcccccccc
61
+abaccccccccaacccccccccaaaccaaaaaaaacaaaaaaaaaaccccccccccccccccccccccccaaaaccccccccccccaaaaaaaaaaccccccaccccgggmmmtttxxxxxxxyyyyyvvppplllcccccccc
62
+SbaaaccccccaaacaaaccccccccaaaaaaaaacaaaaaaaaacccccccccccccccccccccccccaaaaacccccccccccaaaaaaaaaaaaacaaaccaagggmmmtttxxxEzzzzyyyvvppplllccccccccc
63
+abaacccccccaaaaaaacccccccaaaaaaacaaccaaaaaaaccccccccccccccaaaccccccccaaaaaacccccccccccacacaaacccaaaaaaacaaagggmmmsssxxxxxyyyyyvvvqqqlllccccccccc
64
+abaccccccccaaaaaaccacccaaaaaaaaacccccccaaaaaaccccccccccccaaaaccccccccaaccaacccccccccccccccaaaccccaaaaaaccaagggmmmssssxxwwyyyyyyvvqqqlllccccccccc
65
+abaccccccaaaaaaaaccaacaaaccaaaaaacccccaaaaaaaccccccccccccaaaaccccccccccaacccccccccccccccccaacccccaaaaaaaaaaggggmmmssssswwyywyyyyvvqqlllccccccccc
66
+abaccccccaaaaaaaaacaaaaacccaaaaaacccccaaacaaaccccccccccccaaaaccccccccaaaaaaccccccccccccaacccccccaaaaaaaaaaaaggggmmmossswwyywwyyvvvqqqllccccccccc
67
+abcccccccaaaaaaaaaacaaaaaacaaccccccccaaacccccccccccccccccccccccccccccaaaaaaccccccccccccaaaaacccaaaaaaaaaaaaaaggggoooosswwywwwwvvvvqqqmlccccccccc
68
+abccccccccccaaacaaaaaaaaaacccccccccccaaacaccccccccccccccccccccccccccccaaaaccccccccccccaaaaaccccaaacaaacccaaacagggfooosswwwwwrvvvvqqqqmmccccccccc
69
+abccccccccccaaacccaaaaaaaacccccccccaacaaaaacccccccccccccccccccccccccccaaaaccccccccccccaaaaaacccccccaaacccaaccccfffooosswwwwrrrrrqqqqqmmccccccccc
70
+abccccccccccaacccccccaaccccccccccccaaaaaaaacccccccccccccaaccccccccccccaccaccccccccccccccaaaacccccccaacccccccccccfffoossrwrrrrrrrqqqqmmmccccccccc
71
+abccaaaccccccccccccccaacccccccccccccaaaaaccccccccccccaacaacccccccaaaaacccccccccccccccccaacccccccccccccccccccccccfffoossrrrrrnnnmqqmmmmmccccccccc
72
+abcaaaaccccccccccccccccccccccccccccccaaaaacccccccccccaaaaacccccccaaaaacccaaaccccccccccccccccccccccccccccccccccccfffooorrrrrnnnnmmmmmmmccccaacccc
73
+abcaaaacccccccccccccccccccccccccccccaaacaaccccacccccccaaaaaaccccaaaaaaccccaaaccacccccccccccccccccccccccccccccccccffoooonnnnnnnnmmmmmmccccaaacccc
74
+abccaaacccccccccccccccccccccaaaaaccccaaccccaaaacccccaaaaaaaaccccaaaaaaccccaaaaaaaccccccccccccccccaccaccccccccccccfffooonnnnnnddddddddcccaaaccccc
75
+abccccccccccccccccccccccccccaaaaaccccccccccaaaaaacccaaaaacaacccaaaaaaaccaaaaaaaacccccccccccccccccaaaaccccccccccccfffeonnnnneddddddddddcaaacccccc
76
+abccccccccccaaaccccccccccccaaaaaacccccccccccaaaacccccacaaacccccaacaacccaaaaaaaaacccccccccccccccccaaaacccccccccccccffeeeeeeeeddddddddcccaaacccccc
77
+abcccccccccaaaaccccacccccccaaaaaaccccccccccaaaaacccccccaaaacccaaacaccccaaaaaaaaaccccccccccccccccaaaaaaccccccccccccceeeeeeeeedacccccccccccccccccc
78
+abaccccccccaaaaccccaaacaaacaaaaaaccccccccccaacaaccccccccaaaacaaaacaaacaaaaaaaaaacccccccccccccaacaaaaaacccccccccccccceeeeeeeaaacccccccccccccccaaa
79
+abaaacccccccaaaccccaaaaaaaccaaaccccccccaaacccccccccccccccaaaaaaaacaaaaaaaaaaaaaaacacaaccaaacaaacccaacccccccccccccccccaacccaaaacccccccccccccccaaa
80
+abaaaccccccccccccccaaaaaaccccccccccccccaaacccccccccccccccaaaaaaaccaaaaaaccaacccaccaaaaccaaaaaaaccccccccaaccccccccccccccccccaaacccccccccccccccaaa
81
+abaaccccccccccccccaaaaaaacccccccccccaaaaaaaaccccccccccccccaaaaaaaaaaaaaacaaaccccccaaaaaccaaaaaaccccccaaaaccccccccccccccccccaaaccccccccccccaaaaaa
82
+abaaaccccccccccccaaaaaaaaaacccccccccaaaaaaaacccccccccccaaaaaaaaaaaaaaaaaaacccccccaaaaaacaaaaaaaaaccccaaaaaacccccccccccccccccccccccccccccccaaaaaa

+ 87
- 0
src/Day12.hs View File

1
+module Day12
2
+  ( day12,
3
+    possibleStarts,
4
+    parseGrid,
5
+    findPath,
6
+    GridPoints (..),
7
+    Grid,
8
+    findBestPath
9
+  )
10
+where
11
+
12
+import Algorithm.Search (dijkstra)
13
+import Data.Char (ord)
14
+import Data.Map (Map)
15
+import qualified Data.Map as Map
16
+import Data.Maybe (mapMaybe)
17
+import Data.Tuple (swap)
18
+import Linear.V2 (V2 (..))
19
+import Data.Sort (sortOn)
20
+
21
+type Pos = V2 Int
22
+
23
+type Grid = Map Pos Char
24
+
25
+data GridPoints = GridPoints { _start :: Pos, _end :: Pos }
26
+  deriving (Show, Eq)
27
+
28
+parseGrid :: String -> (GridPoints, Grid)
29
+parseGrid input =
30
+  ( GridPoints
31
+      { _start = inverseGrid Map.! 'S',
32
+        _end = inverseGrid Map.! 'E'
33
+      },
34
+    Map.fromList grid
35
+  )
36
+  where
37
+    rows = zip [0 ..] $ lines input
38
+    grid = concatMap (uncurry colsForRow) rows
39
+    inverseGrid = Map.fromList $ map swap grid
40
+
41
+colsForRow :: Int -> String -> [(Pos, Char)]
42
+colsForRow r = zipWith makePos [0 ..]
43
+  where
44
+    makePos c height = (V2 r c, height)
45
+
46
+findPath :: Grid -> GridPoints -> Maybe (Int, [Pos])
47
+findPath grid GridPoints {_start = start, _end = end} =
48
+  dijkstra (neighbors grid) (const . const 1) solutionFound start
49
+  where
50
+    solutionFound = (==) end
51
+
52
+neighbors :: Grid -> Pos -> [Pos]
53
+neighbors grid pos = filter (canMove grid pos) possiblePositions
54
+  where
55
+    directions = [V2 1 0, V2 (-1) 0, V2 0 1, V2 0 (-1)]
56
+    possiblePositions = map (+ pos) directions
57
+
58
+canMove :: Grid -> Pos -> Pos -> Bool
59
+canMove grid from p = case grid Map.!? p of
60
+  Just c -> asHeight c <= (currentHeight + 1)
61
+  Nothing -> False
62
+  where
63
+    asHeight 'S' = asHeight 'a'
64
+    asHeight 'E' = asHeight 'z'
65
+    asHeight x = ord x - ord 'a'
66
+    currentHeight = asHeight $ grid Map.! from
67
+
68
+possibleStarts :: Grid -> [Pos]
69
+possibleStarts grid = map fst $ filter (\(_, v) -> v == 'a') (Map.toList grid)
70
+
71
+findBestPath :: Grid -> GridPoints -> (Int, [Pos])
72
+findBestPath grid points = fastest
73
+  where
74
+    starts = possibleStarts grid
75
+    newPoints = [points { _start = s} | s <- starts]
76
+    allPaths = mapMaybe (findPath grid) newPoints
77
+    fastest = head $ sortOn fst allPaths
78
+
79
+day12 :: IO ()
80
+day12 = do
81
+  input <- readFile "ressources/day12-input"
82
+  putStrLn "Day12"
83
+  let (positions, grid) = parseGrid input
84
+  let path = findPath grid positions
85
+  putStrLn ("Cost and path : " ++ show path)
86
+  let fastest = findBestPath grid positions
87
+  putStrLn ("Cost and path for nices route: " ++ show fastest)

+ 2
- 1
src/Lib.hs View File

13
 import Day9 (day9)
13
 import Day9 (day9)
14
 import Day10 (day10)
14
 import Day10 (day10)
15
 import Day11 (day11)
15
 import Day11 (day11)
16
+import Day12 (day12)
16
 
17
 
17
 days :: [IO ()]
18
 days :: [IO ()]
18
-days = [day1, day2, day3, day4, day5, day6, day7, day8, day9, day10, day11]
19
+days = [day1, day2, day3, day4, day5, day6, day7, day8, day9, day10, day11, day12]
19
 
20
 
20
 sep :: IO ()
21
 sep :: IO ()
21
 sep = putStrLn "---------"
22
 sep = putStrLn "---------"

+ 30
- 0
test/Day12Spec.hs View File

1
+{-# LANGUAGE QuasiQuotes #-}
2
+
3
+module Day12Spec (spec) where
4
+
5
+import Day12
6
+import Test.Hspec
7
+import Text.Heredoc
8
+import Data.Maybe (fromJust, isJust)
9
+
10
+testInput :: String
11
+testInput = [str|Sabqponm
12
+                |abcryxxl
13
+                |accszExk
14
+                |acctuvwj
15
+                |abdefghi
16
+                |]
17
+
18
+spec :: Spec
19
+spec =
20
+  describe "Day12" $ do
21
+    describe "Part1" $ do
22
+      it "finds a path" $ do
23
+        let (points, grid) = parseGrid testInput
24
+        let result = findPath grid points
25
+        isJust result `shouldBe` True
26
+        fst (fromJust result) `shouldBe` 31
27
+      it "finds shortest paths from all lowest points" $ do
28
+        let (points, grid) = parseGrid testInput
29
+        let fastest = findBestPath grid points
30
+        fst fastest `shouldBe` 29

Loading…
Cancel
Save