Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.4 kB · 76 lines
Ruby
at rust
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677#!/usr/bin/env ruby# frozen_string_literal: true
HEIGHT = ARGV.first == 't' ? 400_000 : 40
class Row def initialize(tiles) @tiles = tiles end
def width @tiles.size end
def trap?(index) return false if index < 0 || index >= @tiles.size @tiles[index] end
def left?(index) trap? index - 1 end
def right?(index) trap? index + 1 end
def center?(index) trap? index end
def count_safe @tiles.reduce(0) { |a, e| a + (e ? 0 : 1) } end
def to_s @tiles.map { |t| t ? '^' : '.' }.join endend
class Room def initialize(row) @rows = [Row.new(row.map { |t| t == '^' })] end
def row_count @rows.size end
def make_row previous = @rows.last data = previous.width.times.map do |index| (previous.left?(index) && previous.center?(index) && !previous.right?(index)) || (!previous.left?(index) && previous.center?(index) && previous.right?(index)) || (previous.left?(index) && !previous.center?(index) && !previous.right?(index)) || (!previous.left?(index) && !previous.center?(index) && previous.right?(index)) end @rows << Row.new(data) end
def count_safe @rows.reduce(0) { |a, e| a + e.count_safe } end
def to_s @rows.map(&:to_s).join "\n" endend
input = $stdin.readline.strip.chars
room = Room.new input
(HEIGHT - 1).times { room.make_row }
puts room.count_safe