#!/usr/bin/env tclsh proc solve {input} { set data {} while {[gets $input line] >= 0} { lappend data $line } set data [join $data] set x 0 set y 0 set y0 0 set y1 1 set x0 0 set x1 1 set grid [dict create 0,0 1] foreach {dir steps color} $data { puts "$x $y $dir $steps $color" for {set i 0} {$i < $steps} {incr i} { switch $dir { {U} {incr y -1} {D} {incr y} {L} {incr x -1} {R} {incr x} } dict set grid $x,$y 1 set x0 [expr {min($x0,$x)}] set y0 [expr {min($y0,$y)}] set x1 [expr {max($x1,$x+1)}] set y1 [expr {max($y1,$y+1)}] } } set t 0 for {set y $y0} {$y < $y1} {incr y} { set row "" set in 0 for {set x $x0} {$x < $x1} {incr x} { if {[dict exists $grid $x,$y]} { if {[dict exists $grid $x,[expr {$y+1}]]} { set in [expr {!$in}] } lappend row "#" incr t } else { if {$in} { lappend row "o" incr t } else { lappend row "." } } } puts [join $row ""] } puts "$t" } solve stdin