Created
December 6, 2015 13:46
-
-
Save wconrad/2636f30d30fe2bcac850 to your computer and use it in GitHub Desktop.
Advent of Code, day 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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