Skip to content

Instantly share code, notes, and snippets.

@wconrad
Created December 6, 2015 13:46
Show Gist options
  • Select an option

  • Save wconrad/2636f30d30fe2bcac850 to your computer and use it in GitHub Desktop.

Select an option

Save wconrad/2636f30d30fe2bcac850 to your computer and use it in GitHub Desktop.
Advent of Code, day 3
#!/usr/bin/env ruby
# http://adventofcode.com/day/3
require "set"
class Visits
def initialize
@locations = Set.new
end
def traverse(path)
coord = [0, 0]
@locations << coord
path.each do |c|
delta = DIRS.fetch(c)
coord = coord.zip(delta).map { |a, d| a + d }
@locations << coord
end
end
def count
@locations.count
end
private
DIRS = {
"^" => [0, -1],
"v" => [0, +1],
"<" => [-1, 0],
">" => [+1, 0],
}
end
PATH = File.read("input").chars
# part 1
visits = Visits.new
visits.traverse(PATH)
puts visits.count
# part 2
visits = Visits.new
PATH.partition.with_index do |c, i|
i.even?
end.each do |individual_path|
visits.traverse(individual_path)
end
puts visits.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment