Ruby solution. It's an eternity of eachs (Ruby's for loop), but it runs in a few seconds.
Today's main problem was the example. Instructions were pretty clear, but the example had me thinking I misunderstood. Had to check Reddit, where a lot of other folks were confused too, and realised I was correct, the example had just been cropped weirdly.
$actives={0=>{0=>{}}}x_bounds={max: 0,min: 0}y_bounds={max: 0,min: 0}z_bounds={max: 0,min: 0}w_bounds={max: 0,min: 0}File.readlines("input.txt").each_with_indexdo|line,y|line.each_char.with_indexdo|char,x|ifchar=="#"$actives[0][0][y]||={}$actives[0][0][y][x]=truex_bounds[:max]=xifx>x_bounds[:max]x_bounds[:min]=xifx<x_bounds[:min]endendy_bounds[:max]=yify>y_bounds[:max]y_bounds[:min]=yify<y_bounds[:min]enddefget_cube_status(x,y,z,w)if($actives[w][z][y][x]rescuefalse)==true:activeelse:inactiveendenddefget_active_neighbours(x,y,z,w)active_neighbours=[](w-1..w+1).eachdo|current_w|(z-1..z+1).eachdo|current_z|(y-1..y+1).eachdo|current_y|(x-1..x+1).eachdo|current_x|nextif[x,y,z,w]==[current_x,current_y,current_z,current_w]ifget_cube_status(current_x,current_y,current_z,current_w)==:activeactive_neighbours<<[current_x,current_y,current_z,current_w]endendendendendactive_neighboursend6.timesdow_bounds[:max]+=1w_bounds[:min]-=1z_bounds[:max]+=1z_bounds[:min]-=1x_bounds[:max]+=1x_bounds[:min]-=1y_bounds[:max]+=1y_bounds[:min]-=1next_state=Marshal.load(Marshal.dump($actives))(w_bounds[:min]..w_bounds[:max]).eachdo|w|(z_bounds[:min]..z_bounds[:max]).eachdo|z|(y_bounds[:min]..y_bounds[:max]).eachdo|y|(x_bounds[:min]..x_bounds[:max]).eachdo|x|cube_status=get_cube_status(x,y,z,w)active_neighbours=get_active_neighbours(x,y,z,w)casecube_statuswhen:activenext_state[w][z][y][x]=nilunless(active_neighbours.size==2||active_neighbours.size==3)when:inactiveifactive_neighbours.size==3next_state[w]||={}next_state[w][z]||={}next_state[w][z][y]||={}next_state[w][z][y][x]=trueendendendendendend$actives=next_stateenddefcount_active(actives,x_bounds,y_bounds,z_bounds,w_bounds)active_count=0(w_bounds[:min]..w_bounds[:max]).eachdo|w|(z_bounds[:min]..z_bounds[:max]).eachdo|z|(y_bounds[:min]..y_bounds[:max]).eachdo|y|(x_bounds[:min]..x_bounds[:max]).eachdo|x|active=(actives[w][z][y][x]rescuefalse)==trueactive_count+=1ifactiveendendendendactive_countend# Helper function for when you need to visualise the layoutdefprint_grid(actives,x_bounds,y_bounds,z_bounds,w_bounds)(w_bounds[:min]..w_bounds[:max]).eachdo|w|(z_bounds[:min]..z_bounds[:max]).eachdo|z|print"z = #{z}, w = #{w}\n"(y_bounds[:min]..y_bounds[:max]).eachdo|y|(x_bounds[:min]..x_bounds[:max]).eachdo|x|active=(actives[z][y][x]rescuefalse)==trueprint(active?'#':'.')endprint"\n"endprint"\n"endprint"\n\n"endend# print_grid($actives, x_bounds, y_bounds, z_bounds, w_bounds)pcount_active($actives,x_bounds,y_bounds,z_bounds,w_bounds)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Ruby solution. It's an eternity of
each
s (Ruby'sfor
loop), but it runs in a few seconds.Today's main problem was the example. Instructions were pretty clear, but the example had me thinking I misunderstood. Had to check Reddit, where a lot of other folks were confused too, and realised I was correct, the example had just been cropped weirdly.