-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.rb
111 lines (96 loc) · 3.19 KB
/
16.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# https://adventofcode.com/2024/day/16
map = File.readlines('input16.txt').map(&:chomp).map(&:chars)
start_y = map.index { |r| r.include?('S') }
start_x = map[start_y].index('S')
finish_y = map.index { |r| r.include?('E') }
finish_x = map[finish_y].index('E')
map.each_index do |y|
map[y].each_with_index do |item, x|
item == '#' ? (map[y][x] = -1) : (map[y][x] = 0)
end
end
########################################################################################################################
# 1
########################################################################################################################
paths = [[start_y, start_x]]
dirs = { [start_y, start_x] => 'e' }
loop do
new_paths = []
paths.each do |y, x|
score = map[y][x]
if dirs[[y, x]] == 'e'
if map[y][x + 1] == 0 || map[y][x + 1] > score + 1
new_paths << [y, x + 1]
map[y][x + 1] = score + 1
dirs[[y, x + 1]] = 'e'
end
if map[y - 1][x] == 0 || map[y - 1][x] > score + 1001
new_paths << [y - 1, x]
map[y - 1][x] = score + 1001
dirs[[y - 1, x]] = 'n'
end
if map[y + 1][x] == 0 || map[y + 1][x] > score + 1001
new_paths << [y + 1, x]
map[y + 1][x] = score + 1001
dirs[[y + 1, x]] = 's'
end
end
if dirs[[y, x]] == 'n'
if map[y - 1][x] == 0 || map[y - 1][x] > score + 1
new_paths << [y - 1, x]
map[y - 1][x] = score + 1
dirs[[y - 1, x]] = 'n'
end
if map[y][x + 1] == 0 || map[y][x + 1] > score + 1001
new_paths << [y, x + 1]
map[y][x + 1] = score + 1001
dirs[[y, x + 1]] = 'e'
end
if map[y][x - 1] == 0 || map[y][x - 1] > score + 1001
new_paths << [y, x - 1]
map[y][x - 1] = score + 1001
dirs[[y, x - 1]] = 'w'
end
end
if dirs[[y, x]] == 's'
if map[y + 1][x] == 0 || map[y + 1][x] > score + 1
new_paths << [y + 1, x]
map[y + 1][x] = score + 1
dirs[[y + 1, x]] = 's'
end
if map[y][x + 1] == 0 || map[y][x + 1] > score + 1001
new_paths << [y, x + 1]
map[y][x + 1] = score + 1001
dirs[[y, x + 1]] = 'e'
end
if map[y][x - 1] == 0 || map[y][x - 1] > score + 1001
new_paths << [y, x - 1]
map[y][x - 1] = score + 1001
dirs[[y, x - 1]] = 'w'
end
end
if dirs[[y, x]] == 'w'
if map[y][x - 1] == 0 || map[y][x - 1] > score + 1
new_paths << [y, x - 1]
map[y][x - 1] = score + 1
dirs[[y, x - 1]] = 'w'
end
if map[y - 1][x] == 0 || map[y - 1][x] > score + 1001
new_paths << [y - 1, x]
map[y - 1][x] = score + 1001
dirs[[y - 1, x]] = 'n'
end
if map[y + 1][x] == 0 || map[y + 1][x] > score + 1001
new_paths << [y + 1, x]
map[y + 1][x] = score + 1001
dirs[[y + 1, x]] = 's'
end
end
end
break if new_paths.empty?
paths = new_paths
end
puts map[finish_y][finish_x]
########################################################################################################################
# 2
########################################################################################################################