Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 718ffde

Browse files
committed
Deduplicate parts in 2024 day 20
1 parent eedd1fe commit 718ffde

File tree

2 files changed

+15
-56
lines changed

2 files changed

+15
-56
lines changed

‎src/main/scala/eu/sim642/adventofcode2024/Day20.scala

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,9 @@ object Day20 {
2626
}
2727

2828
trait Part {
29-
deffindCheats(grid: Grid[Char]):Set[Cheat]
29+
valmaxCheat: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

‎src/test/scala/eu/sim642/adventofcode2024/Day20Test.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Day20Test extends AnyFunSuite {
6060
assert(cheats(76) == 3)
6161
}
6262

63-
ignore("Part 2 input answer") { // TODO: optimize (~4.3s)
63+
test("Part 2 input answer") { // TODO: optimize (~4.3s)
6464
assert(Part2.countGoodCheats(parseGrid(input)) == 1011325)
6565
}
6666
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /