#!/usr/bin/env ruby require_relative '../common' class Path attr_reader :path def initialize(path) @path = path.split(',') @x = @y = 0 end def coordinates @coordinates ||= path.inject([]) do |result, move| direction = move[0] count = move[1..].to_i result + count.times.map { |i| move(direction) } end end def move(direction) if direction == "L" @x -= 1 elsif direction == "R" @x += 1 elsif direction == "D" @y += 1 elsif direction == "U" @y -= 1 end [@x, @y] end end def part1 path1, path2 = Input.new(__dir__).readlines.map { |line| Path.new(line).coordinates } (path1 & path2).map { |coordinates| coordinates.map(&:abs).sum }.min end puts "=== Part 1 ===" puts part1