about summary refs log tree commit diff
path: root/aoc/2022/09/part-one.tcl
blob: 3e7716a5354f8bc68e1bddf96512f4a087886224 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env tclsh
proc vec x {list [expr {abs($x)}] [expr {($x>0)-($x<0)}]}
set tx [set hx 0]
set ty [set hy 0]
while {1} {
    set line [gets stdin]
    if {[eof stdin] || $line == ""} then break
    lassign [split $line] d n
    for {set i 0} {$i < $n} {incr i} {
        switch $d {
            R {incr hx 1}
            U {incr hy 1}
            L {incr hx -1}
            D {incr hy -1}
        }
        lassign [vec [expr {$hx-$tx}]] mx dx
        lassign [vec [expr {$hy-$ty}]] my dy
        if {$mx == 2 || $my == 2} {
            incr tx $dx
            incr ty $dy
        }
        set v($tx,$ty) 1
    }
}
puts [array size v]