Add updated solutions, including day 20.
This commit is contained in:
51
day17.cr
51
day17.cr
@@ -1,46 +1,47 @@
|
||||
require "advent"
|
||||
require "benchmark"
|
||||
|
||||
INPUT = input(2020, 17).lines.map(&.chars)
|
||||
|
||||
def part1(input)
|
||||
def solve(input, dim)
|
||||
step = input.clone
|
||||
cubes = Set({Int32,Int32,Int32,Int32}).new
|
||||
new_cubes = Set({Int32,Int32,Int32,Int32}).new
|
||||
cubes = Set(Array(Int32)).new
|
||||
new_cubes = Set(Array(Int32)).new
|
||||
input.each_with_index do |row, y|
|
||||
row.each_with_index do |c, x|
|
||||
cubes << {x,y,0,0} if c == '#'
|
||||
cubes << [x,y].concat([0] * (dim-2)) if c == '#'
|
||||
end
|
||||
end
|
||||
|
||||
6.times do |i|
|
||||
neighbor_count = {} of {Int32,Int32,Int32,Int32} => Int32
|
||||
cubes.each do |c|
|
||||
x,y,z,w = c
|
||||
(-1..1).each do |dx|
|
||||
(-1..1).each do |dy|
|
||||
(-1..1).each do |dz|
|
||||
(-1..1).each do |dw|
|
||||
next if dx == 0 && dy == 0 && dz == 0 && dw == 0
|
||||
neighbor_count[{x+dx,y+dy,z+dz,w+dw}] = (neighbor_count[{x+dx,y+dy,z+dz,w+dw}]? || 0) + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
8.times do |i|
|
||||
print '.'
|
||||
neighbor_count = Hash(Array(Int32), Int32).new(0)
|
||||
Array.product([[-1,0,1]] * dim).each do |diff|
|
||||
next if diff.all? &.==(0)
|
||||
cubes.each do |c|
|
||||
neighbor_count[c.zip_with(diff) { |a,b| a+b }] += 1
|
||||
end
|
||||
end
|
||||
|
||||
new_cubes.clear
|
||||
neighbor_count.each do |n, i|
|
||||
if cubes.includes?(n)
|
||||
new_cubes << n if (i == 2 || i == 3)
|
||||
elsif i == 3
|
||||
new_cubes << n
|
||||
end
|
||||
new_cubes << n if i == 3 || (cubes.includes?(n) && i == 2)
|
||||
end
|
||||
new_cubes, cubes = cubes, new_cubes
|
||||
end
|
||||
cubes.size
|
||||
end
|
||||
|
||||
def part2(input)
|
||||
def part1(input)
|
||||
solve(input, 3)
|
||||
end
|
||||
|
||||
puts part1(INPUT.clone)
|
||||
puts part2(INPUT.clone)
|
||||
def part2(input)
|
||||
solve(input, 4)
|
||||
end
|
||||
|
||||
(3..).each do |i|
|
||||
print "Dim #{i} "
|
||||
bm = Benchmark.measure { puts " #{solve(INPUT, i)}" }
|
||||
puts bm.real * 1000
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user