2019 Day 8 Complete!

This commit is contained in:
Brian Buller 2019-12-09 08:53:49 -06:00
parent 7cc0053b33
commit 22c83634fa
5 changed files with 118 additions and 9 deletions

View File

@ -113,19 +113,24 @@ func pt2RunWithPhaseSettings(settings []int) int {
a3.Input(settings[2])
a4.Input(settings[3])
a5.Input(settings[4])
a1.Input(0)
for {
if a5.NeedsOutput() {
a5Out := a5.Output()
fmt.Println("A5: ", a5Out)
a1.Input(a5Out)
}
a2.Input(a1.Output())
a3.Input(a2.Output())
a4.Input(a3.Output())
a5.Input(a4.Output())
for !a5.NeedsOutput() {
time.Sleep(1)
}
if a5.State() == intcode.RET_DONE {
break
}
a1.Input(a5.Output())
for !a5.NeedsOutput() {
time.Sleep(1)
}
}
}()
for a5.State() != intcode.RET_DONE && !a5.NeedsOutput() {

1
2019/day08/input Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,17 +1,118 @@
package main
import (
"bytes"
"fmt"
"os"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := helpers.StdinToStringSlice()
part1(inp)
part2(inp)
args := make([]string, len(os.Args)-1)
copy(args, os.Args[1:])
progFileName := "input"
if len(args) == 0 {
progFileName, args = "input", []string{"25", "6"}
} else {
if len(args) == 3 {
progFileName, args = args[0], args[1:]
}
if len(args) != 2 {
panic("Image Dimensions Required")
}
}
w, h := helpers.Atoi(args[0]), helpers.Atoi(args[1])
inp := bytes.TrimSpace(helpers.FileToBytes(progFileName))
i := NewImage(inp, w, h)
//part1(i)
part2(i)
}
func part1(inp []string) {
func part1(i *Image) {
lowestZero, lowestZeroLayer := -1, -1
for l := range i.Layers {
countZeroes := i.Layers[l].CountByte('0')
if lowestZeroLayer == -1 || lowestZero > countZeroes {
lowestZero, lowestZeroLayer = countZeroes, l
}
}
layer := i.Layers[lowestZeroLayer]
fmt.Println(layer.CountByte('1') * layer.CountByte('2'))
}
func part2(inp []string) {
func part2(i *Image) {
wrk := i.Flatten()
fmt.Println(wrk)
}
type Image struct {
w, h int
Layers []Layer
}
func NewImage(bts []byte, w, h int) *Image {
ret := Image{w: w, h: h}
for len(bts) > 0 {
var lyr *Layer
lyr, bts = NewLayer(bts, w, h)
ret.Layers = append(ret.Layers, *lyr)
}
return &ret
}
func (i Image) String() string {
var ret string
for l := range i.Layers {
ret = ret + fmt.Sprintf("Layer %d\n", l+1)
ret = ret + i.Layers[l].String() + "\n"
ret = ret + "\n"
}
return ret
}
func (i Image) Flatten() *Image {
ret := make([]byte, i.w*i.h)
for l := range i.Layers {
for v := range i.Layers[l].bytes {
if ret[v] == '0' || ret[v] == '1' {
continue
}
ret[v] = i.Layers[l].bytes[v]
}
}
return NewImage(ret, i.w, i.h)
}
type Layer struct {
w, h int
bytes []byte
}
func NewLayer(bts []byte, w, h int) (*Layer, []byte) {
ret := Layer{w: w, h: h, bytes: bts[:w*h]}
if len(bts) > w*h {
return &ret, bts[w*h:]
}
return &ret, nil
}
func (l Layer) CountByte(bt byte) int {
return bytes.Count(l.bytes, []byte{bt})
}
func (l Layer) GetPos(x, y int) byte {
pos := (y * l.w) + x
return l.bytes[pos]
}
func (l Layer) String() string {
var ret string
for i := range l.bytes {
if i > 0 && i%l.w == 0 {
ret = ret + "\n"
}
ret = ret + fmt.Sprintf("%d", l.bytes[i]-48)
}
return ret
}

1
2019/day08/testinput Normal file
View File

@ -0,0 +1 @@
123456789012

1
2019/day08/testinput2 Normal file
View File

@ -0,0 +1 @@
0222112222120000