-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.rb
85 lines (73 loc) · 2.46 KB
/
day20.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# https://adventofcode.com/2024/day/20
module Advent
module Year2024
class Day20 < Advent::Challenge
# Explanation here
def part1
shortcut_count(2)
end
def part2
shortcut_count(20)
end
# Prepopulate a grid representing the distance from the origin to each path point
def distance_grid
@distance_grid ||= Array.new(maze.height) { Array.new(maze.width, -1) }.tap do |distances|
current = origin
distances[current.y][current.x] = 0
while current != finish
maze.adjacent_points_in_range(current).each do |point|
next if maze.value_at(point) == "#"
next if distances[point.y][point.x] != -1
distances[point.y][point.x] = distances[current.y][current.x] + 1
current = point
end
end
end
end
def shortcut_count(test_radius, cutoff = 100)
count = 0
# Iterate the whole grid
maze.each_point do |start_point|
next if is_wall?(start_point)
# Find all points within 20 of the start point
(2..test_radius).each do |radius|
# For each radius, find all points by varying row_delta and col_delta
(0..radius).each do |row_delta|
col_delta = radius - row_delta
point_in_quadrants(start_point, row_delta, col_delta).each do |end_point|
next unless maze.in_range?(end_point)
next if is_wall?(end_point)
# We now have a valid shortcut. Check the distance it saves
distance = distance_grid[end_point.y][end_point.x] - distance_grid[start_point.y][start_point.x]
if distance >= cutoff + radius
count += 1
end
end
end
end
end
count
end
def point_in_quadrants(point, row_delta, col_delta)
Set.new([
point + Point.new(col_delta, row_delta),
point + Point.new(col_delta, -row_delta),
point + Point.new(-col_delta, row_delta),
point + Point.new(-col_delta, -row_delta)
])
end
def is_wall?(point)
maze.value_at(point) == "#"
end
def maze
@maze ||= Grid.new(input_lines)
end
def origin
@origin ||= maze.find_first_char("S")
end
def finish
@finish ||= maze.find_first_char("E")
end
end
end
end