diff options
author | Nat Lasseter <Nat Lasseter nathan@bytemark.co.uk> | 2017-12-19 18:34:34 +0000 |
---|---|---|
committer | Nat Lasseter <Nat Lasseter nathan@bytemark.co.uk> | 2017-12-19 18:34:34 +0000 |
commit | b8e0dc361e2ddf8c272629a8b4bb6d52a82baad7 (patch) | |
tree | f714ca6e67f53933be98354acf479ead68ebf9b2 /day19/part1 | |
parent | dbb4a9a351e95e4af223b7324b0795124c2b8cd9 (diff) |
Day18
Diffstat (limited to 'day19/part1')
-rwxr-xr-x | day19/part1 | 66 |
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 |