Browse Source

Day 14

master
Chris Smith 4 years ago
parent
commit
4557482f97
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
3 changed files with 163 additions and 0 deletions
  1. 2
    0
      14/answers.txt
  2. 59
    0
      14/input.txt
  3. 102
    0
      14/main.go

+ 2
- 0
14/answers.txt View File

@@ -0,0 +1,2 @@
1
+431448
2
+3279311

+ 59
- 0
14/input.txt View File

@@ -0,0 +1,59 @@
1
+2 RWPCH => 9 PVTL
2
+1 FHFH => 4 BLPJK
3
+146 ORE => 5 VJNBT
4
+8 KDFNZ, 1 ZJGH, 1 GSCG => 5 LKPQG
5
+11 NWDZ, 2 WBQR, 1 VRQR => 2 BMJR
6
+3 GSCG => 4 KQDVM
7
+5 QVNKN, 6 RPWKC => 3 BCNV
8
+10 QMBM, 4 RBXB, 2 VRQR => 1 JHXBM
9
+15 RPWKC => 6 MGCQ
10
+1 QWKRZ => 4 FHFH
11
+10 RWPCH => 6 MZKG
12
+11 JFKGV, 5 QVNKN, 1 CTVK => 4 VQDT
13
+1 SXKT => 5 RPWKC
14
+1 VQDT, 25 ZVMCB => 2 RBXB
15
+6 LGLNV, 4 XSNKB => 3 WBQR
16
+199 ORE => 2 SXKT
17
+1 XSNKB, 6 CWBNX, 1 HPKB, 5 PVTL, 1 JNKH, 9 SXKT, 3 KQDVM => 3 ZKTX
18
+7 FDSX => 6 WJDF
19
+7 JLRM => 4 CWBNX
20
+167 ORE => 5 PQZXH
21
+13 JHXBM, 2 NWDZ, 4 RFLX, 12 VRQR, 10 FJRFG, 14 PVTL, 2 JLRM => 6 DGFG
22
+12 HPKB, 3 WHVXC => 9 ZJGH
23
+1 JLRM, 2 ZJGH, 2 QVNKN => 9 FJRFG
24
+129 ORE => 7 KZFPJ
25
+2 QMBM => 1 RWPCH
26
+7 VJMWM => 4 JHDW
27
+7 PQZXH, 7 SXKT => 9 BJVQM
28
+1 VJMWM, 4 JHDW, 1 MQXF => 7 FDSX
29
+1 RPWKC => 7 WHVXC
30
+1 ZJGH => 1 ZVMCB
31
+1 RWPCH => 3 MPKR
32
+187 ORE => 8 VJMWM
33
+15 CTVK => 5 GSCG
34
+2 XSNKB, 15 ZVMCB, 3 KDFNZ => 2 RFLX
35
+18 QVNKN => 8 XLFZJ
36
+4 CWBNX => 8 ZSCX
37
+2 ZJGH, 1 JLRM, 1 MGCQ => 9 NPRST
38
+13 BJVQM, 2 BCNV => 2 QWKRZ
39
+2 QWKRZ, 2 BLPJK, 5 XSNKB => 2 VRQR
40
+13 HPKB, 3 VQDT => 9 JLRM
41
+2 SXKT, 1 VJNBT, 5 VLWQB => 6 CTVK
42
+2 MPKR, 2 LMNCH, 24 VRQR => 8 DZFNW
43
+2 VQDT => 1 KDFNZ
44
+1 CTVK, 6 FDSX => 6 QVNKN
45
+3 CTVK, 1 QVNKN => 4 HPKB
46
+3 NPRST, 1 KGSDJ, 1 CTVK => 2 QMBM
47
+4 KZFPJ, 1 PQZXH => 5 VLWQB
48
+2 VQDT => 7 KGSDJ
49
+3 MPKR => 2 JNKH
50
+1 KQDVM => 5 XQBS
51
+3 ZKGMX, 1 XQBS, 11 MZKG, 11 NPRST, 1 DZFNW, 5 VQDT, 2 FHFH => 6 JQNF
52
+2 FJRFG, 17 BMJR, 3 BJVQM, 55 JQNF, 8 DGFG, 13 ZJGH, 29 ZKTX => 1 FUEL
53
+27 KZFPJ, 5 VJNBT => 5 MQXF
54
+11 FDSX, 1 WHVXC, 1 WJDF => 4 ZKGMX
55
+1 ZVMCB => 4 NWDZ
56
+1 XLFZJ => 6 LGLNV
57
+13 ZSCX, 4 XLFZJ => 8 LMNCH
58
+1 RPWKC, 1 FDSX, 2 BJVQM => 8 JFKGV
59
+1 WJDF, 1 LKPQG => 4 XSNKB

