aboutsummaryrefslogtreecommitdiff
path: root/day19/part1
diff options
context:
space:
mode:
Diffstat (limited to 'day19/part1')
-rwxr-xr-xday19/part166
1 files changed, 66 insertions, 0 deletions
diff --git a/day19/part1 b/day19/part1
new file mode 100755
index 0000000..536bb42
--- /dev/null
+++ b/day19/part1
@@ -0,0 +1,66 @@
+#!/usr/bin/env ruby
+
+def turn_left(vector)
+ return case vector
+ when [1, 0]
+ [0, 1]
+ when [0, 1]
+ [-1, 0]
+ when [-1, 0]
+ [0, -1]
+ when [0, -1]
+ [1, 0]
+ end
+end
+
+def turn_right(vector)
+ return case vector
+ when [1, 0]
+ [0, -1]
+ when [0, -1]
+ [-1, 0]
+ when [-1, 0]
+ [0, 1]
+ when [0, 1]
+ [1, 0]
+ end
+end
+
+def turn(map, vector, row, col)
+ lvec = turn_left(vector)
+ rvec = turn_right(vector)
+
+ lrow = row + lvec[0]
+ lcol = col + lvec[1]
+
+ rrow = row + rvec[0]
+ rcol = col + rvec[1]
+
+ if map[lrow][lcol] == ' ' then
+ return rvec
+ else
+ return lvec
+ end
+end
+
+input = $stdin.readlines.map(&:chomp).map(&:chars).map(&:to_a)
+
+row = 0
+col = input[0].index('|')
+vec = [1, 0]
+saw = []
+
+loop do
+ row += vec[0]
+ col += vec[1]
+ this = input[row][col]
+ if this == ' ' then
+ break
+ elsif this >= 'A' && this <= 'Z' then
+ saw << this
+ elsif this == '+' then
+ vec = turn(input, vec, row, col)
+ end
+end
+
+puts saw.join