adventofcode2023/day01/sol.tcl

29 lines
713 B
Tcl
Executable File

#!/usr/bin/env tclsh8.6
proc tonum {x} {
if {[string is digit $x]} {
return $x
}
return [expr {1+[lsearch {one two three four five six seven eight nine} $x]}]
}
set in stdin
set part2 0
while {[gets $in line] >= 0} {
set chars [split $line ""]
set digits [list]
for {set i 0} {$i < [string length $line]} {incr i} {
if {[regexp -start $i {\A([0-9]|one|two|three|four|five|six|seven|eight|nine)} $line x]} {
lappend digits [tonum $x]
}
}
if {[string length $digits] >= 1} {
set n "[lindex $digits 0][lindex $digits end]"
incr part2 $n
} else { set n "<invalid>" }
puts "\"$line\": $digits => $n"
}
puts "-"
puts $part2