Part 2 of day 2
This commit is contained in:
parent
78311953c7
commit
f499c61f7b
1 changed files with 22 additions and 8 deletions
30
p02.janet
30
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)
|
||||
|
|
Loading…
Reference in a new issue