Compare commits
2 Commits
f1e2dc0d24
...
f58ed8eb97
Author | SHA1 | Date |
---|---|---|
magical | f58ed8eb97 | |
magical | 0fb7b77afe |
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env tclsh
|
||||
source ../prelude.tcl
|
||||
set input stdin
|
||||
set total 0
|
||||
set power 0
|
||||
while {[gets $input line] >= 0} {
|
||||
regexp {Game (\d+): (.*)} $line _ num takes
|
||||
set min(red) 0
|
||||
set min(blue) 0
|
||||
set min(green) 0
|
||||
foreach t [split $takes ";"] {
|
||||
foreach {n color} [string map {, {}} $t] {
|
||||
set min($color) [max $min($color) $n]
|
||||
}
|
||||
}
|
||||
if {$min(red) <= 12 && $min(blue) <= 14 && $min(green) <= 13} {
|
||||
incr total $num
|
||||
}
|
||||
incr power [expr {$min(red) * $min(blue) * $min(green)}]
|
||||
#puts "${min(red)} ${min(blue)} ${min(green)}"
|
||||
}
|
||||
puts $total
|
||||
puts $power
|
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env tclsh
|
||||
source ../prelude.tcl
|
||||
package require struct::set
|
||||
set input stdin
|
||||
|
||||
set part1 0
|
||||
while {[gets $input line] >= 0} {
|
||||
regexp {^Card +(\d+): ([\d ]*) \| ([\d ]*)$} $line _ card winners numbers
|
||||
set matches [struct::set intersect $winners $numbers]
|
||||
set score [expr {1 << [llength $matches] >> 1}]
|
||||
incr part1 $score
|
||||
|
||||
incr copies($card)
|
||||
for {set i 0} {$i < [llength $matches]} {incr i} {
|
||||
incr copies([expr {$card+$i+1}]) $copies($card)
|
||||
}
|
||||
}
|
||||
puts "-"
|
||||
puts $part1
|
||||
|
||||
foreach {c n} [array get copies] {
|
||||
if {$c > $card} {
|
||||
error "should not have any copies of card $c > $card"
|
||||
}
|
||||
}
|
||||
|
||||
set part2 [ladd [lmap {card n} [array get copies] {list $n}]]
|
||||
puts $part2
|
|
@ -0,0 +1,16 @@
|
|||
package require Tcl 8.6
|
||||
namespace import tcl::mathfunc::min
|
||||
namespace import tcl::mathfunc::max
|
||||
|
||||
proc ladd {list} {
|
||||
set t 0
|
||||
foreach x $list { incr t $x }
|
||||
return $t
|
||||
}
|
||||
|
||||
proc lmul {list} {
|
||||
set p 1
|
||||
foreach x $list { set p [eval {$p * $x}] }
|
||||
return $p
|
||||
}
|
||||
|
Loading…
Reference in New Issue