You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

43 lines
915 B

def parse(s)
s.split(",").map { |ss| { ss[0], ss[1..].to_i32 } }
end
def run_line(l)
poses = Set({Int32,Int32}).new
step_map = {} of {Int32,Int32} => Int32
x, y, steps = 0, 0, 0
l.each do |cmd|
dir, len = cmd
dx, dy = 0, 0
case dir
when 'U'
dy = 1
when 'D'
dy = -1
when 'L'
dx = -1
when 'R'
dx = 1
end
len.times do |i|
x += dx
y += dy
steps += 1
poses << {x,y}
step_map[{x,y}] ||= steps
end
end
return {poses, step_map}
end
lines = File.read("day3.txt").lines.map(&.chomp)
# wire1 = parse("R8,U5,L5,D3")
# wire2 = parse("U7,R6,D4,L4")
# puts run_line(wire1)
# puts run_line(wire2)
wire1 = parse(lines[0])
wire2 = parse(lines[1])
poses1, steps1 = run_line(wire1)
poses2, steps2 = run_line(wire2)
puts (poses1 & poses2).min_of { |p| p[0] + p[1] }
puts (poses1 & poses2).min_of { |p| steps1[p] + steps2[p] }