Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @posx=0
- @posy=0
- orient="N"
- step=0
- part2=0
- # N
- # W E
- # S
- move={
- "N"=>lambda {|num|@posy=@posy+num},
- "S"=>lambda {|num|@posy=@posy-num},
- "E"=>lambda {|num|@posx=@posx+num},
- "W"=>lambda {|num|@posx=@posx-num}
- }
- orientation={
- "N"=>{"R"=>"E","L"=>"W"},
- "S"=>{"R"=>"W","L"=>"E"},
- "E"=>{"R"=>"S","L"=>"N"},
- "W"=>{"R"=>"N","L"=>"S"}
- }
- visited=Hash.new
- File.read("day1.input").split(', ').each {|direction|
- orient = orientation[orient][direction[0]]
- oldx,oldy=@posx,@posy
- move[orient].call(direction[1..-1].to_i)
- #Retracing steps
- ([oldx,@posx].min..[oldx,@posx].max).each {|x|
- ([oldy,@posy].min..[oldy,@posy].max).each {|y|
- if visited[x.to_s+','+y.to_s] and visited[x.to_s+','+y.to_s]<step-1 and part2==0
- puts "Hey, I was already there at step "+visited[x.to_s+','+y.to_s].to_s
- part2=(x.abs+y.abs)
- puts "That's "+part2.to_s+" blocks from where I started"
- else
- visited[x.to_s+','+y.to_s]=step
- end
- }
- }
- step=step+1
- }
- puts "I am now "+ (@posx.abs+@posy.abs).to_s+" blocks from where I started"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement