Skip to content

Instantly share code, notes, and snippets.

@wconrad
Created December 6, 2015 13:15
Show Gist options
  • Save wconrad/293521a817f9517a15cb to your computer and use it in GitHub Desktop.
Save wconrad/293521a817f9517a15cb to your computer and use it in GitHub Desktop.
Advent of code, day 1
#!/usr/bin/env ruby
# Advent of code, day 1
# http://adventofcode.com/day/2
input = File.read("input")
floors = Enumerator.new do |yielder|
floor = 0
input.chars.each do |c|
floor += case c
when "(" then 1
when ")" then -1
else raise
end
yielder.yield floor
end
end
puts floors.to_a.last
index_where_basement_entered = floors.with_index.find do |floor, i|
floor < 0
end.last
puts index_where_basement_entered + 1
@wconrad
Copy link
Author

wconrad commented Dec 6, 2015

Overengineered. Just wanted to try it with an enumeration.

@onebree
Copy link

onebree commented Dec 11, 2015

@wconrad Don't you mean /day/1 on line 4?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment