Last active
December 20, 2020 06:55
-
-
Save gurgeous/9a515a3cf36ddffcda5db5a98b1a4468 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# use this to print at the end of part 1 | |
# | |
# (0...input.dim).each do |r| | |
# lines = [] | |
# (0...input.dim).each do |c| | |
# tile = board[[ r, c ]] | |
# tile[1..-2].each.with_index do |line, index| | |
# (lines[index] ||= '') << line[1..-2].join | |
# end | |
# end | |
# puts lines.join("\n") | |
# end | |
# | |
data = DATA.lines.map(&:chomp).map(&:chars) | |
monster = MONSTER.lines.map(&:chomp).map(&:chars) | |
def check(data, monster) | |
match = false | |
(0...data.length - monster.length).each do |r| | |
(0...data.first.length).each do |c| | |
# is there a monster here? | |
copy = data.map(&:dup) | |
n = 0 | |
(0...monster.length).each do |r2| | |
(0...monster.first.length).each do |c2| | |
if monster[r2][c2] == '#' && copy[r + r2][c + c2] == '#' | |
copy[r + r2][c + c2] = 'O' | |
n += 1 | |
end | |
end | |
end | |
# if we found a monster, make sure we use the new state from here on out | |
if n == 15 | |
match = true | |
data = copy | |
end | |
end | |
end | |
if match | |
p data.map(&:join).join.chars.count { |i| i == '#' } | |
exit | |
end | |
end | |
2.times do | |
# original | |
check(data, monster) | |
# rotate 3 times | |
3.times { check(data = data.transpose.map(&:reverse), monster) } | |
# flip | |
data = data.reverse | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment