Last active
August 29, 2015 14:01
-
-
Save francirp/d9016f08e182e894a160 to your computer and use it in GitHub Desktop.
In terminal, run: ruby turtle_tracks.rb, then type simple.txt or complex.txt, then open output.txt. More info at: http://www.puzzlenode.com/puzzles/9-turtle-tracks
This file contains 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
1001 | |
REPEAT 4 [ FD 200 LT 45 FD 100 LT 45 FD 50 LT 45 FD 25 LT 45 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 RT 90 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 LT 45 BK 25 RT 90 FD 25 LT 45 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 RT 90 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 LT 45 BK 25 LT 45 BK 50 RT 90 FD 50 LT 45 FD 25 LT 45 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 RT 90 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 LT 45 BK 25 RT 90 FD 25 LT 45 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 RT 90 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 LT 45 BK 25 LT 45 BK 50 LT 45 BK 100 RT 90 FD 100 LT 45 FD 50 LT 45 FD 25 LT 45 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 RT 90 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 LT 45 BK 25 RT 90 FD 25 LT 45 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 RT 90 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 LT 45 BK 25 LT 45 BK 50 RT 90 FD 50 LT 45 FD 25 LT 45 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 RT 90 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 LT 45 BK 25 RT 90 FD 25 LT 45 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 RT 90 FD 12 LT 45 FD 6 LT 45 RT 90 LT 45 BK 6 RT 90 FD 6 LT 45 RT 90 LT 45 BK 6 LT 45 BK 12 LT 45 BK 25 LT 45 BK 50 LT 45 BK 100 LT 45 BK 200 RT 90 ] |
This file contains 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
61 | |
RT 135 | |
FD 5 | |
REPEAT 2 [ RT 90 FD 15 ] | |
RT 90 | |
FD 5 | |
RT 45 | |
FD 20 |
This file contains 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
class TurtleTracks | |
def initialize(options = {}) | |
@inputs = parse_file(options[:file]) | |
@size = @inputs.shift.to_i | |
@commands = @inputs | |
@cursor = starting_point | |
@facing_degrees = 0 | |
end | |
def run | |
center_turtle | |
run_commands(@commands) | |
output_to_file | |
end | |
private | |
def parse_file(file) | |
array = [] | |
File.open(file).each do |line| | |
val = line.chop.split("\t") | |
array << val.first unless val.empty? | |
end | |
array | |
end | |
def run_commands(commands) | |
commands.each do |command| | |
args = command.split(" ") | |
method = args.shift | |
args = args.first.to_i unless method == "REPEAT" | |
send(method.downcase, args) | |
end | |
end | |
def output_to_file | |
file = open("output.txt","w") | |
file.write(to_s) | |
file.close() | |
end | |
def grid | |
@grid ||= Array.new(@size ** 2, ".") | |
end | |
def directions | |
{ 0 => -@size, | |
45 => -@size + 1, | |
90 => 1, | |
135 => @size + 1, | |
180 => @size, | |
225 => @size - 1, | |
270 => -1, | |
315 => -@size - 1 } | |
end | |
def starting_point | |
grid.size / 2 | |
end | |
def center_turtle | |
grid[starting_point] = "X" | |
end | |
def to_s | |
out = "" | |
grid.each_with_index do |i,n| | |
out << sprintf("%s%s", @grid[n], (n+1) % @size == 0 ? "\n" : " ") | |
end | |
out | |
end | |
def rt(degrees) | |
@facing_degrees += degrees | |
@facing_degrees -= 360 if @facing_degrees >= 360 | |
end | |
def lt(degrees) | |
@facing_degrees -= degrees | |
@facing_degrees += 360 if @facing_degrees < 0 | |
end | |
def fd(dist) | |
cursor_change = directions[@facing_degrees] | |
dist.times do | |
@cursor += cursor_change | |
grid[@cursor] = "X" | |
end | |
end | |
def bk(dist) | |
about_face | |
fd(dist) | |
about_face | |
end | |
def about_face | |
@facing_degrees += (@facing_degrees < 180) ? 180 : -180 | |
end | |
def repeat(args) | |
number_times = args.shift.to_i | |
args = args[1..-2] | |
commands = [] | |
args.each_with_index do |x,i| | |
commands << (args[i-1] + " " + args[i]) if i.odd? | |
end | |
number_times.times do |i| | |
run_commands(commands) | |
end | |
end | |
end | |
puts "Name of input file (for example: simple.txt)" | |
file = gets.strip | |
turtle_tracks = TurtleTracks.new(file: file) | |
turtle_tracks.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment