Skip to content

Instantly share code, notes, and snippets.

@eam
Created December 10, 2015 01:54
Show Gist options
  • Select an option

  • Save eam/7b20f184043d03100c10 to your computer and use it in GitHub Desktop.

Select an option

Save eam/7b20f184043d03100c10 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
map = {}
xpos = 0
ypos = 0
xpos_robo = 0
ypos_robo = 0
map[0] = {}
map[0][0] = 1
robo = false
STDIN.each_char do |c|
if robo
xpos_robo += 1 if c == '>'
xpos_robo -= 1 if c == '<'
ypos_robo += 1 if c == '^'
ypos_robo -= 1 if c == 'v'
map[xpos_robo] = {} if map[xpos_robo].nil?
map[xpos_robo][ypos_robo] = 0 if map[xpos_robo][ypos_robo].nil?
map[xpos_robo][ypos_robo] += 1
else
xpos += 1 if c == '>'
xpos -= 1 if c == '<'
ypos += 1 if c == '^'
ypos -= 1 if c == 'v'
map[xpos] = {} if map[xpos].nil?
map[xpos][ypos] = 0 if map[xpos][ypos].nil?
map[xpos][ypos] += 1
end
robo = !robo
end
puts map.values.map(&:size).reduce(&:+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment