-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.rb
115 lines (85 loc) · 3.03 KB
/
06.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
112
113
114
115
# https://adventofcode.com/2024/day/6
map = File.readlines('input06.txt').map(&:chomp).map(&:chars)
@height = map.length
@width = map[0].length
@start_y = map.index { |row| row.include?('^') }
@start_x = map[@start_y].index('^')
########################################################################################################################
# 1
########################################################################################################################
dup_map = map.map(&:dup)
cur_y, cur_x = @start_y, @start_x
cur_dir = '^'
loop do
dup_map[cur_y][cur_x] = 'X'
if cur_dir == '^'
break if cur_y == 0
dup_map[cur_y - 1][cur_x] == '#' ? cur_dir = '>' : cur_y -= 1
elsif cur_dir == '>'
break if cur_x == @width - 1
dup_map[cur_y][cur_x + 1] == '#' ? cur_dir = 'v' : cur_x += 1
elsif cur_dir == 'v'
break if cur_y == @height - 1
dup_map[cur_y + 1][cur_x] == '#' ? cur_dir = '<' : cur_y += 1
elsif cur_dir == '<'
break if cur_x == 0
dup_map[cur_y][cur_x - 1] == '#' ? cur_dir = '^' : cur_x -= 1
end
end
puts dup_map.sum { |row| row.count { |cell| cell == 'X' } }
# 5199
########################################################################################################################
# 2
# This is not a perfect performance, but it works for the input. It can be probably optimized by calculation during
# initial path finding.
########################################################################################################################
dup_map = map.map(&:dup)
cur_y, cur_x = @start_y, @start_x
cur_dir = '^'
path = []
loop do
path << [cur_y, cur_x]
if cur_dir == '^'
break if cur_y == 0
dup_map[cur_y - 1][cur_x] == '#' ? cur_dir = '>' : cur_y -= 1
elsif cur_dir == '>'
break if cur_x == @width - 1
dup_map[cur_y][cur_x + 1] == '#' ? cur_dir = 'v' : cur_x += 1
elsif cur_dir == 'v'
break if cur_y == @height - 1
dup_map[cur_y + 1][cur_x] == '#' ? cur_dir = '<' : cur_y += 1
elsif cur_dir == '<'
break if cur_x == 0
dup_map[cur_y][cur_x - 1] == '#' ? cur_dir = '^' : cur_x -= 1
end
end
def map_has_loop?(map)
cur_y, cur_x = @start_y, @start_x
dup_map = map.map(&:dup)
cur_dir = '0'
loop do
return true if dup_map[cur_y][cur_x].include?(cur_dir)
dup_map[cur_y][cur_x] += cur_dir
if cur_dir == '0'
return false if cur_y == 0
dup_map[cur_y - 1][cur_x] == '#' ? cur_dir = '1' : cur_y -= 1
elsif cur_dir == '1'
return false if cur_x == @width - 1
dup_map[cur_y][cur_x + 1] == '#' ? cur_dir = '2' : cur_x += 1
elsif cur_dir == '2'
return false if cur_y == @height - 1
dup_map[cur_y + 1][cur_x] == '#' ? cur_dir = '3' : cur_y += 1
elsif cur_dir == '3'
return false if cur_x == 0
dup_map[cur_y][cur_x - 1] == '#' ? cur_dir = '0' : cur_x -= 1
end
end
end
result = 0
path[1..].uniq.each do |path_point_y, path_point_x|
dup_map[path_point_y][path_point_x] = '#'
result += 1 if map_has_loop?(dup_map)
dup_map[path_point_y][path_point_x] = '.'
end
puts result
# 1915