day 19 cleanup

main
magical 2023-12-19 07:06:29 +00:00
parent 7b51d4b8dd
commit 61c44b283c
1 changed files with 37 additions and 41 deletions

View File

@ -45,21 +45,7 @@ proc filter {flows value} {
return $f return $f
} }
proc solve input { proc filter-all flows {
set flows [read-workflows stdin]
set values [read-values stdin]
puts $flows
puts $values
foreach v $values {
set f [filter $flows $v]
set score [ladd [lextract $v 2 1]]
puts "$v : $f : $score"
if {$f eq "A"} { incr t $score }
}
puts "$t"
}
proc unfilter flows {
lappend state(in) [dict create x {1 4000} m {1 4000} a {1 4000} s {1 4000}] lappend state(in) [dict create x {1 4000} m {1 4000} a {1 4000} s {1 4000}]
#set state(in) { #set state(in) {
# {x {787 787} m {2655 2655} a {1222 1222} s {2876 2876}} # {x {787 787} m {2655 2655} a {1222 1222} s {2876 2876}}
@ -68,10 +54,11 @@ proc unfilter flows {
# {x {2461 2461} m {1339 1339} a {466 466} s {291 291}} # {x {2461 2461} m {1339 1339} a {466 466} s {291 291}}
# {x {2127 2127} m {1623 1623} a {2188 2188} s {1013 1013}} # {x {2127 2127} m {1623 1623} a {2188 2188} s {1013 1013}}
#} #}
#set flows [dict map {k v} $flows {lmap } set flows [dict create {*}[replace $flows "$" ""]]
#puts $flows
set done 0 set done 0
while {!$done} { while {!$done} {
puts [array get state] #puts [array get state]
# break if everything is in the accept or reject state # break if everything is in the accept or reject state
set done 1 set done 1
foreach {f ranges} [array get state] { foreach {f ranges} [array get state] {
@ -87,8 +74,7 @@ proc unfilter flows {
} }
if {[string first < $cond] >= 0} { if {[string first < $cond] >= 0} {
lassign [split $cond <] var cutoff lassign [split $cond <] var cutoff
set key [trim $var "$"] ;# TODO lassign [dict get $range $var] min max
lassign [dict get $range $key] min max
if {$cutoff > $max} { if {$cutoff > $max} {
# entire range already lower than required # entire range already lower than required
lappend state($dest) $range lappend state($dest) $range
@ -99,14 +85,13 @@ proc unfilter flows {
continue continue
} }
# split range # split range
lappend state($dest) [dict replace $range $key [list $min [expr {$cutoff-1}]]] lappend state($dest) [dict replace $range $var [list $min [expr {$cutoff-1}]]]
dict set range $key [list $cutoff $max] dict set range $var [list $cutoff $max]
continue continue
} }
if {[string first > $cond] >= 0} { if {[string first > $cond] >= 0} {
lassign [split $cond >] var cutoff lassign [split $cond >] var cutoff
set key [trim $var "$"] lassign [dict get $range $var] min max
lassign [dict get $range $key] min max
if {$cutoff < $min} { if {$cutoff < $min} {
# entire range already greater than required # entire range already greater than required
lappend state($dest) $range lappend state($dest) $range
@ -117,8 +102,8 @@ proc unfilter flows {
continue continue
} }
# split range # split range
lappend state($dest) [dict replace $range $key [list [expr {$cutoff+1}] $max]] lappend state($dest) [dict replace $range $var [list [expr {$cutoff+1}] $max]]
dict set range $key [list $min $cutoff] dict set range $var [list $min $cutoff]
continue continue
} }
error "unhandled rule $cond:$dest" error "unhandled rule $cond:$dest"
@ -126,25 +111,36 @@ proc unfilter flows {
} }
} }
} }
set t 0 return $state(A)
foreach r $state(A) { }
set p 1
foreach {min max} [join [dict values $r]] { 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]] {
if {$min > $max} { if {$min > $max} {
error "invalid range: $min $max" error "invalid range: $min $max"
} }
set p [expr {$p * ($max - $min + 1)}] expr {$max - $min + 1}
} }]]
incr t $p incr part2 $combos
puts "A: $r = $p" puts "A: $r = $combos"
} }
puts "$t" puts "part 2 = $part2"
} }
proc solve2 input { solve stdin
set flows [read-workflows stdin]
#set values [read-values stdin]
unfilter $flows
}
solve2 stdin