You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2016/ruby/day_18.rb

50 lines
1.1 KiB

Row = Struct.new(:tiles) do
INPUT_MAP = { ?. => :safe, ?^ => :trap }
def self.from_s(input)
new(input.chars.map {|char| INPUT_MAP.fetch(char) })
end
def next
self.class.new(
%i[ safe safe ].insert(1, *tiles).each_cons(3).map {|l,c,r|
if l == :trap && c == :trap && r == :safe
:trap
elsif l == :safe && c == :trap && r == :trap
:trap
elsif l == :trap && c == :safe && r == :safe
:trap
elsif l == :safe && c == :safe && r == :trap
:trap
else
:safe
end
}
)
end
end
if __FILE__ == $0
row = Row.from_s(DATA.read.chomp)
rows = [row]
while rows.size < 40
rows << rows.last.next
end
p rows.map {|row| row.tiles.count(:safe) }.inject(:+)
end
require 'minitest'
# require 'minitest/autorun'
class TestRow < Minitest::Test
def test_row
row = Row.from_s('..^^.')
assert_equal %i[ safe safe trap trap safe ], row.tiles
assert_equal Row.from_s('.^^^^'), row.next
end
end
__END__
^^.^..^.....^..^..^^...^^.^....^^^.^.^^....^.^^^...^^^^.^^^^.^..^^^^.^^.^.^.^.^.^^...^^..^^^..^.^^^^