+ 102
- 0
14/main.go View File

@@ -0,0 +1,102 @@
1
+package main
2
+
3
+import (
4
+	"github.com/csmith/aoc-2019/common"
5
+	"math"
6
+	"strings"
7
+)
8
+
9
+type chemical string
10
+type recipeBook map[chemical]*recipe
11
+
12
+type amount struct {
13
+	chemical chemical
14
+	quantity int
15
+}
16
+
17
+type recipe struct {
18
+	produces *amount
19
+	requires []*amount
20
+}
21
+
22
+func parseAmount(text string) *amount {
23
+	parts := strings.Split(text, " ")
24
+	return &amount{
25
+		quantity: common.MustAtoi(parts[0]),
26
+		chemical: chemical(parts[1]),
27
+	}
28
+}
29
+
30
+func parseInput(file string) recipeBook {
31
+	recipes := make(map[chemical]*recipe)
32
+	for _, line := range common.ReadFileAsStrings(file) {
33
+		parts := strings.Split(line, " => ")
34
+		ingredients := strings.Split(parts[0], ", ")
35
+		r := &recipe{
36
+			produces: parseAmount(parts[1]),
37
+			requires: make([]*amount, 0, len(ingredients)),
38
+		}
39
+		for _, in := range ingredients {
40
+			r.requires = append(r.requires, parseAmount(in))
41
+		}
42
+		recipes[r.produces.chemical] = r
43
+	}
44
+	return recipes
45
+}
46
+
47
+func (rb recipeBook) produce(target *amount, used map[chemical]int, spare map[chemical]int) {
48
+	recipe := rb[target.chemical]
49
+	needed := target.quantity
50
+
51
+	if recipe == nil {
52
+		used[target.chemical] += needed
53
+		return
54
+	}
55
+
56
+	free := spare[target.chemical]
57
+	if free >= needed {
58
+		used[target.chemical] += needed
59
+		spare[target.chemical] -= needed
60
+		return
61
+	} else {
62
+		used[target.chemical] += free
63
+		spare[target.chemical] = 0
64
+		needed -= free
65
+	}
66
+
67
+	runs := int(math.Ceil(float64(needed) / float64(recipe.produces.quantity)))
68
+	for _, i := range recipe.requires {
69
+		rb.produce(&amount{
70
+			chemical: i.chemical,
71
+			quantity: i.quantity * runs,
72
+		}, used, spare)
73
+	}
74
+
75
+	used[target.chemical] += needed
76
+	spare[target.chemical] += (recipe.produces.quantity * runs) - needed
77
+}
78
+
79
+func main() {
80
+	recipes := parseInput("14/input.txt")
81
+	used := make(map[chemical]int, len(recipes))
82
+	spare := make(map[chemical]int, len(recipes))
83
+
84
+	recipes.produce(&amount{
85
+		chemical: "FUEL",
86
+		quantity: 1,
87
+	}, used, spare)
88
+
89
+	orePerFuel := used["ORE"]
90
+	println(used["ORE"])
91
+
92
+	last := 0
93
+	for used["ORE"] < 1000000000000 {
94
+		last = used["FUEL"]
95
+		recipes.produce(&amount{
96
+			chemical: "FUEL",
97
+			quantity: common.Max(1, (1000000000000-used["ORE"])/orePerFuel),
98
+		}, used, spare)
99
+	}
100
+
101
+	println(last)
102
+}

Loading…
Cancel
Save