Created
December 10, 2015 01:54
-
-
Save eam/7b20f184043d03100c10 to your computer and use it in GitHub Desktop.
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 | |
| 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