#!/usr/bin/env tclsh8.6 set xmax 0 set ymax 0 while {[gets stdin line] >= 0} { switch -glob -- $line { "" {continue} *,* { lassign [split $line ,] x y set grid($x,$y) x if {$x > $xmax} {set xmax $x} if {$y > $ymax} {set ymax $y} } fold\ along\ x* { lassign [split $line =] _ xfold for {set dx 1} {$xfold + $dx <= $xmax} {incr dx} { set x [expr {$xfold + $dx}] set xto [expr {$xfold - $dx}] for {set y 0} {$y <= $ymax} {incr y} { if {! [info exists grid($x,$y)]} continue set grid($xto,$y) x unset grid($x,$y) } } } fold\ along\ y* { lassign [split $line =] _ yfold for {set dy 1} {$yfold + $dy <= $ymax} {incr dy} { set y [expr {$yfold + $dy}] set yto [expr {$yfold - $dy}] for {set x 0} {$x <= $xmax} {incr x} { if {! [info exists grid($x,$y)]} continue set grid($x,$yto) x unset grid($x,$y) } } } } } set xmax 0 set ymax 0 foreach {key val} [array get grid] { lassign [split $key ,] x y if {$x > $xmax} {set xmax $x} if {$y > $ymax} {set ymax $y} } for {set y 0} {$y <= $ymax} {incr y} { set line "" for {set x 0} {$x <= $xmax} {incr x} { if {[info exists grid($x,$y)]} {append line x} else {append line { }} } puts $line }