adventofcode2023/day12/sol.tcl

43 lines
864 B
Tcl
Executable File

#!/usr/bin/env tclsh
source ../prelude.tcl
set input stdin
proc expand {row {start 0}} {
set i [string first {?} $row $start]
if {$i < 0} {
return [list $row]
}
set a [string replace $row $i $i "."]
set b [string replace $row $i $i "#"]
incr i
return [concat [expand $a $i] [expand $b $i]]
}
proc match {row nums} {
#puts -nonewline " $row -> "
set row [string map {. " "} $row]
#puts " $row"
foreach group $row n $nums {
if {[string length $group] != $n} {
return 0
}
}
return 1
}
set part1 0
while {[gets $input line] >= 0} {
set n 0
lassign $line row nums
set nums [split $nums ","]
foreach x [expand $row] {
#puts $x
if {[match $x $nums]} {
incr n
}
}
puts "$n | $row $nums"
incr part1 $n
}
puts $part1