-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.nim
59 lines (47 loc) · 1.67 KB
/
day15.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import std/[algorithm, sequtils, sugar, strscans]
type
Teaspoons = int
Score = int
Ingredient = object
name: string
cap: int # capacity
dur: int # durability
fla: int # flavor
txr: int # texture
cal: int # calories
func listedProps(i: Ingredient): seq[int] =
result = @[i.cap, i.dur, i.fla, i.txr]
when IsPart2:
result.add(i.cal)
func parse(properties: string): Ingredient =
var i: Ingredient
const pattern = "$w: capacity $i, durability $i, flavor $i, texture $i, calories $i$."
discard scanf(properties, pattern, i.name, i.cap, i.dur, i.fla, i.txr, i.cal)
i
proc bestCookieScore(kitchenIngredients: string): Score =
var ingredients = kitchenIngredients.splitLines.map(x => x.parse)
var mix: seq[seq[Score]]
for _ in ingredients:
mix.add(toSeq(1 ..< 100))
for teaspoons in product(mix):
const MaxTeaspoons: Teaspoons = 100
if teaspoons.foldl(a + b) != MaxTeaspoons: continue
let props = toSeq(ingredients.pairs)
.map(pair => pair.val.listedProps.map(x => x * teaspoons[pair.key]))
.foldl(a + b)
.map(x => x.max(0))
when IsPart1:
result = result.max(props.foldl(a * b))
else:
const CalRequirement = 500
if props[^1] != CalRequirement: continue
result = result.max(props[0 ..< ^1].foldl(a * b))
when isMainModule:
echo fmt"Part {P}: {stdin.readAll.strip.bestCookieScore}"
static:
const example = """Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3"""
when IsPart1:
assert example.bestCookieScore == 62842880
else:
assert example.bestCookieScore == 57600000