Day 14 & 15 progress
This commit is contained in:
61
2019/day14/input
Normal file
61
2019/day14/input
Normal file
@@ -0,0 +1,61 @@
|
||||
4 SRWZ, 3 ZGSFW, 1 HVJVQ, 1 RWDSX, 12 BDHX, 1 GDPKF, 23 WFPSM, 1 MPKC => 6 VCWNW
|
||||
3 BXVJK, 3 WTPN => 4 GRQC
|
||||
5 KWFD => 9 NMZND
|
||||
1 DNZQ, 5 CDSP => 3 PFDBV
|
||||
4 VSPSC, 34 MPKC, 9 DFNVL => 9 PZWSP
|
||||
5 NTXHM => 9 DBKN
|
||||
4 JNSP, 4 TCKR, 7 PZWSP => 7 DLHG
|
||||
12 CNBS, 3 FNPC => 2 SRWZ
|
||||
3 RWDSX, 4 NHSTB, 2 JNSP => 8 TCKR
|
||||
24 PGHF, 1 NMZND => 3 RWDSX
|
||||
1 DLHG => 9 QSVN
|
||||
6 HVJVQ => 2 QSNCW
|
||||
4 CHDTJ => 9 FDVNC
|
||||
1 HBXF, 1 RWDSX => 7 BWSPN
|
||||
2 ZGSFW, 1 KWFD => 8 JNSP
|
||||
2 BWSPN, 7 GDPKF, 1 BXVJK => 6 FVQM
|
||||
2 MHBH => 6 FNPC
|
||||
2 WTPN, 15 GRQC => 3 ZGSFW
|
||||
9 LXMLX => 6 CLZT
|
||||
5 DFNVL, 1 KHCQ => 4 MHLBR
|
||||
21 CNTFK, 3 XHST => 9 CHDTJ
|
||||
1 CNTFK => 7 MHBH
|
||||
1 GMQDW, 34 GDPKF, 2 ZDGPL, 1 HVJVQ, 13 QSVN, 1 QSNCW, 1 BXVJK => 2 SGLGN
|
||||
1 BMVRK, 1 XHST => 8 XHLNT
|
||||
23 CXKN => 1 BDKN
|
||||
121 ORE => 9 XHST
|
||||
4 NTXHM, 4 FNPC, 15 VCMVN => 8 MPKC
|
||||
2 ZDGPL, 7 JNSP, 3 FJVMD => 4 GMQDW
|
||||
1 LXMLX, 2 BWSPN => 2 DNZQ
|
||||
6 WTPN => 9 KCMH
|
||||
20 CDSP => 2 VSPSC
|
||||
2 QSNCW, 1 BDHX, 3 HBXF, 8 PFDBV, 17 ZDGPL, 1 MHLBR, 9 ZGSFW => 8 FDWSG
|
||||
2 VSFTG, 2 DLHG => 9 BDHX
|
||||
174 ORE => 5 BMVRK
|
||||
2 BMVRK => 2 KWFD
|
||||
3 WTPN, 9 TVJPG => 9 CDSP
|
||||
191 ORE => 2 CNTFK
|
||||
9 FDVNC, 1 MHBH => 8 NTXHM
|
||||
3 NHSTB, 2 BXVJK, 1 JNSP => 1 WFPSM
|
||||
7 FJVMD => 9 CXKN
|
||||
3 GDPKF, 10 QSNCW => 7 ZDGPL
|
||||
7 LPXM, 11 VSPSC => 1 LXMLX
|
||||
6 RWDSX, 2 NMZND, 1 MPKC => 1 KHCQ
|
||||
6 RWDSX => 4 QMJK
|
||||
15 MHBH, 28 DBKN, 12 CNBS => 4 PGHF
|
||||
20 NMZND, 1 PGHF, 1 BXVJK => 2 LPXM
|
||||
1 CDSP, 17 BXVJK => 5 NHSTB
|
||||
12 HVJVQ => 3 VSFTG
|
||||
2 PGHF, 3 VCMVN, 2 NHSTB => 1 DFNVL
|
||||
5 FNPC => 9 HBXF
|
||||
3 DPRL => 4 FJVMD
|
||||
1 KWFD, 1 TVJPG => 8 VCMVN
|
||||
1 FDWSG, 1 VCWNW, 4 BDKN, 14 FDVNC, 1 CLZT, 62 SGLGN, 5 QMJK, 26 ZDGPL, 60 KCMH, 32 FVQM, 15 SRWZ => 1 FUEL
|
||||
3 XHLNT => 8 TVJPG
|
||||
5 HBXF => 2 HVJVQ
|
||||
3 CHDTJ, 15 KWFD => 9 WTPN
|
||||
7 CNTFK => 7 CNBS
|
||||
1 CNBS => 2 JPDF
|
||||
5 JNSP => 8 DPRL
|
||||
11 NTXHM => 8 GDPKF
|
||||
10 JPDF => 9 BXVJK
|
153
2019/day14/main.go
Normal file
153
2019/day14/main.go
Normal file
@@ -0,0 +1,153 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := helpers.StdinToStringSlice()
|
||||
part1(inp)
|
||||
part2(inp)
|
||||
}
|
||||
|
||||
var allReactions map[string]*Reaction
|
||||
|
||||
func part1(inp []string) {
|
||||
allReactions = make(map[string]*Reaction)
|
||||
for _, v := range inp {
|
||||
r := NewReaction(v)
|
||||
allReactions[r.out] = r
|
||||
}
|
||||
factory := NewFactory(allReactions)
|
||||
factory.setInfinite("ORE", true)
|
||||
factory.generate("FUEL")
|
||||
fmt.Println("# Part 1")
|
||||
fmt.Println("Ore Used:", factory.generated["ORE"])
|
||||
}
|
||||
|
||||
func part2(inp []string) {
|
||||
allReactions = make(map[string]*Reaction)
|
||||
for _, v := range inp {
|
||||
r := NewReaction(v)
|
||||
allReactions[r.out] = r
|
||||
}
|
||||
factory := NewFactory(allReactions)
|
||||
oreStart := 1000000000000
|
||||
factory.setStock("ORE", oreStart)
|
||||
fmt.Println("\n# Part 2")
|
||||
fmt.Println("Ore in stock", factory.stock["ORE"])
|
||||
var out1 string
|
||||
for factory.generate("FUEL") {
|
||||
clearStr := strings.Repeat("", len(out1))
|
||||
out1 = fmt.Sprintf("%s%3d%% (%d)", clearStr, ((factory.stock["ORE"] * 100) / oreStart), factory.generated["FUEL"])
|
||||
fmt.Print(out1)
|
||||
}
|
||||
}
|
||||
|
||||
type Factory struct {
|
||||
stock map[string]int
|
||||
recipes map[string]*Reaction
|
||||
generated map[string]int
|
||||
used map[string]int
|
||||
infinite map[string]bool
|
||||
}
|
||||
|
||||
func NewFactory(recipes map[string]*Reaction) *Factory {
|
||||
f := Factory{
|
||||
stock: make(map[string]int),
|
||||
recipes: recipes,
|
||||
generated: make(map[string]int),
|
||||
used: make(map[string]int),
|
||||
infinite: make(map[string]bool),
|
||||
}
|
||||
return &f
|
||||
}
|
||||
|
||||
func (f *Factory) setStock(chem string, amt int) {
|
||||
f.stock[chem] = amt
|
||||
}
|
||||
|
||||
func (f *Factory) setInfinite(chem string, is bool) {
|
||||
f.infinite[chem] = is
|
||||
}
|
||||
|
||||
func (f *Factory) count(chem string) int {
|
||||
return f.stock[chem]
|
||||
}
|
||||
|
||||
func (f *Factory) use(chem string, amt int) bool {
|
||||
if f.count(chem) < amt {
|
||||
return false
|
||||
}
|
||||
f.stock[chem] = f.stock[chem] - amt
|
||||
f.used[chem] = f.used[chem] + amt
|
||||
return true
|
||||
}
|
||||
|
||||
func (f *Factory) generate(chem string) bool {
|
||||
if f.infinite[chem] {
|
||||
f.stock[chem] = f.stock[chem] + 1
|
||||
f.generated[chem] = f.generated[chem] + 1
|
||||
return true
|
||||
}
|
||||
react, ok := f.recipes[chem]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
for k, v := range react.in {
|
||||
for f.count(k) < v {
|
||||
f.generate(k)
|
||||
}
|
||||
f.use(k, v)
|
||||
}
|
||||
f.stock[chem] = f.stock[chem] + react.outQty
|
||||
f.generated[chem] = f.generated[chem] + react.outQty
|
||||
return true
|
||||
}
|
||||
|
||||
type Reaction struct {
|
||||
in map[string]int
|
||||
out string
|
||||
outQty int
|
||||
|
||||
ingredients []*Reaction
|
||||
}
|
||||
|
||||
func NewReaction(inp string) *Reaction {
|
||||
pts := strings.Split(inp, " => ")
|
||||
if len(pts) != 2 {
|
||||
panic("Invalid Reaction Input: " + inp)
|
||||
}
|
||||
ret := Reaction{
|
||||
in: make(map[string]int),
|
||||
}
|
||||
inps := strings.Split(pts[0], ", ")
|
||||
for k := range inps {
|
||||
var qty int
|
||||
var prod string
|
||||
_, err := fmt.Sscanf(inps[k], "%d %s", &qty, &prod)
|
||||
if err != nil {
|
||||
panic("Invalid Reaction Input: " + inp)
|
||||
}
|
||||
ret.in[prod] = qty
|
||||
}
|
||||
_, err := fmt.Sscanf(pts[1], "%d %s", &ret.outQty, &ret.out)
|
||||
if err != nil {
|
||||
panic("Invalid Reaction Input: " + inp)
|
||||
}
|
||||
return &ret
|
||||
}
|
||||
|
||||
func (r Reaction) String() string {
|
||||
var ret string
|
||||
for k, v := range r.in {
|
||||
if len(ret) > 0 {
|
||||
ret = ret + ","
|
||||
}
|
||||
ret = fmt.Sprintf("%s %d %s", ret, v, k)
|
||||
}
|
||||
return fmt.Sprintf("%s => %d %s", ret, r.outQty, r.out)
|
||||
}
|
6
2019/day14/testinput1
Normal file
6
2019/day14/testinput1
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
2019/day14/testinput2
Normal file
7
2019/day14/testinput2
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
2019/day14/testinput3
Normal file
9
2019/day14/testinput3
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
|
Reference in New Issue
Block a user