Created
December 24, 2020 05:57
-
-
Save gurgeous/e78b9b06566821e9013df444c97a5c72 to your computer and use it in GitHub Desktop.
This file contains 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
DIRS = { | |
'e' => [ 1, 0 ], | |
'w' => [ -1, 0 ], | |
'sw' => [ -1, -1 ], | |
'se' => [ 0, -1 ], | |
'nw' => [ 0, 1 ], | |
'ne' => [ 1, 1 ], | |
}.freeze | |
# | |
# part 1 | |
# | |
tiles = Set.new | |
data.split("\n").each do |s| | |
coords = [ 0, 0 ] | |
until s.empty? | |
dir = s[/^(?:w|e|sw|se|nw|ne)/] | |
s = s[dir.length..] | |
coords = coords.zip(DIRS[dir]).map(&:sum) | |
end | |
tiles.include?(coords) ? tiles.delete(coords) : (tiles << coords) | |
end | |
p tiles.length | |
# | |
# part 2 | |
# | |
1.upto(100) do |day| | |
copy = tiles.dup | |
# note Range.grow helper I added after day 17 | |
b = tiles.to_a.transpose.map { |x| Range.new(*x.minmax).grow } | |
b[0].each do |x| | |
b[1].each do |y| | |
adj = DIRS.values.count { |dx, dy| tiles === [ x + dx, y + dy ] } | |
coord = [ x, y ] | |
if tiles.include?(coord) && (adj == 0) || adj > 2 | |
copy.delete(coord) | |
elsif !tiles.include?(coord) && adj == 2 | |
copy << coord | |
end | |
end | |
end | |
puts "#{day}: #{copy.length}" | |
tiles = copy | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment