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