@@ -26,41 +26,9 @@ object Day20 {
26
26
}
27
27
28
28
trait Part {
29
- def findCheats ( grid : Grid [ Char ]) : Set [ Cheat ]
29
+ val maxCheat : Int
30
30
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 ] = {
64
32
val forwardSearch = gridGraphSearch(grid, 'S' , 'E' )
65
33
val forwardResult = BFS .search(forwardSearch)
66
34
val backwardSearch = gridGraphSearch(grid, 'E' , 'S' )
@@ -70,35 +38,16 @@ object Day20 {
70
38
71
39
// TODO: optimize
72
40
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
-
92
41
(for {
93
42
(row, y) <- grid.view.zipWithIndex
94
43
(cell, x) <- row.view.zipWithIndex
95
44
if cell != '#'
96
45
start = Pos (x, y)
97
- xOffset <- - 20 to 20
46
+ xOffset <- - maxCheat to maxCheat
98
47
pos = start + Pos (xOffset, 0 )
99
48
if grid.containsPos(pos)
100
49
startCheat = xOffset.abs
101
- maxEndCheat = 20 - startCheat
50
+ maxEndCheat = maxCheat - startCheat
102
51
yOffset <- (- maxEndCheat) to maxEndCheat
103
52
end = pos + Pos (0 , yOffset)
104
53
if grid.containsPos(end) && grid(end) != '#'
@@ -108,6 +57,16 @@ object Day20 {
108
57
save = noCheatDistance - cheatDistance
109
58
} yield Cheat (start, end, save)).toSet
110
59
}
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
111
70
}
112
71
113
72
def parseGrid (input : String ): Grid [Char ] = input.linesIterator.map(_.toVector).toVector
0 commit comments