-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rb
85 lines (69 loc) · 1.79 KB
/
day12.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
Node = Struct.new(:x, :y, :height, :best, :isstart, :isend, :val)
hm = []
$start = nil
$end = nil
def init_node(val, x, y)
if val == 'S'
$start = [x, y]
Node.new(x, y, 1, nil, true, false, val)
elsif val == 'E'
$end = [x, y]
Node.new(x, y, 26, 0, false, true, val)
else
Node.new(x, y, (val.ord - 'a'.ord + 1), nil, false, false, val)
end
end
File.read(ARGV[0]).lines.each_with_index do |line, y|
hm.append line.chomp.split('').map.with_index { |val, x| init_node(val, x, y) }
end
$maxx = hm[0].size - 1
$maxy = hm.size - 1
#puts hm.inspect
#puts $start.inspect
#puts $end.inspect
def neighbors(node)
result = []
result << [node[:x]-1, node[:y]] if node[:x] > 0
result << [node[:x], node[:y]-1] if node[:y] > 0
result << [node[:x]+1, node[:y]] if node[:x] < $maxx
result << [node[:x], node[:y]+1] if node[:y] < $maxy
result
end
def printmap(hm)
hm.each do |row|
row.each { |node| print "#{node[:val]}/#{node[:best] || '-'} " }
puts ""
end
end
def lookup(hm, coord)
hm[coord[1]][coord[0]]
end
puts neighbors(lookup(hm, $start)).inspect
puts neighbors(lookup(hm, $end)).inspect
searchlist = []
searchlist << $end
while searchlist.size > 0 do
current = lookup(hm, searchlist.shift)
neighbors(current).each do |n|
neighbor = lookup(hm, n)
#puts neighbor.inspect
if neighbor[:height] >= current[:height] - 1
if neighbor[:best].nil? or neighbor[:best] > current[:best] + 1
neighbor[:best] = current[:best] + 1
searchlist << [neighbor[:x], neighbor[:y]]
end
end
end
end
#printmap(hm)
puts lookup(hm, $start).inspect
puts lookup(hm, $start)[:best]
best = 1000000
hm.each do |row|
row.each do |node|
if node[:height] == 1 && !node[:best].nil? && node[:best] < best
best = node[:best]
end
end
end
puts best