aboutsummaryrefslogtreecommitdiff
path: root/day21/part1
blob: d579784e8f8d79cf6f32f9abdcb274adc919f116 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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