34 lines
850 B
Tcl
34 lines
850 B
Tcl
|
#!/usr/bin/env tclsh
|
||
|
source ../prelude.tcl
|
||
|
|
||
|
proc read_input {input} {
|
||
|
set dirs [split [gets $input] ""] ;# LR...
|
||
|
if {[gets $input] ne ""} { error "invalid input" }
|
||
|
set graph {}
|
||
|
while {[gets $input line] >= 0} {
|
||
|
must_regexp {^([A-Z]+) = \(([A-Z]+), ([A-Z]+)\)$} $line _ node left right
|
||
|
lappend graph [list $node $left $right]
|
||
|
}
|
||
|
return [list $dirs $graph]
|
||
|
}
|
||
|
|
||
|
proc solve {input} {
|
||
|
lassign [read_input $input] dirs graph
|
||
|
foreach x $graph {
|
||
|
lassign $x node l r
|
||
|
set G($node) [list $l $r]
|
||
|
}
|
||
|
puts [array get G]
|
||
|
set node AAA
|
||
|
set steps 0
|
||
|
while {$node ne "ZZZ"} {
|
||
|
set d [lindex $dirs [expr {$steps % [llength $dirs]}]]
|
||
|
set node [lindex $G($node) [expr {($d eq "L") ? 0 : 1}]]
|
||
|
puts "$d -> $node"
|
||
|
incr steps
|
||
|
}
|
||
|
puts "$steps"
|
||
|
}
|
||
|
|
||
|
solve stdin
|