-
-
Save leejarvis/0d3f029754f0cf7cb9b54fb8942a62d5 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# http://adventofcode.com/2017/day/19 | |
# slurp the input into a 2d array | |
ARGF.readlines.map(&:chars).tap{|m| | |
# assign some variables: | |
# v = visited cells | |
# d = current direction | |
# c = current position | |
# s = number of steps | |
v,d,c,s=[],[1,0],[0,m[0].find_index{|x|x!=" "}],1 | |
# get going | |
loop{ | |
# visit the current cell | |
v << m.dig(*c) | |
# lots of ugly stuff happening here | |
# 1. decide possible moves: | |
# (d[0].zero?? [d,[1,0],[-1,0]]:[d,[0,1],[0,-1]]) | |
# if d[0] is zero, we're moving on the Y axis, otherwise the X axis, | |
# so we can plan our next possible moves | |
# 2. Find the first move that contains a value in the cell | |
# find{|(a,b)|m.dig(c[0]+a,c[1]+b)!=" "} | |
# 3. Break from our loop if no possible moves have a value | |
# and s+= 1 or break | |
# and also increment the step counter | |
d=(d[0].zero?? [d,[1,0],[-1,0]]:[d,[0,1],[0,-1]]).find{|(a,b)|m.dig(c[0]+a,c[1]+b)!=" "}and s+=1 or break | |
# move position | |
c=[c[0] + d[0], c[1] + d[1]] | |
}|| | |
# print the result | |
p(v.grep(/[A-Z]+/).join,s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment