diff --git a/p02.janet b/p02.janet index 84fc1ca..636c44e 100755 --- a/p02.janet +++ b/p02.janet @@ -24,7 +24,7 @@ [subsets pred] (all (fn [subset] (subset-pred subset pred)) subsets)) -(def die-pool +(def *die-pool* @{:red 12 :green 13 :blue 14}) @@ -33,7 +33,19 @@ "Check if die fits in the die pool" [pair] (var [count color] pair) - (<= count (die-pool color))) + (<= count (*die-pool* color))) + +(defn create-pool [init] + @{:red init + :green init + :blue init}) + +(defn create-pool-walker + "Build up the die pool" + [pool] + (fn [pair] + (var [count color] pair) + (set (pool color) (max (pool color) count)))) (defn all-possible "Check if all fit the pool" @@ -41,14 +53,16 @@ (all-pred subsets possible)) (defn solve [lines] - (var sum 0) + (defn calc-power [pool] + (* (splice (values pool)))) + (var power-sum 0) (each line lines + (var pool (create-pool 0)) (var [game subsets] (pmatch line)) - (if (all-possible subsets) - (do - (print "Game " game " is good") - (+= sum game)))) - sum) + (all-pred subsets (create-pool-walker pool)) + (pp pool) + (+= power-sum (calc-power pool))) + (print power-sum)) (start-suite)