adventofcode2023/day19/sol.tcl

147 lines
4.8 KiB
Tcl
Raw Normal View History

2023-12-19 05:31:27 +00:00
#!/usr/bin/env tclsh
source ../prelude.tcl
proc read-workflows input {
set flows [dict create]
while {[gets $input line] >= 0} {
if {$line eq ""} break
set line [string map [list \{ @ \} @] $line]
must_regexp {([a-z]+)@([^@]*)@} $line _ name rulestr
set rules {}
foreach r [split $rulestr ","] {
if {[string first : $r] >= 0} {
lassign [split $r :] cond dest
set cond [regsub -all {([xmas])} $cond {$\1}]
lappend rules $cond $dest
} else {
lappend rules default $r
}
}
dict set flows $name $rules
}
return $flows
}
proc read-values input {
set values {}
while {[gets $input line] >= 0} {
set line [trim [regsub -all {[^a-z0-9]} $line { }]]
lappend values [dict create {*}[split $line]]
}
return $values
}
proc filter {flows value} {
set f in
while {[dict exists $flows $f]} {
set rules [dict get $flows $f]
foreach {cond dest} $rules {
if {$cond eq "default" || [dict with value {expr $cond}]} {
set f $dest
break
}
}
}
return $f
}
2023-12-19 07:06:29 +00:00
proc filter-all flows {
2023-12-19 06:38:56 +00:00
lappend state(in) [dict create x {1 4000} m {1 4000} a {1 4000} s {1 4000}]
#set state(in) {
# {x {787 787} m {2655 2655} a {1222 1222} s {2876 2876}}
# {x {1679 1679} m {44 44} a {2067 2067} s {496 496}}
# {x {2036 2036} m {264 264} a {79 79} s {2244 2244}}
# {x {2461 2461} m {1339 1339} a {466 466} s {291 291}}
# {x {2127 2127} m {1623 1623} a {2188 2188} s {1013 1013}}
#}
2023-12-19 07:06:29 +00:00
set flows [dict create {*}[replace $flows "$" ""]]
#puts $flows
2023-12-19 06:38:56 +00:00
set done 0
while {!$done} {
2023-12-19 07:06:29 +00:00
#puts [array get state]
2023-12-19 06:38:56 +00:00
# break if everything is in the accept or reject state
set done 1
foreach {f ranges} [array get state] {
if {$f eq "A" || $f eq "R"} continue
set done 0
unset state($f)
foreach range $ranges {
set rules [dict get $flows $f]
foreach {cond dest} $rules {
if {$cond eq "default"} {
lappend state($dest) $range
break
}
if {[string first < $cond] >= 0} {
lassign [split $cond <] var cutoff
2023-12-19 07:06:29 +00:00
lassign [dict get $range $var] min max
2023-12-19 06:38:56 +00:00
if {$cutoff > $max} {
# entire range already lower than required
lappend state($dest) $range
break
}
if {$min >= $cutoff} {
# entire range above required value
continue
}
# split range
2023-12-19 07:06:29 +00:00
lappend state($dest) [dict replace $range $var [list $min [expr {$cutoff-1}]]]
dict set range $var [list $cutoff $max]
2023-12-19 06:38:56 +00:00
continue
}
if {[string first > $cond] >= 0} {
lassign [split $cond >] var cutoff
2023-12-19 07:06:29 +00:00
lassign [dict get $range $var] min max
2023-12-19 06:38:56 +00:00
if {$cutoff < $min} {
# entire range already greater than required
lappend state($dest) $range
break
}
if {$max <= $cutoff} {
# entire range below required value
continue
}
# split range
2023-12-19 07:06:29 +00:00
lappend state($dest) [dict replace $range $var [list [expr {$cutoff+1}] $max]]
dict set range $var [list $min $cutoff]
2023-12-19 06:38:56 +00:00
continue
}
error "unhandled rule $cond:$dest"
}
}
}
}
2023-12-19 07:06:29 +00:00
return $state(A)
}
proc solve input {
set flows [read-workflows $input]
set values [read-values $input]
#puts $flows
puts $values
# part 1
foreach v $values {
set f [filter $flows $v]
set score [ladd [lextract $v 2 1]]
puts "$v : $f : $score"
if {$f eq "A"} { incr part1 $score }
}
puts "part 1 = $part1"
# part 2
foreach r [filter-all $flows] {
set combos [lmul [lmap {min max} [join [dict values $r]] {
2023-12-19 06:38:56 +00:00
if {$min > $max} {
error "invalid range: $min $max"
}
2023-12-19 07:06:29 +00:00
expr {$max - $min + 1}
}]]
incr part2 $combos
puts "A: $r = $combos"
2023-12-19 06:38:56 +00:00
}
2023-12-19 07:06:29 +00:00
puts "part 2 = $part2"
2023-12-19 06:38:56 +00:00
}
2023-12-19 07:06:29 +00:00
solve stdin