blob: 90f1d4fd7dc923feebd2fae972f0132c811818ec (
plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
#!/usr/bin/env tclsh
set fp [open input r]
set map ""
while {1} {
set ret [gets $fp line]
if {$ret == -1} {
break
}
if {[string length $line] == 0} {
break
}
lappend map $line
}
close $fp
proc char_at {x y} {
set line [lindex $::map $y]
return [string index $line $x]
}
set height [llength $map]
set width [string length [lindex $map 0]]
proc find_start {} {
for {set i 0} {$i < $::width} {incr i} {
for {set j 0} {$j < $::height} {incr j} {
if {[char_at $i $j] == "^"} {
return "$i $j"
}
}
}
}
set start_point [find_start]
proc front_point {x y direction} {
if {$direction == "up"} {
return [list $x [expr {$y - 1}]]
} elseif {$direction == "down"} {
return [list $x [expr {$y + 1}]]
} elseif {$direction == "left"} {
return [list [expr {$x - 1}] $y]
} elseif {$direction == "right"} {
return [list [expr {$x + 1}] $y]
}
}
proc out_of_range {x y} {
return [expr {
($x < 0) || ($x >= $::width) || ($y < 0) || ($y >= $::height)
}]
}
proc turn_right {direction} {
if {$direction == {up}} {
return right
} elseif {$direction == {down}} {
return left
} elseif {$direction == {right}} {
return down
} else {
return up
}
}
proc next_state {x y direction} {
set forward [front_point $x $y $direction]
if {[out_of_range {*}$forward]} {
return [list {*}$forward $direction]
}
if {[char_at {*}$forward] == {#}} {
set forward [front_point $x $y [turn_right $direction]]
return [list {*}$forward [turn_right $direction]]
} else {
return [list {*}$forward $direction]
}
}
set state [list {*}$start_point up]
set visited {}
while {![out_of_range {*}[lrange $state 0 1]]} {
dict set visited [lrange $state 0 1] {}
set state [next_state {*}$state]
}
puts [dict size $visited]
|