-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.rb
108 lines (85 loc) · 1.93 KB
/
10.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
#!/usr/bin/env ruby
def path(x, y, lines, ends)
return 0 if x < 0 || x >= lines.length || y < 0 || y >= lines[0].length
curr = lines[x][y].to_i
if curr == 9 then
if ends["#{x},#{y}"] == false then
ends["#{x},#{y}"] = true
end
return
end
dirs = [[0, -1], [1, 0], [0, 1], [-1, 0]]
dirs.each do |dir|
xx = x + dir[0]
yy = y + dir[1]
if xx >= 0 && xx < lines.length && yy >= 0 && yy < lines[0].length then
if lines[xx][yy].to_i == curr + 1 then
path(xx, yy, lines, ends)
end
end
end
end
def p1
# Read file
file = File.new("input", "r")
lines = file.readlines.map(&:chomp)
file.close()
# Find all ends
ends = {}
lines.each_with_index do |line, i|
line.chars().each_with_index do |char, j|
ends["#{i},#{j}"] = false
end
end
# Count paths
paths = 0
lines.each_with_index do |line, i|
line.chars().each_with_index do |char, j|
if char == '0' then
path(i, j, lines, ends)
ends.each do |k, v|
paths += 1 if v == true
ends[k] = false
end
end
end
end
puts paths
end
def rate(x, y, lines)
return 0 if x < 0 || x >= lines.length || y < 0 || y >= lines[0].length
curr = lines[x][y].to_i
return 1 if curr == 9
dirs = [[0, -1], [1, 0], [0, 1], [-1, 0]]
paths = 0
dirs.each do |dir|
xx = x + dir[0]
yy = y + dir[1]
if xx >= 0 && xx < lines.length && yy >= 0 && yy < lines[0].length then
if lines[xx][yy].to_i == curr + 1 then
paths += rate(xx, yy, lines)
end
end
end
return paths
end
def p2
# Read file
file = File.new("input", "r")
lines = file.readlines.map(&:chomp)
file.close()
# Count paths
paths = 0
lines.each_with_index do |line, i|
line.chars().each_with_index do |char, j|
if char == '0' then
paths += rate(i, j, lines)
end
end
end
puts paths
end
if __FILE__ == $0 then
p1()
p2()
end