Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 20, 2025. It is now read-only.

Commit 01f79de

Browse files
Day 12 Ruby solutions
1 parent ca87d2e commit 01f79de

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

‎12-1.rb‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
require 'algorithms'
5+
6+
class Map
7+
attr_accessor :start, :end, :map, :lengths
8+
9+
def initialize(input)
10+
@height = input.size
11+
@width = input[0].size
12+
@map = Numo::UInt8.zeros(@height, @width)
13+
input.each_index do |row|
14+
input[row].each_index do |col|
15+
case input[row][col]
16+
when 'S'
17+
@start = [row, col]
18+
@map[row, col] = 0
19+
when 'E'
20+
@end = [row, col]
21+
@map[row, col] = 'z'.ord - 'a'.ord
22+
else
23+
@map[row, col] = input[row][col].ord - 'a'.ord
24+
end
25+
end
26+
end
27+
28+
@lengths = Numo::UInt32.new(@height, @width).fill Numo::UInt32::MAX
29+
@lengths[@start[0], @start[1]] = 0
30+
end
31+
32+
def solve!
33+
queue = Containers::MinHeap.new
34+
queue.push(0, @start)
35+
36+
until queue.empty?
37+
length = queue.next_key
38+
row, col = queue.pop
39+
next if length > @lengths[row, col]
40+
41+
neighbours = [
42+
[row + 1, col],
43+
[row - 1, col],
44+
[row, col + 1],
45+
[row, col - 1]
46+
]
47+
neighbours.select! do |coords|
48+
coords[0] >= 0 and
49+
coords[0] < @height and
50+
coords[1] >= 0 and
51+
coords[1] < @width and
52+
@map[coords[0], coords[1]] <= @map[row, col] + 1
53+
end
54+
55+
neighbours.each do |step|
56+
new_length = length + 1
57+
if new_length < @lengths[step[0], step[1]]
58+
@lengths[step[0], step[1]] = new_length
59+
queue.push(new_length, [step[0], step[1]])
60+
end
61+
end
62+
end
63+
@lengths[@end[0], @end[1]]
64+
end
65+
66+
def to_s
67+
s = "<#{self.class}:\n"
68+
s += "Start: #{@start}, end: #{@end}\n"
69+
s += "\nMap:\n"
70+
@height.times do |row|
71+
@width.times do |col|
72+
s += @map[row, col].to_s.rjust(2, '0') + ' '
73+
end
74+
s += "\n"
75+
end
76+
s += "\nLengths:\n"
77+
@height.times do |row|
78+
@width.times do |col|
79+
s += @lengths[row, col].to_s.rjust(Math::log10(@lengths.max).ceil, '0') + ' '
80+
end
81+
s += "\n"
82+
end
83+
s += '>'
84+
s
85+
end
86+
87+
alias inspect to_s
88+
end
89+
90+
input = File.read('12.input').lines.map(&:strip).map(&:chars)
91+
92+
map = Map.new(input)
93+
94+
print map.solve!, "\n"

‎12-2.rb‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
require 'algorithms'
5+
6+
class Map
7+
attr_accessor :start, :end, :map, :lengths
8+
9+
def initialize(input)
10+
@height = input.size
11+
@width = input[0].size
12+
@map = Numo::UInt8.zeros(@height, @width)
13+
input.each_index do |row|
14+
input[row].each_index do |col|
15+
case input[row][col]
16+
when 'S'
17+
@map[row, col] = 0
18+
when 'E'
19+
@start = [row, col]
20+
@map[row, col] = 'z'.ord - 'a'.ord
21+
else
22+
@map[row, col] = input[row][col].ord - 'a'.ord
23+
end
24+
end
25+
end
26+
27+
@lengths = Numo::UInt32.new(@height, @width).fill Numo::UInt32::MAX
28+
@lengths[@start[0], @start[1]] = 0
29+
end
30+
31+
def solve!
32+
queue = Containers::MinHeap.new
33+
queue.push(0, @start)
34+
35+
until queue.empty?
36+
length = queue.next_key
37+
row, col = queue.pop
38+
next if length > @lengths[row, col]
39+
40+
neighbours = [
41+
[row + 1, col],
42+
[row - 1, col],
43+
[row, col + 1],
44+
[row, col - 1]
45+
]
46+
neighbours.select! do |coords|
47+
coords[0] >= 0 and
48+
coords[0] < @height and
49+
coords[1] >= 0 and
50+
coords[1] < @width and
51+
@map[row, col] <= @map[coords[0], coords[1]] + 1
52+
end
53+
54+
neighbours.each do |step|
55+
new_length = length + 1
56+
if new_length < @lengths[step[0], step[1]]
57+
@lengths[step[0], step[1]] = new_length
58+
queue.push(new_length, [step[0], step[1]])
59+
end
60+
end
61+
end
62+
end
63+
64+
def solution
65+
closest = Numo::UInt32::MAX
66+
@map.each_with_index do |height, row, col|
67+
next if height.positive?
68+
69+
if @lengths[row, col] < closest
70+
closest = @lengths[row, col]
71+
end
72+
end
73+
closest
74+
end
75+
76+
def to_s
77+
s = "<#{self.class}:\n"
78+
s += "Start: #{@start}, end: #{@end}\n"
79+
s += "\nMap:\n"
80+
@height.times do |row|
81+
@width.times do |col|
82+
s += @map[row, col].to_s.rjust(2, '0') + ' '
83+
end
84+
s += "\n"
85+
end
86+
s += "\nLengths:\n"
87+
@height.times do |row|
88+
@width.times do |col|
89+
s += @lengths[row, col].to_s.rjust(Math::log10(@lengths.max).ceil, '0') + ' '
90+
end
91+
s += "\n"
92+
end
93+
s += '>'
94+
s
95+
end
96+
97+
alias inspect to_s
98+
end
99+
100+
input = File.read('12.input').lines.map(&:strip).map(&:chars)
101+
102+
map = Map.new(input)
103+
104+
map.solve!
105+
106+
print map.solution, "\n"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /