Day 14 & 15 progress

This commit is contained in:
Brian Buller 2019-12-15 08:57:44 -06:00
parent 74510f9723
commit a3cee63862
9 changed files with 336 additions and 2 deletions

View File

@ -15,7 +15,7 @@ import (
var auto bool
func main() {
progFileName := "finishinput"
progFileName := "input"
if len(os.Args) > 1 && os.Args[1] == "-auto" {
auto = true
@ -90,7 +90,6 @@ func play(prog []int) {
}
}
}
//lookForScore(p, score)
switch p.State() {
case intcode.RET_ERR:
panic(p.Error())

61
2019/day14/input Normal file
View 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
View 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
View 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
View 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
View 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

1
2019/day15/input Normal file
View File

@ -0,0 +1 @@
3,1033,1008,1033,1,1032,1005,1032,31,1008,1033,2,1032,1005,1032,58,1008,1033,3,1032,1005,1032,81,1008,1033,4,1032,1005,1032,104,99,102,1,1034,1039,1002,1036,1,1041,1001,1035,-1,1040,1008,1038,0,1043,102,-1,1043,1032,1,1037,1032,1042,1105,1,124,102,1,1034,1039,101,0,1036,1041,1001,1035,1,1040,1008,1038,0,1043,1,1037,1038,1042,1105,1,124,1001,1034,-1,1039,1008,1036,0,1041,101,0,1035,1040,1001,1038,0,1043,101,0,1037,1042,1105,1,124,1001,1034,1,1039,1008,1036,0,1041,101,0,1035,1040,101,0,1038,1043,101,0,1037,1042,1006,1039,217,1006,1040,217,1008,1039,40,1032,1005,1032,217,1008,1040,40,1032,1005,1032,217,1008,1039,5,1032,1006,1032,165,1008,1040,9,1032,1006,1032,165,1102,1,2,1044,1105,1,224,2,1041,1043,1032,1006,1032,179,1102,1,1,1044,1105,1,224,1,1041,1043,1032,1006,1032,217,1,1042,1043,1032,1001,1032,-1,1032,1002,1032,39,1032,1,1032,1039,1032,101,-1,1032,1032,101,252,1032,211,1007,0,73,1044,1106,0,224,1101,0,0,1044,1106,0,224,1006,1044,247,101,0,1039,1034,1002,1040,1,1035,1002,1041,1,1036,1002,1043,1,1038,101,0,1042,1037,4,1044,1105,1,0,43,57,94,36,95,30,10,40,88,72,99,97,53,21,87,48,77,40,75,69,46,98,78,22,21,38,17,12,96,34,94,81,18,49,92,1,26,67,48,15,80,51,60,92,9,77,89,64,15,85,53,94,84,99,70,7,8,69,79,79,41,62,98,22,94,92,69,97,65,96,47,99,71,4,75,10,89,85,13,89,93,93,33,46,80,61,80,75,47,99,54,63,54,57,99,80,97,77,48,33,97,95,92,20,75,3,90,84,1,50,15,94,80,95,93,70,22,3,74,69,27,99,91,66,99,1,67,12,94,31,78,83,51,97,25,4,92,85,3,96,60,5,98,69,23,95,70,92,99,1,5,84,51,87,60,67,56,98,44,80,71,81,59,58,97,82,48,87,4,76,87,45,23,75,62,89,29,37,83,22,89,81,48,64,92,30,13,90,89,83,50,49,14,89,2,34,39,84,88,21,1,81,41,74,95,89,37,82,30,87,11,93,78,67,99,8,95,84,26,93,9,95,7,18,93,94,55,96,50,92,97,43,88,53,22,91,91,35,5,79,34,66,56,24,95,49,86,72,98,52,19,81,10,90,78,12,76,8,37,87,62,80,98,52,19,40,97,83,70,18,94,77,62,87,13,35,90,35,78,68,84,89,77,13,71,19,81,54,96,88,22,40,99,24,62,85,37,95,97,89,64,30,18,98,95,9,27,76,85,49,99,31,55,71,89,95,86,94,69,24,98,32,84,99,72,82,89,61,75,30,90,74,10,71,14,80,55,68,61,99,54,84,49,17,74,83,79,38,25,90,38,99,36,89,14,38,80,71,92,10,4,65,35,78,95,40,36,78,13,39,83,76,82,64,16,96,95,31,75,95,79,2,89,38,36,87,36,76,81,38,42,92,38,7,83,87,83,87,54,96,99,78,50,43,94,96,41,87,77,8,90,78,72,79,49,82,82,56,13,94,34,90,44,82,22,60,96,48,97,2,88,87,47,92,40,91,4,58,93,29,61,83,98,99,7,8,91,30,15,88,20,90,79,10,93,31,41,95,94,56,94,95,70,93,50,94,40,37,42,84,45,35,59,27,75,80,52,90,93,15,21,92,18,52,96,83,1,90,86,12,79,21,38,98,13,74,99,40,85,41,60,94,54,44,98,83,35,57,76,66,94,94,59,82,62,77,76,22,87,39,95,98,5,90,60,88,46,91,23,58,16,83,79,7,99,11,53,76,12,88,96,88,35,58,63,81,12,26,79,89,79,26,28,23,5,90,1,76,85,55,74,44,42,88,78,36,83,61,86,92,37,62,82,80,60,46,78,32,76,20,56,77,81,9,40,45,81,85,46,7,65,96,90,19,83,16,78,66,25,24,87,80,55,93,71,84,21,86,38,79,80,94,11,42,81,89,56,18,81,33,86,72,48,86,90,59,10,92,35,77,39,94,58,97,36,5,90,96,87,40,21,22,74,80,42,32,59,60,96,25,26,95,54,90,54,15,18,98,61,91,58,84,2,19,83,36,87,60,99,63,34,79,84,92,25,74,62,6,76,84,33,80,54,91,84,3,83,95,34,22,92,88,6,88,93,17,87,59,95,17,98,65,24,20,90,95,31,74,93,30,66,80,79,72,98,7,74,34,87,77,3,24,4,82,93,42,53,90,47,82,65,65,16,75,91,79,20,93,77,54,71,81,47,82,18,78,94,92,63,75,36,87,34,87,31,92,29,98,22,80,95,91,17,97,35,79,87,87,61,93,93,99,63,95,36,90,78,77,61,83,0,0,21,21,1,10,1,0,0,0,0,0,0

80
2019/day15/main.go Normal file
View File

@ -0,0 +1,80 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
)
var auto bool
func main() {
progFileName := "input"
if len(os.Args) > 1 && os.Args[1] == "-auto" {
auto = true
}
prog := intcode.ReadIntCodeFile(progFileName)
play(prog)
}
func play(prog []int) {
p := intcode.NewProgram(prog)
go func() {
for {
for !p.NeedsInput() {
time.Sleep(1)
}
if auto {
} else {
var gotInput bool
for !gotInput {
fmt.Print("Input (vimlike): ")
reader := bufio.NewReader(os.Stdin)
inp, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
inp = strings.TrimSpace(inp)
switch inp {
case "h":
p.Input(3)
gotInput = true
case "j":
p.Input(2)
gotInput = true
case "k":
p.Input(1)
gotInput = true
case "l":
p.Input(4)
gotInput = true
}
}
}
for !p.NeedsOutput() {
time.Sleep(1)
}
moveRes := p.Output()
switch moveRes {
case 0: // Hit a wall
fmt.Println("WALL")
case 1: // Moved
fmt.Println("OK")
case 2: // Moved and done
fmt.Println("DONE")
}
}
}()
ret := p.Run()
if ret == intcode.RET_DONE {
} else if ret == intcode.RET_ERR {
fmt.Println("ERROR")
fmt.Println(p.Error())
}
}

View File

@ -3,7 +3,11 @@ package intcodeprocessor
import (
"errors"
"fmt"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
"io/ioutil"
"math"
"os"
"strings"
)
const (
@ -48,6 +52,20 @@ type Program struct {
debug bool
}
func ReadIntCodeFile(fn string) []int {
dat, err := ioutil.ReadFile(fn)
if err != nil {
fmt.Println("Error reading program file:", err.Error())
os.Exit(1)
}
var prog []int
stringDat := strings.TrimSpace(string(dat))
for _, v := range strings.Split(stringDat, ",") {
prog = append(prog, helpers.Atoi(v))
}
return prog
}
func NewProgram(prog []int) *Program {
p := new(Program)
p.originalCode = make([]int, len(prog))