@@ -26,41 +26,9 @@ object Day20 {
2626 }
2727
2828 trait Part {
29- def findCheats ( grid : Grid [ Char ]) : Set [ Cheat ]
29+ val maxCheat : Int
3030
31- def countGoodCheats (grid : Grid [Char ]): Int = findCheats(grid).count(_.save >= 100 )
32- }
33- 34- object Part1 extends Part {
35- override def findCheats (grid : Grid [Char ]): Set [Cheat ] = {
36- val forwardSearch = gridGraphSearch(grid, 'S' , 'E' )
37- val forwardResult = BFS .search(forwardSearch)
38- val backwardSearch = gridGraphSearch(grid, 'E' , 'S' )
39- val backwardResult = BFS .search(backwardSearch)
40- 41- val noCheatDistance = forwardResult.target.get._2
42- 43- (for {
44- (row, y) <- grid.view.zipWithIndex
45- (cell, x) <- row.view.zipWithIndex
46- if cell == '#'
47- pos = Pos (x, y)
48- startOffset <- Pos .axisOffsets
49- start = pos + startOffset
50- if grid.containsPos(start) && grid(start) != '#'
51- endOffset <- Pos .axisOffsets
52- if startOffset != endOffset
53- end = pos + endOffset
54- if grid.containsPos(end) && grid(end) != '#'
55- cheatDistance = forwardResult.distances(start) + 2 + backwardResult.distances(end)
56- // if cheatDistance <= noCheatDistance
57- save = noCheatDistance - cheatDistance
58- } yield Cheat (start, end, save)).toSet
59- }
60- }
61- 62- object Part2 extends Part {
63- override def findCheats (grid : Grid [Char ]): Set [Cheat ] = {
31+ def findCheats (grid : Grid [Char ]): Set [Cheat ] = {
6432 val forwardSearch = gridGraphSearch(grid, 'S' , 'E' )
6533 val forwardResult = BFS .search(forwardSearch)
6634 val backwardSearch = gridGraphSearch(grid, 'E' , 'S' )
@@ -70,35 +38,16 @@ object Day20 {
7038
7139 // TODO: optimize
7240
73- /* (for {
74- (row, y) <- grid.view.zipWithIndex
75- (cell, x) <- row.view.zipWithIndex
76- if cell == '.'
77- start = Pos(x, y)
78- startOffset <- Pos.axisOffsets
79- startCheat <- 0 to 20
80- pos = start + startCheat *: startOffset
81- if grid.containsPos(pos)
82- endOffset <- Pos.axisOffsets
83- if startOffset != endOffset && startOffset != -endOffset
84- endCheat <- 0 to (20 - startCheat)
85- end = pos + endCheat *: endOffset
86- if grid.containsPos(end) && grid(end) != '#'
87- cheatDistance = forwardResult.distances(start) + (startCheat + endCheat) + backwardResult.distances(end)
88- //if cheatDistance <= noCheatDistance
89- save = noCheatDistance - cheatDistance
90- } yield Cheat(start, end, save)).toSet*/
91- 9241 (for {
9342 (row, y) <- grid.view.zipWithIndex
9443 (cell, x) <- row.view.zipWithIndex
9544 if cell != '#'
9645 start = Pos (x, y)
97- xOffset <- - 20 to 20
46+ xOffset <- - maxCheat to maxCheat
9847 pos = start + Pos (xOffset, 0 )
9948 if grid.containsPos(pos)
10049 startCheat = xOffset.abs
101- maxEndCheat = 20 - startCheat
50+ maxEndCheat = maxCheat - startCheat
10251 yOffset <- (- maxEndCheat) to maxEndCheat
10352 end = pos + Pos (0 , yOffset)
10453 if grid.containsPos(end) && grid(end) != '#'
@@ -108,6 +57,16 @@ object Day20 {
10857 save = noCheatDistance - cheatDistance
10958 } yield Cheat (start, end, save)).toSet
11059 }
60+ 61+ def countGoodCheats (grid : Grid [Char ]): Int = findCheats(grid).count(_.save >= 100 )
62+ }
63+ 64+ object Part1 extends Part {
65+ override val maxCheat : Int = 2
66+ }
67+ 68+ object Part2 extends Part {
69+ override val maxCheat : Int = 20
11170 }
11271
11372 def parseGrid (input : String ): Grid [Char ] = input.linesIterator.map(_.toVector).toVector
0 commit comments