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 | |
parent | 749bf2f5daca318a6dda0e35112b8d211140e823 (diff) |
Day22
-rw-r--r-- | day22/input | 25 | ||||
-rwxr-xr-x | day22/part1 | 65 | ||||
-rwxr-xr-x | day22/part2 | 71 |
3 files changed, 161 insertions, 0 deletions
diff --git a/day22/input b/day22/input new file mode 100644 index 0000000..e68a336 --- /dev/null +++ b/day22/input @@ -0,0 +1,25 @@ +.########.....#...##.#### +....#..#.#.##.###..#.##.. +##.#.#..#.###.####.##.#.. +####...#...####...#.##.## +..#...###.#####.....##.## +..#.##.######.#...###...# +.#....###..##....##...##. +##.##..####.#.######...## +#...#..##.....#..#...#..# +........#.##..###.#.....# +#.#..######.#.###..#...#. +.#.##.##..##.####.....##. +.....##..#....#####.#.#.. +...#.#.#..####.#..###..#. +##.#..##..##....#####.#.. +.#.#..##...#.#####....##. +.####.#.###.####...#####. +...#...######..#.##...#.# +#..######...#.####.#..#.# +...##..##.#.##.#.#.#....# +###..###.#..#.....#.##.## +..#....##...#..#..##..#.. +.#.###.##.....#.###.#.### +####.##...#.#....#..##... +#.....#.#..#.##.#..###..# 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 diff --git a/day22/part2 b/day22/part2 new file mode 100755 index 0000000..affcea0 --- /dev/null +++ b/day22/part2 @@ -0,0 +1,71 @@ +#!/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 << ([:clean]*((2*infinity)+size)) +end + +input.each do |r| + map << ([:clean]*infinity) + r.chars.map{|x| x == '#' ? :infected : :clean} + ([:clean]*infinity) +end + +infinity.times do + map << ([:clean]*((2*infinity)+size)) +end + +row = infinity + (size + 1)/2 - 1 +col = infinity + (size + 1)/2 - 1 +dir = [-1, 0] +infections = 0 + +10_000_000.times do + case map[row][col] + when :clean + dir = turn_left(dir) + map[row][col] = :weakened + when :weakened + map[row][col] = :infected + infections += 1 + when :infected + dir = turn_right(dir) + map[row][col] = :flagged + when :flagged + dir = turn_right(turn_right(dir)) + map[row][col] = :clean + end + row += dir[0] + col += dir[1] +end + +puts infections |