Skip to content

Instantly share code, notes, and snippets.

@onebree

onebree/day03.rb Secret

Last active December 18, 2015 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onebree/c13c6245e9fbbb4feb0d to your computer and use it in GitHub Desktop.
Save onebree/c13c6245e9fbbb4feb0d to your computer and use it in GitHub Desktop.
advent of code - day 3
def current_position(previous, move)
x, y = 0, 0
case move
when '^' then y += 1
when '>' then x += 1
when 'v' then y -= 1
when '<' then x -= 1
end
[ previous[0] + x, previous[1] + y ]
end
def map_deliveries(directions)
deliveries = [ [0, 0] ]
directions.each do |move|
previous = deliveries.last
deliveries.push current_position(previous, move)
end
deliveries
end
def solo_santa_deliveries
map_deliveries(@input.chars)
end
def santa_deliveries
directions = @input.chars.select.with_index { |x, i| i.even? }
map_deliveries(directions)
end
def robo_santa_deliveries
directions = @input.chars.select.with_index { |x, i| i.odd? }
map_deliveries(directions)
end
def calculate_houses
results = {
solo: solo_santa_deliveries.uniq.length,
helper: (santa_deliveries + robo_santa_deliveries).uniq.length
}
end
@input = File.read('input').strip
puts "Houses that received at least one gift from Santa: #{calculate_houses[:solo]}"
puts "Houses that received at least one gift from Santa or Robo-Santa: #{calculate_houses[:helper]}"

Day 3: Perfectly Spherical Houses in a Vacuum

Santa is delivering presents to an infinite two-dimensional grid of houses.

He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.

However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?

For example:

  • > delivers presents to 2 houses: one at the starting location, and one to the east.
  • ^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
  • ^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.
x, y = 0, 0
moves = [ [x, y] ]
input = File.read('input')
input.chars.each do |move|
case move
when '^' then y += 1
when '>' then x += 1
when 'v' then y -= 1
when '<' then x -= 1
end
moves.push [x, y]
end
houses = moves.uniq.size
puts "Houses that receive at least one present: #{houses}"

Part Two

The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.

Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.

This year, how many houses receive at least one present?

For example:

  • ^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.
  • ^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.
  • ^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.
def directions(santa)
input = File.read('input').strip
input.chars.select.with_index { |x, i| i.send(santa) }
end
def current_position(previous, move)
x, y = 0, 0
case move
when '^' then y += 1
when '>' then x += 1
when 'v' then y -= 1
when '<' then x -= 1
end
[ previous[0] + x, previous[1] + y ]
end
def map_deliveries(santa)
deliveries = [ [0, 0] ]
directions(santa).each do |move|
previous = deliveries.last
deliveries.push current_position(previous, move)
end
deliveries
end
def santa_deliveries
map_deliveries(:even?)
end
def robo_santa_deliveries
map_deliveries(:odd?)
end
def calculate_houses
(santa_deliveries + robo_santa_deliveries).uniq.length
end
puts "Santa delivered to #{santa_deliveries.length} houses"
puts "Robo Santa delivered to #{robo_santa_deliveries.length} houses"
puts "Houses that receive at least one present: #{calculate_houses}"
v>v<vvv<<vv^v<v>vv>v<<<^^^^^<<^<vv>^>v^>^>^>^>^><vvvv<^>^<<^><<<^vvvv>^>^><^v^><^<>^^>^vvv^<vv>>^>^^<>><>^>vvv>>^vv>^<><>^<v^>^>^><vv^vv^>><<^><<v>><>^<^>>vvv>v>>>v<<^<><^<v<>v>^^v^^^<^v^^>>><^>^>v<>^<>>^>^^v^><v<v>>><>v<v^v>^v<>>^><v>^<>v^>^<>^v^^^v^^>>vv<<^^><^<vvv>^>^^<^>>^^^^^v^<v>vv<>>v^v<^v^^<><^<^vv^><>><><>v>vvv^vv^^<<><<vvv><<^v^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^^>^^>^<^><>>v<>>^^<^>><<<v<>>>^v^>v>v<<^^<<^>v<v^>>v^^v^^<<>^v>v><v^>v<^^>^<vv><vv^<>v<><^<<<vv<<v>v<^<<<<^^>v^v^^><<><^^^<v>v^^>>>vvv><>vv<>>^^v^v<<^>v^^v^>vv>^<<v<^<v^>^^<<v<^^>^v^^<^^v<<>>vv<^>>^><><>v>>v<>^<v^^><<>>>
@onebree
Copy link
Author

onebree commented Dec 18, 2015

x, y = compute_position_from(move)

def compute_position_from(move)
x, y = 0, 0

do the case thing here

return x, y
end

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