aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-22 01:12:34 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-22 01:12:34 +0000
commit749bf2f5daca318a6dda0e35112b8d211140e823 (patch)
tree3a8a8ba8842db99167ace61b1db9322e17be8a58
parent8f62ee7864320794b3f92548e122b4d6eded02c2 (diff)
Day21
-rw-r--r--day21/input108
-rwxr-xr-xday21/part1122
-rwxr-xr-xday21/part2122
3 files changed, 352 insertions, 0 deletions
diff --git a/day21/input b/day21/input
new file mode 100644
index 0000000..65f3e97
--- /dev/null
+++ b/day21/input
@@ -0,0 +1,108 @@
+../.. => ..#/.#./...
+#./.. => .../#../.##
+##/.. => .##/###/##.
+.#/#. => #.#/..#/#.#
+##/#. => .../.##/...
+##/## => ##./..#/..#
+.../.../... => ##../..../##../.###
+#../.../... => ...#/.#.#/.#../.#.#
+.#./.../... => #.#./...#/#.#./.##.
+##./.../... => ..#./#.##/#.../.###
+#.#/.../... => ##../##.#/..#./#.##
+###/.../... => ..../.#.#/.###/#..#
+.#./#../... => #..#/#.../.##./....
+##./#../... => #.##/..##/####/.###
+..#/#../... => ..#./#.##/####/####
+#.#/#../... => .##./#.##/#.#./##.#
+.##/#../... => #.##/####/.###/...#
+###/#../... => ..../#.#./##.#/..##
+.../.#./... => .###/.##./##../.##.
+#../.#./... => ..../#.##/...#/#.#.
+.#./.#./... => ...#/####/.##./#...
+##./.#./... => .###/#.##/###./....
+#.#/.#./... => #.##/###./..../..#.
+###/.#./... => .#../#.#./#.##/#.##
+.#./##./... => .###/##../..##/#..#
+##./##./... => ..#./#.#./.#.#/##.#
+..#/##./... => .#../####/...#/..##
+#.#/##./... => ..../##.#/.##./....
+.##/##./... => .#.#/.#.#/.##./####
+###/##./... => ##.#/..../..../....
+.../#.#/... => ..##/##../##.#/###.
+#../#.#/... => ####/#.##/#.../###.
+.#./#.#/... => ..../#..#/..##/.#..
+##./#.#/... => #.../..##/##../..#.
+#.#/#.#/... => ...#/#.#./#.#./#...
+###/#.#/... => ###./###./##.#/###.
+.../###/... => ..#./###./##.#/####
+#../###/... => ##.#/..#./##../..##
+.#./###/... => #.../#.##/##../....
+##./###/... => ..##/.#.#/#..#/#.##
+#.#/###/... => #.##/..#./.#../..##
+###/###/... => ..#./#..#/####/.##.
+..#/.../#.. => ##.#/#.##/...#/###.
+#.#/.../#.. => #..#/..#./##../###.
+.##/.../#.. => ..#./.#../###./#.#.
+###/.../#.. => ...#/...#/.#.#/.##.
+.##/#../#.. => ##../#.#./#..#/##..
+###/#../#.. => ##../.#.#/##../#..#
+..#/.#./#.. => ##.#/##.#/...#/.#..
+#.#/.#./#.. => .###/.#.#/###./....
+.##/.#./#.. => #..#/###./####/..#.
+###/.#./#.. => ..#./.###/.###/...#
+.##/##./#.. => #.##/..##/...#/.###
+###/##./#.. => ####/##.#/#.##/#..#
+#../..#/#.. => ..../.##./#.##/#...
+.#./..#/#.. => #..#/##../...#/#...
+##./..#/#.. => ..#./.###/..##/.#.#
+#.#/..#/#.. => .##./..##/..#./#..#
+.##/..#/#.. => ####/.#.#/#.../.#.#
+###/..#/#.. => ..../..##/#.##/###.
+#../#.#/#.. => #.##/.#.#/.#../.##.
+.#./#.#/#.. => ..##/###./.###/###.
+##./#.#/#.. => ##.#/##.#/#.#./##..
+..#/#.#/#.. => ###./###./.#.#/.#..
+#.#/#.#/#.. => ##../..#./##../....
+.##/#.#/#.. => .###/#.#./##.#/##..
+###/#.#/#.. => ##.#/#.#./.#.#/#...
+#../.##/#.. => .#.#/...#/.#.#/..#.
+.#./.##/#.. => ###./##../##.#/....
+##./.##/#.. => ..##/###./#.#./#.#.
+#.#/.##/#.. => ##.#/..##/#..#/####
+.##/.##/#.. => ..../####/..#./##..
+###/.##/#.. => .###/#..#/..../.#..
+#../###/#.. => #..#/.#../.#.#/#...
+.#./###/#.. => .#../..../.##./.###
+##./###/#.. => ##.#/.#../.#.#/#..#
+..#/###/#.. => #.##/##../..##/#...
+#.#/###/#.. => ####/..##/.#../##.#
+.##/###/#.. => .###/#..#/.###/#.##
+###/###/#.. => ..##/.##./##../#..#
+.#./#.#/.#. => ..##/.##./.##./.###
+##./#.#/.#. => ..##/...#/.##./####
+#.#/#.#/.#. => .###/.###/#.#./.#..
+###/#.#/.#. => ##.#/###./##.#/####
+.#./###/.#. => ...#/..#./.#.#/.#..
+##./###/.#. => ###./##.#/#.../#.#.
+#.#/###/.#. => .##./#.#./...#/..#.
+###/###/.#. => .#.#/.#../..##/####
+#.#/..#/##. => .##./...#/#..#/.###
+###/..#/##. => #.##/.#.#/...#/..##
+.##/#.#/##. => ###./.###/...#/....
+###/#.#/##. => .##./.##./#.#./#...
+#.#/.##/##. => #.#./.##./.#.#/.###
+###/.##/##. => ..../####/.#.#/#.##
+.##/###/##. => .##./.###/###./.#..
+###/###/##. => #.../###./.##./##.#
+#.#/.../#.# => #.#./..../#.##/###.
+###/.../#.# => .#../.#.#/#.../.###
+###/#../#.# => ###./#..#/####/##..
+#.#/.#./#.# => ###./##.#/..../.#..
+###/.#./#.# => ####/.#.#/.#../..##
+###/##./#.# => #.#./####/..##/#...
+#.#/#.#/#.# => #.#./#.#./#.../#.##
+###/#.#/#.# => #.##/.#../..#./.##.
+#.#/###/#.# => .###/..##/####/#..#
+###/###/#.# => #.../..#./..#./#.##
+###/#.#/### => .#.#/.###/#.##/..##
+###/###/### => #.#./...#/.#../.#.#
diff --git a/day21/part1 b/day21/part1
new file mode 100755
index 0000000..d579784
--- /dev/null
+++ b/day21/part1
@@ -0,0 +1,122 @@
+#!/usr/bin/env ruby
+
+class Grid
+ def self.from_s(str)
+ out = []
+ str.split('/').each do |row|
+ out << row.chars.map{|x| x == '#'}
+ end
+ return Grid.new(out)
+ end
+
+ def self.join(grids)
+ out = []
+ l = grids[0][0].size
+ grids.each_with_index do |row, i|
+ (0...l).each do |j|
+ out[(i*l)+j] = [] if out[(i*l)+j].nil?
+ end
+ row.each do |grid|
+ grid.to_a.each_with_index do |srow, j|
+ out[(i*l)+j] += srow
+ end
+ end
+ end
+ return Grid.new(out)
+ end
+
+ def initialize(arr = [[false, true, false], [false, false, true], [true, true, true]])
+ @grid = arr
+ end
+
+ def size
+ return @grid.length
+ end
+
+ def split
+ out = []
+ if @grid.length.even? then
+ (0...@grid.length).step(2).each do |r|
+ row = []
+ (0...@grid.length).step(2).each do |c|
+ row << Grid.new([
+ [@grid[r][c], @grid[r][c+1]],
+ [@grid[r+1][c], @grid[r+1][c+1]]
+ ])
+ end
+ out << row
+ end
+ else
+ (0...@grid.length).step(3).each do |r|
+ row = []
+ (0...@grid.length).step(3).each do |c|
+ row << Grid.new([
+ [@grid[r][c], @grid[r][c+1], @grid[r][c+2]],
+ [@grid[r+1][c], @grid[r+1][c+1], @grid[r+1][c+2]],
+ [@grid[r+2][c], @grid[r+2][c+1], @grid[r+2][c+2]]
+ ])
+ end
+ out << row
+ end
+ end
+ return out
+ end
+
+ def flip
+ return Grid.new(@grid.map(&:reverse))
+ end
+
+ def rot
+ return Grid.new(@grid.transpose.map(&:reverse))
+ end
+
+ def liveness
+ return @grid.flatten.count(true)
+ end
+
+ def to_s
+ out = []
+ @grid.each do |row|
+ out << row.map{|x| x ? '#' : '.'}.join
+ end
+ return out.join('/')
+ end
+
+ def to_a
+ return @grid
+ end
+end
+
+input = $stdin.readlines.map(&:chomp)
+map = input.map{|line| line.split(' => ')}.to_h
+
+grid = Grid.new
+
+5.times do
+ grids = grid.split
+ new = grids.map do |row|
+ row.map do |grid|
+ if !map[grid.to_s].nil? then
+ Grid.from_s(map[grid.to_s])
+ elsif !map[grid.rot.to_s].nil? then
+ Grid.from_s(map[grid.rot.to_s])
+ elsif !map[grid.rot.rot.to_s].nil? then
+ Grid.from_s(map[grid.rot.rot.to_s])
+ elsif !map[grid.rot.rot.rot.to_s].nil? then
+ Grid.from_s(map[grid.rot.rot.rot.to_s])
+
+ elsif !map[grid.flip.to_s].nil? then
+ Grid.from_s(map[grid.flip.to_s])
+ elsif !map[grid.flip.rot.to_s].nil? then
+ Grid.from_s(map[grid.flip.rot.to_s])
+ elsif !map[grid.flip.rot.rot.to_s].nil? then
+ Grid.from_s(map[grid.flip.rot.rot.to_s])
+ elsif !map[grid.flip.rot.rot.rot.to_s].nil? then
+ Grid.from_s(map[grid.flip.rot.rot.rot.to_s])
+ end
+ end
+ end
+ grid = Grid.join(new)
+end
+
+puts grid.liveness
diff --git a/day21/part2 b/day21/part2
new file mode 100755
index 0000000..f37083a
--- /dev/null
+++ b/day21/part2
@@ -0,0 +1,122 @@
+#!/usr/bin/env ruby
+
+class Grid
+ def self.from_s(str)
+ out = []
+ str.split('/').each do |row|
+ out << row.chars.map{|x| x == '#'}
+ end
+ return Grid.new(out)
+ end
+
+ def self.join(grids)
+ out = []
+ l = grids[0][0].size
+ grids.each_with_index do |row, i|
+ (0...l).each do |j|
+ out[(i*l)+j] = [] if out[(i*l)+j].nil?
+ end
+ row.each do |grid|
+ grid.to_a.each_with_index do |srow, j|
+ out[(i*l)+j] += srow
+ end
+ end
+ end
+ return Grid.new(out)
+ end
+
+ def initialize(arr = [[false, true, false], [false, false, true], [true, true, true]])
+ @grid = arr
+ end
+
+ def size
+ return @grid.length
+ end
+
+ def split
+ out = []
+ if @grid.length.even? then
+ (0...@grid.length).step(2).each do |r|
+ row = []
+ (0...@grid.length).step(2).each do |c|
+ row << Grid.new([
+ [@grid[r][c], @grid[r][c+1]],
+ [@grid[r+1][c], @grid[r+1][c+1]]
+ ])
+ end
+ out << row
+ end
+ else
+ (0...@grid.length).step(3).each do |r|
+ row = []
+ (0...@grid.length).step(3).each do |c|
+ row << Grid.new([
+ [@grid[r][c], @grid[r][c+1], @grid[r][c+2]],
+ [@grid[r+1][c], @grid[r+1][c+1], @grid[r+1][c+2]],
+ [@grid[r+2][c], @grid[r+2][c+1], @grid[r+2][c+2]]
+ ])
+ end
+ out << row
+ end
+ end
+ return out
+ end
+
+ def flip
+ return Grid.new(@grid.map(&:reverse))
+ end
+
+ def rot
+ return Grid.new(@grid.transpose.map(&:reverse))
+ end
+
+ def liveness
+ return @grid.flatten.count(true)
+ end
+
+ def to_s
+ out = []
+ @grid.each do |row|
+ out << row.map{|x| x ? '#' : '.'}.join
+ end
+ return out.join('/')
+ end
+
+ def to_a
+ return @grid
+ end
+end
+
+input = $stdin.readlines.map(&:chomp)
+map = input.map{|line| line.split(' => ')}.to_h
+
+grid = Grid.new
+
+18.times do |i|
+ grids = grid.split
+ new = grids.map do |row|
+ row.map do |grid|
+ if !map[grid.to_s].nil? then
+ Grid.from_s(map[grid.to_s])
+ elsif !map[grid.rot.to_s].nil? then
+ Grid.from_s(map[grid.rot.to_s])
+ elsif !map[grid.rot.rot.to_s].nil? then
+ Grid.from_s(map[grid.rot.rot.to_s])
+ elsif !map[grid.rot.rot.rot.to_s].nil? then
+ Grid.from_s(map[grid.rot.rot.rot.to_s])
+
+ elsif !map[grid.flip.to_s].nil? then
+ Grid.from_s(map[grid.flip.to_s])
+ elsif !map[grid.flip.rot.to_s].nil? then
+ Grid.from_s(map[grid.flip.rot.to_s])
+ elsif !map[grid.flip.rot.rot.to_s].nil? then
+ Grid.from_s(map[grid.flip.rot.rot.to_s])
+ elsif !map[grid.flip.rot.rot.rot.to_s].nil? then
+ Grid.from_s(map[grid.flip.rot.rot.rot.to_s])
+ end
+ end
+ end
+ grid = Grid.join(new)
+end
+
+puts grid.liveness