From f58ed8eb9792dbf8d75b4b5b450a92342089a2ae Mon Sep 17 00:00:00 2001 From: magical Date: Wed, 6 Dec 2023 01:11:46 -0800 Subject: [PATCH] day 4 tcl solution --- day04/sol.tcl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 day04/sol.tcl diff --git a/day04/sol.tcl b/day04/sol.tcl new file mode 100755 index 0000000..408434c --- /dev/null +++ b/day04/sol.tcl @@ -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