aboutsummaryrefslogtreecommitdiff
path: root/day03/part2
blob: a772a1ef161da37e720cd028224bf16e784b9360 (plain)
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
#!/usr/bin/env ruby

input = $stdin.readlines.map(&:strip).map{|x|x.split(",")}

def map_wire(wire_segments)
  wire = []
  x = 0
  y = 0
  wire_segments.each do |seg|
    dir = seg[0]
    len = seg[1..-1].to_i
    len.times do
      case dir
      when "R"
        x += 1
      when "L"
        x -= 1
      when "U"
        y += 1
      when "D"
        y -= 1
      end
      wire << [x, y]
    end
  end
  wire
end

wire1 = map_wire(input[0])
wire2 = map_wire(input[1])

intersections = (wire1 & wire2) - [0,0]
steps = intersections.map do |i|
  wire1.index(i) + wire2.index(i) + 2
end

puts steps.min