aboutsummaryrefslogtreecommitdiff
path: root/day03/part2
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2019-12-03 07:38:35 +0000
committerNat Lasseter <user@4574.co.uk>2019-12-03 07:38:35 +0000
commit4099b94b8094ca753df43e946b8804b2bf71b080 (patch)
tree9c181955dc55f166de90d1de87f107fa75d4a2ef /day03/part2
parentbe1c10d71edd16b68ef969f3b8b83dca6bb32dba (diff)
Day 03
Diffstat (limited to 'day03/part2')
-rwxr-xr-xday03/part237
1 files changed, 37 insertions, 0 deletions
diff --git a/day03/part2 b/day03/part2
new file mode 100755
index 0000000..a772a1e
--- /dev/null
+++ b/day03/part2
@@ -0,0 +1,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