diff options
author | Nat Lasseter <Nat Lasseter nathan@bytemark.co.uk> | 2017-12-22 10:11:34 +0000 |
---|---|---|
committer | Nat Lasseter <Nat Lasseter nathan@bytemark.co.uk> | 2017-12-22 10:11:34 +0000 |
commit | ec97c17802d5a21f304de9333369cbaed70da06c (patch) | |
tree | 930fcf7ce57da9c94577258270e90baf6d24f71f /day22/part1 | |
parent | 749bf2f5daca318a6dda0e35112b8d211140e823 (diff) |
Day22
Diffstat (limited to 'day22/part1')
-rwxr-xr-x | day22/part1 | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/day22/part1 b/day22/part1 new file mode 100755 index 0000000..516efee --- /dev/null +++ b/day22/part1 @@ -0,0 +1,65 @@ +#!/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 + +input = $stdin.readlines.map(&:chomp) +size = input.length +infinity = 1000 + +map = [] + +infinity.times do + map << ([false]*((2*infinity)+size)) +end + +input.each do |r| + map << ([false]*infinity) + r.chars.map{|x| x == '#'} + ([false]*infinity) +end + +infinity.times do + map << ([false]*((2*infinity)+size)) +end + +row = infinity + (size + 1)/2 - 1 +col = infinity + (size + 1)/2 - 1 +dir = [-1, 0] +infections = 0 + +10_000.times do + if map[row][col] then + dir = turn_right(dir) + map[row][col] = false + else + dir = turn_left(dir) + map[row][col] = true + infections += 1 + end + row += dir[0] + col += dir[1] +end + +puts infections |