Day 14 WIP
This commit is contained in:
149
days/14.go
Normal file
149
days/14.go
Normal file
@ -0,0 +1,149 @@
|
||||
package days
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
u "parnic.com/aoc2019/utilities"
|
||||
)
|
||||
|
||||
type reaction struct {
|
||||
inputs map[string]int
|
||||
output u.Pair[string, int]
|
||||
}
|
||||
|
||||
type Day14 struct {
|
||||
reactions []reaction
|
||||
leftovers map[string]int
|
||||
}
|
||||
|
||||
func (d *Day14) Parse() {
|
||||
d.leftovers = make(map[string]int)
|
||||
|
||||
lines := u.GetStringLines("14s2")
|
||||
d.reactions = make([]reaction, len(lines))
|
||||
for i, line := range lines {
|
||||
sides := strings.Split(line, " => ")
|
||||
inputs := strings.Split(sides[0], ", ")
|
||||
output := sides[1]
|
||||
|
||||
outPair := strings.Split(output, " ")
|
||||
outAmt, _ := strconv.Atoi(outPair[0])
|
||||
d.reactions[i].output = u.Pair[string, int]{First: outPair[1], Second: outAmt}
|
||||
d.reactions[i].inputs = make(map[string]int)
|
||||
for _, input := range inputs {
|
||||
pair := strings.Split(input, " ")
|
||||
d.reactions[i].inputs[pair[1]], _ = strconv.Atoi(pair[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d Day14) getReactionProducing(chem string) *reaction {
|
||||
for _, reaction := range d.reactions {
|
||||
if reaction.output.First == chem {
|
||||
return &reaction
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Day14) Num() int {
|
||||
return 14
|
||||
}
|
||||
|
||||
func (d Day14) getOreRequiredFor(chem string, amt int) (int, int) {
|
||||
requiredOre := 0
|
||||
for _, reaction := range d.reactions {
|
||||
if reaction.output.First == chem {
|
||||
// if oreAmt, exists := reaction.inputs["ORE"]; exists && len(reaction.inputs) == 1 {
|
||||
// if d.leftovers[chem] >= amt {
|
||||
// d.leftovers[chem] -= amt
|
||||
// return 0, amt
|
||||
// }
|
||||
|
||||
// produced := reaction.output.Second
|
||||
// for produced < amt {
|
||||
// requiredOre += oreAmt
|
||||
// produced += reaction.output.Second
|
||||
// }
|
||||
// requiredOre += oreAmt
|
||||
// if produced > amt {
|
||||
// d.leftovers[chem] += produced - amt
|
||||
// }
|
||||
// return requiredOre, produced
|
||||
// } else {
|
||||
for inChem, inAmt := range reaction.inputs {
|
||||
produced := 0
|
||||
if d.leftovers[inChem] >= inAmt {
|
||||
d.leftovers[inChem] -= inAmt
|
||||
produced = inAmt
|
||||
} else {
|
||||
for produced < inAmt {
|
||||
madeOre, madeChem := d.getOreRequiredFor(inChem, inAmt)
|
||||
produced += madeChem
|
||||
requiredOre += madeOre
|
||||
}
|
||||
if produced > inAmt {
|
||||
d.leftovers[inChem] += produced - inAmt
|
||||
}
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
return requiredOre, 0
|
||||
}
|
||||
|
||||
func (d *Day14) Part1() string {
|
||||
fuelReaction := d.getReactionProducing("FUEL")
|
||||
if fuelReaction == nil {
|
||||
panic("")
|
||||
}
|
||||
|
||||
neededMaterial := map[string]int{
|
||||
"FUEL": 1,
|
||||
}
|
||||
var recurse func(neededMaterial map[string]int) map[string]int
|
||||
recurse = func(neededMaterial map[string]int) map[string]int {
|
||||
neededInputs := make(map[string]int)
|
||||
for chem, amt := range neededMaterial {
|
||||
reaction := d.getReactionProducing(chem)
|
||||
if reaction == nil {
|
||||
continue
|
||||
}
|
||||
produced := reaction.output.Second
|
||||
reactionsNeeded := 1
|
||||
for produced < amt {
|
||||
produced += reaction.output.Second
|
||||
reactionsNeeded++
|
||||
}
|
||||
for inChem, inAmt := range reaction.inputs {
|
||||
neededInputs[inChem] += inAmt * reactionsNeeded
|
||||
}
|
||||
}
|
||||
if len(neededInputs) > 0 {
|
||||
recursed := recurse(neededInputs)
|
||||
for k, v := range recursed {
|
||||
neededInputs[k] += v
|
||||
}
|
||||
}
|
||||
return neededInputs
|
||||
}
|
||||
recursed := recurse(neededMaterial)
|
||||
fmt.Println(len(recursed))
|
||||
|
||||
ore := 0
|
||||
for inChem, inAmt := range fuelReaction.inputs {
|
||||
requiredOre, _ := d.getOreRequiredFor(inChem, inAmt)
|
||||
ore += requiredOre
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%d%s", u.TextBold, ore, u.TextReset)
|
||||
}
|
||||
|
||||
func (d *Day14) Part2() string {
|
||||
return ""
|
||||
}
|
60
inputs/14p.txt
Normal file
60
inputs/14p.txt
Normal file
@ -0,0 +1,60 @@
|
||||
11 RVCS => 8 CBMDT
|
||||
29 QXPB, 8 QRGRH => 8 LGMKD
|
||||
3 VPRVD => 6 PMFZG
|
||||
1 CNWNQ, 11 MJVXS => 6 SPLM
|
||||
13 SPDRZ, 13 PMFZG => 2 BLFM
|
||||
8 QWPFN => 7 LWVB
|
||||
1 SPLM => 8 TKWQ
|
||||
2 QRGRH, 6 CNWNQ => 7 DTZW
|
||||
2 DMLT, 1 SPLM, 1 TMDK => 9 NKNS
|
||||
1 MJVXS, 1 HLBV => 7 PQCQH
|
||||
1 JZHZP, 9 LWVB => 7 MJSCQ
|
||||
29 DGFR => 7 QRGRH
|
||||
14 XFLKQ, 2 NKNS, 4 KMNJF, 3 MLZGQ, 7 TKWQ, 24 WTDW, 11 CBMDT => 4 GJKX
|
||||
4 TKWQ, 1 WLCFR => 4 PDKGT
|
||||
2 NKNS => 4 GDKL
|
||||
4 WRZST => 9 XFLKQ
|
||||
19 DGFR => 4 VPRVD
|
||||
10 MJSCQ, 4 QWPFN, 4 QXPB => 2 MLZGQ
|
||||
1 JZHZP => 7 QWPFN
|
||||
1 XFLKQ => 9 FQGVL
|
||||
3 GQGXC => 9 VHGP
|
||||
3 NQZTV, 1 JZHZP => 2 NVZWL
|
||||
38 WLCFR, 15 GJKX, 44 LGMKD, 2 CBVXG, 2 GDKL, 77 FQGVL, 10 MKRCZ, 29 WJQD, 33 BWXGC, 19 PQCQH, 24 BKXD => 1 FUEL
|
||||
102 ORE => 5 DGFR
|
||||
17 NWKLB, 1 SBPLK => 5 HRQM
|
||||
3 BWXGC => 8 TQDP
|
||||
1 TQDP => 2 PSZDZ
|
||||
2 MJVXS => 9 WNXG
|
||||
2 NBTW, 1 HRQM => 2 SVHBH
|
||||
8 CNWNQ, 1 DTZW => 4 RVCS
|
||||
4 VHGP, 20 WNXG, 2 SVHBH => 3 SPDRZ
|
||||
110 ORE => 5 TXMC
|
||||
10 QRGRH => 5 NWKLB
|
||||
1 SBPLK => 3 MJVXS
|
||||
9 DGFR => 5 RFSRL
|
||||
5 LBTV => 3 DMLT
|
||||
1 NWKLB, 1 KMNJF, 1 HDQXB, 6 LBTV, 2 PSZDZ, 34 PMFZG, 2 SVHBH => 2 WJQD
|
||||
1 RVCS => 5 MKRCZ
|
||||
14 NQZTV, 3 FPLT, 1 SJMS => 2 GQGXC
|
||||
18 RFSRL, 13 VHGP, 23 NBTW => 5 WTDW
|
||||
1 VHGP, 6 TKWQ => 7 QXPB
|
||||
1 JZHZP, 1 CNWNQ => 5 KMNJF
|
||||
109 ORE => 9 BWXGC
|
||||
2 CNWNQ, 1 PDKGT, 2 KMNJF => 5 HDQXB
|
||||
1 PDKGT, 18 WRZST, 9 MJSCQ, 3 VHGP, 1 BLFM, 1 LGMKD, 7 WLCFR => 2 BKXD
|
||||
11 MLJK => 6 FPLT
|
||||
8 DGFR, 2 TXMC, 3 WJRC => 9 SJMS
|
||||
2 SBPLK => 1 LBTV
|
||||
22 QWPFN => 4 WRZST
|
||||
5 WRZST, 22 WNXG, 1 VHGP => 7 NBTW
|
||||
7 RVCS => 9 TMDK
|
||||
1 DGFR, 14 TXMC => 5 JZHZP
|
||||
2 JZHZP => 3 SBPLK
|
||||
19 PDKGT => 8 HLBV
|
||||
195 ORE => 6 WJRC
|
||||
6 GQGXC => 8 CNWNQ
|
||||
1 NVZWL, 4 GQGXC => 2 CBVXG
|
||||
1 NVZWL, 1 KMNJF => 8 WLCFR
|
||||
153 ORE => 4 MLJK
|
||||
1 BWXGC => 6 NQZTV
|
6
inputs/14s1.txt
Normal file
6
inputs/14s1.txt
Normal file
@ -0,0 +1,6 @@
|
||||
10 ORE => 10 A
|
||||
1 ORE => 1 B
|
||||
7 A, 1 B => 1 C
|
||||
7 A, 1 C => 1 D
|
||||
7 A, 1 D => 1 E
|
||||
7 A, 1 E => 1 FUEL
|
7
inputs/14s2.txt
Normal file
7
inputs/14s2.txt
Normal file
@ -0,0 +1,7 @@
|
||||
9 ORE => 2 A
|
||||
8 ORE => 3 B
|
||||
7 ORE => 5 C
|
||||
3 A, 4 B => 1 AB
|
||||
5 B, 7 C => 1 BC
|
||||
4 C, 1 A => 1 CA
|
||||
2 AB, 3 BC, 4 CA => 1 FUEL
|
9
inputs/14s3.txt
Normal file
9
inputs/14s3.txt
Normal file
@ -0,0 +1,9 @@
|
||||
157 ORE => 5 NZVS
|
||||
165 ORE => 6 DCFZ
|
||||
44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL
|
||||
12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ
|
||||
179 ORE => 7 PSHF
|
||||
177 ORE => 5 HKGWZ
|
||||
7 DCFZ, 7 PSHF => 2 XJWVT
|
||||
165 ORE => 2 GPVTF
|
||||
3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT
|
12
inputs/14s4.txt
Normal file
12
inputs/14s4.txt
Normal file
@ -0,0 +1,12 @@
|
||||
2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG
|
||||
17 NVRVD, 3 JNWZP => 8 VPVL
|
||||
53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL
|
||||
22 VJHF, 37 MNCFX => 5 FWMGM
|
||||
139 ORE => 4 NVRVD
|
||||
144 ORE => 7 JNWZP
|
||||
5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC
|
||||
5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV
|
||||
145 ORE => 6 MNCFX
|
||||
1 NVRVD => 8 CXFTF
|
||||
1 VJHF, 6 MNCFX => 4 RFSQX
|
||||
176 ORE => 6 VJHF
|
17
inputs/14s5.txt
Normal file
17
inputs/14s5.txt
Normal file
@ -0,0 +1,17 @@
|
||||
171 ORE => 8 CNZTR
|
||||
7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL
|
||||
114 ORE => 4 BHXH
|
||||
14 VRPVC => 6 BMBT
|
||||
6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL
|
||||
6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT
|
||||
15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW
|
||||
13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW
|
||||
5 BMBT => 4 WPTQ
|
||||
189 ORE => 9 KTJDG
|
||||
1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP
|
||||
12 VRPVC, 27 CNZTR => 2 XDBXC
|
||||
15 KTJDG, 12 BHXH => 5 XCVML
|
||||
3 BHXH, 2 VRPVC => 7 MZWV
|
||||
121 ORE => 7 VRPVC
|
||||
7 XCVML => 6 RJRHP
|
||||
5 BHXH, 4 VRPVC => 5 LTCX
|
Reference in New Issue
Block a user