day 1 part 2 tcl solution
parent
172a8a9d87
commit
cfdab32f3f
|
@ -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
|
Loading…
Reference in New Issue