Posted to tcl by libertas at Mon Dec 07 22:20:32 GMT 2020view raw

  1. # Error:
  2. # 0 -> dark blue contains: {2 {dark violet}}
  3. # .... 2 .... dark violet
  4. # 0 -> dark violet contains: {0 {other bags.}}
  5. # .... 0 .... other bags.
  6. # 7 !!!!!!!!!!!!!!!!!!
  7. # can't use empty string as operand of "*"
  8. # while executing
  9. # "expr {$::cnt + $nr + $nr * [compute2 $b]}"
  10. # (procedure "compute2" line 9)
  11.  
  12. #!/usr/bin/env tclsh
  13.  
  14.  
  15. set lall [list {shiny gold} {{2 {dark red}} {2 {dark pink}}} {dark red} {{2 {dark orange}}} {dark orange} {{2 {dark yellow}}} {dark yellow} {{2 {dark green}}} {dark green} {{2 {dark blue}}} {dark blue} {{2 {dark violet}}} {dark violet} {{0 {other bags.}}} {dark pink} {{2 {dark violet}}}]
  16. set all [dict create {*}$lall]
  17.  
  18. set cnt 0
  19. proc compute2 {tp} {
  20. set contains [dict get $::all $tp]
  21. puts "$::cnt -> $tp contains: $contains "
  22. foreach bagset $contains {
  23. set nr [lindex $bagset 0]
  24. set b [lindex $bagset 1]
  25. puts " .... $nr .... $b"
  26. if {$nr != 0} {
  27. set ::cnt [expr {$::cnt + $nr + $nr * [compute2 $b]}]
  28. } else {puts "[info level] !!!!!!!!!!!!!!!!!!"; return 0}
  29. }
  30. }
  31.  
  32. puts ">> [compute2 "shiny gold"]"
  33.  
  34. puts "day 07 part2: $cnt"