#!/usr/bin/env tclsh set x 0; set y 0 set wx 10; set wy -1 proc left {} { global wx wy set tmp $wy set wy [expr {-1 * $wx}] set wx $tmp } proc right {} { global wx wy set tmp $wy set wy $wx set wx [expr {-1 * $tmp}] } while {[gets stdin line] >= 0} { if {$line eq ""} continue set op [string index $line 0] set n [string range $line 1 end] switch -- $op { N { incr wy -$n } S { incr wy $n } E { incr wx $n } W { incr wx -$n } L { switch -- $n { 90 { left } 180 { set wx [expr {-1 * $wx}] set wy [expr {-1 * $wy}] } 270 { right } } } R { switch -- $n { 90 { right } 180 { set wx [expr {-1 * $wx}] set wy [expr {-1 * $wy}] } 270 { left } } } F { set x [expr {$x + $wx * $n}] set y [expr {$y + $wy * $n}] } } } if {$x < 0} {set x [expr {-1 * $x}]} if {$y < 0} {set y [expr {-1 * $y}]} puts [expr {$x + $y}]