#!/usr/bin/env tclsh set z 0; set zmin 0; set zmax 0 set y -1; set ymin 1 set xmin 0 while {[gets stdin line] >= 0} { if {$line eq ""} continue incr y set x -1 foreach c [split $line {}] { incr x if {$c eq "#"} {set grid($x,$y,$z) $c} } set xmax $x } set ymax $y proc neighbors {g x y z} { upvar $g grid set x0 [expr {$x-1}]; set x1 [expr {$x+1}] set y0 [expr {$y-1}]; set y1 [expr {$y+1}] set z0 [expr {$z-1}]; set z1 [expr {$z+1}] set n 0 for {set xx $x0} {$xx <= $x1} {incr xx} { for {set yy $y0} {$yy <= $y1} {incr yy} { for {set zz $z0} {$zz <= $z1} {incr zz} { if {$xx == $x && $yy == $y && $zz == $z} continue if {[info exists grid($xx,$yy,$zz)] && $grid($xx,$yy,$zz) eq "#"} { incr n } } } } return $n } proc tick {} { global grid xmin xmax ymin ymax zmin zmax incr xmin -1; incr xmax incr ymin -1; incr ymax incr zmin -1; incr zmax array set old [array get grid] array unset grid for {set x $xmin} {$x <= $xmax} {incr x} { for {set y $ymin} {$y <= $ymax} {incr y} { for {set z $zmin} {$z <= $zmax} {incr z} { set n [neighbors old $x $y $z] # puts "x=$x y=$y z=$z n=$n" if {! [info exists old($x,$y,$z)]} { if {$n == 3} {set grid($x,$y,$z) #} } else { if {$n == 2 || $n == 3} {set grid($x,$y,$z) #} } } } } } tick; tick; tick tick; tick; tick puts [array size grid]