day 1 part 2 tcl solution

main
magical 2023-12-06 03:40:44 +00:00
parent 172a8a9d87
commit cfdab32f3f
1 changed files with 28 additions and 0 deletions

28
day01/sol.tcl 100755
View File

@ -0,0 +1,28 @@
#!/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