Skip to content

Commit 77d4cc0

Browse files
committed
Day 10 Ruby solutions
1 parent d0c1111 commit 77d4cc0

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

10-1.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
5+
class Map
6+
attr_reader :map, :visited
7+
8+
def initialize(input)
9+
@height = input.size
10+
@width = input[0].size
11+
@trailheads = []
12+
@peaks = []
13+
@map = Numo::UInt8.zeros(@height, @width)
14+
@height.times do |y|
15+
@width.times do |x|
16+
case input[y][x]
17+
when '.'
18+
@map[y, x] = 255
19+
when '0'
20+
@map[y, x] = 0
21+
@trailheads << [y, x]
22+
when '9'
23+
@map[y, x] = 9
24+
@peaks << [y, x]
25+
else
26+
@map[y, x] = input[y][x].to_i
27+
end
28+
end
29+
end
30+
end
31+
32+
def trace(y, x, visited)
33+
return 0 if visited[y, x] == 1
34+
35+
visited[y, x] = 1
36+
cur_height = @map[y, x]
37+
38+
return 1 if cur_height == 9
39+
40+
score = 0
41+
score += trace(y + 1, x, visited) if y + 1 < @height && @map[y + 1, x] == cur_height + 1
42+
score += trace(y - 1, x, visited) if y - 1 >= 0 && @map[y - 1, x] == cur_height + 1
43+
score += trace(y, x + 1, visited) if x + 1 < @width && @map[y, x + 1] == cur_height + 1
44+
score += trace(y, x - 1, visited) if x - 1 >= 0 && @map[y, x - 1] == cur_height + 1
45+
score
46+
end
47+
48+
def hike
49+
score = 0
50+
@trailheads.each do |t|
51+
score += trace(t[0], t[1], Numo::UInt8.zeros(@height, @width))
52+
end
53+
score
54+
end
55+
56+
def inspect
57+
to_s
58+
end
59+
60+
def to_s
61+
s = "<#{self.class}:\n"
62+
@height.times do |y|
63+
@width.times do |x|
64+
s += case @map[y, x]
65+
when 255
66+
'.'
67+
else
68+
@map[y, x].to_s
69+
end
70+
end
71+
s += "\n"
72+
end
73+
s += '>'
74+
s
75+
end
76+
end
77+
78+
map = Map.new File.read('10.input').lines.map(&:strip)
79+
puts map.hike

10-2.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
5+
class Map
6+
attr_reader :map, :visited
7+
8+
def initialize(input)
9+
@height = input.size
10+
@width = input[0].size
11+
@trailheads = []
12+
@peaks = []
13+
@map = Numo::UInt8.zeros(@height, @width)
14+
@height.times do |y|
15+
@width.times do |x|
16+
case input[y][x]
17+
when '.'
18+
@map[y, x] = 255
19+
when '0'
20+
@map[y, x] = 0
21+
@trailheads << [y, x]
22+
when '9'
23+
@map[y, x] = 9
24+
@peaks << [y, x]
25+
else
26+
@map[y, x] = input[y][x].to_i
27+
end
28+
end
29+
end
30+
end
31+
32+
def trace(y, x, visited)
33+
return 0 if visited.include? [y, x]
34+
35+
cur_height = @map[y, x]
36+
37+
return 1 if cur_height == 9
38+
39+
score = 0
40+
score += trace(y + 1, x, visited + [y, x]) if y + 1 < @height && @map[y + 1, x] == cur_height + 1
41+
score += trace(y - 1, x, visited + [y, x]) if y - 1 >= 0 && @map[y - 1, x] == cur_height + 1
42+
score += trace(y, x + 1, visited + [y, x]) if x + 1 < @width && @map[y, x + 1] == cur_height + 1
43+
score += trace(y, x - 1, visited + [y, x]) if x - 1 >= 0 && @map[y, x - 1] == cur_height + 1
44+
score
45+
end
46+
47+
def hike
48+
score = 0
49+
@trailheads.each do |t|
50+
score += trace(t[0], t[1], [])
51+
end
52+
score
53+
end
54+
55+
def inspect
56+
to_s
57+
end
58+
59+
def to_s
60+
s = "<#{self.class}:\n"
61+
@height.times do |y|
62+
@width.times do |x|
63+
s += case @map[y, x]
64+
when 255
65+
'.'
66+
else
67+
@map[y, x].to_s
68+
end
69+
end
70+
s += "\n"
71+
end
72+
s += '>'
73+
s
74+
end
75+
end
76+
77+
map = Map.new File.read('10.input').lines.map(&:strip)
78+
puts map.hike

0 commit comments

Comments
 (0)