-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday12.rb
46 lines (44 loc) · 1.19 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
input = File.read("day12_input.txt").lines.map {|l| l.strip.chars.map(&:ord) }
start = {}
target = {}
input.each_with_index do |r,y|
r.each_with_index do |h,x|
if h == "S".ord
start = {x: x, y: y}
input[y][x] = "a".ord
end
if h == "E".ord
target = {x: x, y: y}
input[y][x] = "z".ord
end
end
end
checked = []
to_check = [target]
moves = [[1,0], [0,1], [-1, 0], [0, -1]]
depth = 0
found_S = false
found_a = 0
while to_check.length > 0 && !found_S
check_next = []
to_check.each do |pos|
height = input[pos[:y]][pos[:x]]
moves.each do |x,y|
to = {x: pos[:x] + x, y: pos[:y] + y}
next if !to[:x].between?(0, input[0].length-1)
next if !to[:y].between?(0, input.length-1)
next if checked.include?(to)
next if height > input[to[:y]][to[:x]]+1
check_next.push to
if "a".ord == input[to[:y]][to[:x]] && found_a == 0
found_a = depth + 1
end
found_S = true if to == start
end
end
checked.push(*to_check).uniq!
to_check = check_next.uniq
depth += 1
end
p depth
p found_a