Redid day23 from scratch...

It's working but I'm not happy about it yet.
This commit is contained in:
Brian Buller 2020-11-04 09:50:08 -06:00
parent f6e0792525
commit 08cb79c5cb
5 changed files with 302 additions and 160 deletions

BIN
2019/day23-scratch/day23-scratch Executable file

Binary file not shown.

1
2019/day23-scratch/input Normal file

File diff suppressed because one or more lines are too long

123
2019/day23-scratch/main.go Normal file
View File

@ -0,0 +1,123 @@
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
func main() {
input, _ := ioutil.ReadFile("input")
// Build the memory
mem := map[int]int{}
for i, s := range strings.Split(strings.TrimSpace(string(input)), ",") {
mem[i], _ = strconv.Atoi(s)
}
// Mesage Buffers
in, out := make([]chan int, 50), make([]chan int, 50)
for i := 0; i < 50; i++ {
in[i], out[i] = make(chan int), make(chan int)
go run(mem, in[i], out[i])
// Send address to each computer first
in[i] <- i
in[i] <- -1
}
idle := 0
var old, nat [2]int
for i := 0; ; i = (i + 1) % 50 {
select {
case addr := <-out[i]:
if addr == 255 {
new := [2]int{<-out[i], <-out[i]}
if nat == [2]int{} {
fmt.Println(new[1])
}
nat = new
} else {
in[addr] <- <-out[i]
in[addr] <- <-out[i]
}
idle = 0
case in[i] <- -1:
idle++
}
if idle >= 50 {
if nat[1] == old[1] {
fmt.Println(nat[1])
return
}
in[0] <- nat[0]
in[0] <- nat[1]
old = nat
idle = 0
}
}
}
func run(init map[int]int, in <-chan int, out chan<- int) {
ip, rb := 0, 0
mem := map[int]int{}
for i, v := range init {
mem[i] = v
}
for {
ins := fmt.Sprintf("%05d", mem[ip])
op, _ := strconv.Atoi(ins[3:])
par := func(i int) int {
switch ins[3-i] {
case '1':
return ip + i
case '2':
return rb + mem[ip+i]
default:
return mem[ip+i]
}
}
switch op {
case 1:
mem[par(3)] = mem[par(1)] + mem[par(2)]
case 2:
mem[par(3)] = mem[par(1)] * mem[par(2)]
case 3:
mem[par(1)] = <-in
case 4:
out <- mem[par(1)]
case 5:
if mem[par(1)] != 0 {
ip = mem[par(2)]
continue
}
case 6:
if mem[par(1)] == 0 {
ip = mem[par(2)]
continue
}
case 7:
if mem[par(1)] < mem[par(2)] {
mem[par(3)] = 1
} else {
mem[par(3)] = 0
}
case 8:
if mem[par(1)] == mem[par(2)] {
mem[par(3)] = 1
} else {
mem[par(3)] = 0
}
case 9:
rb += mem[par(1)]
case 99:
return
}
ip += []int{1, 4, 4, 2, 2, 3, 3, 4, 4, 2}[op]
}
}

View File

@ -1,78 +1,54 @@
package main package main
import ( import (
"time" "fmt"
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor" h "git.bullercodeworks.com/brian/adventofcode/helpers"
) )
func main() { func main() {
//pt := helpers.GetArgNumber(1) pt := h.GetArgNumber(1)
prog := intcode.ReadIntCodeFile("input") if pt != "2" {
//if pt == "1" { part1()
part1(prog)
//} else {
// part2(prog)
//}
}
func part1(prog []int) {
p := intcode.NewProgram(prog)
n := StartNetwork(*p)
for !n.CrashOverride {
time.Sleep(1)
}
}
/*
func part1o(prog []int) {
var network []*intcode.Program
msgQueue := make([][]int, 255)
for k := 0; k < 50; k++ {
p := intcode.NewProgram(prog)
//p.EnableDebug()
network = append(network, p)
msgQueue = append(msgQueue, []int{})
go func(addr int) {
for !network[addr].NeedsInput() {
time.Sleep(1)
}
network[addr].Input(addr)
for {
if !network[addr].NeedsOutput() && !network[addr].NeedsInput() {
time.Sleep(1)
} else if network[addr].NeedsOutput() {
msgD := network[addr].Output()
msgQueue[msgD] = append(msgQueue[msgD], network[addr].Output())
msgQueue[msgD] = append(msgQueue[msgD], network[addr].Output())
fmt.Println(addr, "->", msgD, msgQueue[msgD])
} else if network[addr].NeedsInput() {
if len(msgQueue[addr]) > 0 {
var v int
v, msgQueue[addr] = msgQueue[addr][0], msgQueue[addr][1:]
fmt.Println(addr, "<-", v)
network[addr].Input(v)
} else { } else {
network[addr].Input(-1) part2()
} }
}
}
}(k)
go network[k].Run()
}
for len(msgQueue[255]) < 2 {
time.Sleep(1)
}
fmt.Println(msgQueue[255])
}
*/
func part2(prog []int) {
} }
func getOutput(p *intcode.Program) int { func part1() {
for !p.NeedsOutput() { devices, queue := NewNetwork()
time.Sleep(1) for {
//fmt.Print(h.CLEAR_SCREEN)
for i, dev := range devices {
var packet h.Coordinate
if len(queue[i]) > 0 {
packet = queue[i][0]
} else {
packet = h.Coordinate{X: -1, Y: -1}
}
select {
case dest := <-dev.Channel:
x := <-dev.Channel
y := <-dev.Channel
//fmt.Printf("%s: [%d] -> [%d]: {%d, %d}\n", time.Now().Format(time.Stamp), i, dest, x, y)
if dest == 255 {
fmt.Println(y)
return
}
queue[dest] = append(queue[dest], h.Coordinate{X: x, Y: y})
case dev.Channel <- packet.X:
//if packet.X != -1 && packet.Y != -1 {
// fmt.Printf("%s: [%d] <-: {%d, %d}\n", time.Now().Format(time.Stamp), i, packet.X, packet.Y)
//}
if packet.X != -1 {
dev.Channel <- packet.Y
queue[i] = queue[i][1:]
}
}
//fmt.Println(dev)
}
} }
return p.Output() }
func part2() {
} }

View File

@ -1,121 +1,163 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"time" "io/ioutil"
"strconv"
"strings"
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
h "git.bullercodeworks.com/brian/adventofcode/helpers" h "git.bullercodeworks.com/brian/adventofcode/helpers"
) )
type System struct { var incache []int
intcode.Program
Running bool type Computer struct {
Address int Address int
IncomingQueue []h.Coordinate Program []int
Network *Network Position int
Offset int
Channel chan int
} }
func NewSystem(program intcode.Program, addr int, network *Network) *System { func (c Computer) String() string {
s := System{ status := "○"
Program: program, if c.Position%2 == 0 {
Address: addr, status = "●"
Network: network,
} }
go func() { return fmt.Sprintf("[%2d] %s {%d}", c.Address, status, c.Offset)
fmt.Println("Booting", s.Address, "...") }
for !s.Program.NeedsInput() {
time.Sleep(1) func (c *Computer) parseInstruction() (int, []int) {
inst := c.read(c.Position)
opcode := inst % 100
modes := make([]int, 4)
inst /= 100
for i := 0; inst > 0; i++ {
modes[i] = inst % 10
inst /= 10
} }
s.Program.Input(s.Address) return opcode, modes
fmt.Println(s.Address, "booted") }
// System should just continue to run now
for { func (c *Computer) parseParameters(modes []int, num int) []int {
if s.NeedsOutput() { params := make([]int, num)
// There should be 3 outputs, an address and a coordinate for i := 0; i < num; i++ {
addr, x, y := s.Output(), s.Output(), s.Output() if i >= len(modes) || modes[i] == 0 {
coord := h.Coordinate{ params[i] = c.read(c.read(c.Position + i + 1))
X: x, } else if modes[i] == 1 {
Y: y, params[i] = c.read(c.Position + i + 1)
}
s.Network.Queue(addr, coord)
} else if s.NeedsInput() {
v, err := s.Network.Retrieve(s.Address)
if err != nil {
s.Input(v.X)
for !s.NeedsInput() {
time.Sleep(1)
}
s.Input(v.Y)
} else { } else {
s.Input(-1) params[i] = c.read(c.read(c.Position+i+1) + c.Offset)
} }
} }
} return params
}()
go s.Program.Run()
return &s
} }
func (s System) String() string { func (c *Computer) read(pos int) int {
status := "." if pos >= len(c.Program) {
if s.Program.NeedsInput() { return 0
status = "<"
} else if s.Program.NeedsOutput() {
status = ">"
} }
return fmt.Sprintf("[%d] %s\n", s.Address, status) return c.Program[pos]
} }
type Network struct { func (c *Computer) write(pos, val, mode int) {
Systems []*System if mode == 2 {
Messages map[int][]h.Coordinate pos += c.Offset
CrashOverride bool }
if pos >= len(c.Program) {
prog := make([]int, pos*2)
copy(prog, c.Program)
c.Program = prog
}
c.Program[pos] = val
} }
func StartNetwork(nic intcode.Program) *Network { func (c *Computer) run() {
n := Network{ for c.read(c.Position) != 99 {
Messages: make(map[int][]h.Coordinate), inst, modes := c.parseInstruction()
} switch inst {
for i := 0; i < 50; i++ { case 1:
n.Systems = append(n.Systems, NewSystem(nic, i, &n)) params := c.parseParameters(modes, 2)
} c.write(c.read(c.Position+3), params[0]+params[1], modes[2])
return &n c.Position += 4
} case 2:
params := c.parseParameters(modes, 2)
func (n *Network) Queue(addr int, coord h.Coordinate) { c.write(c.read(c.Position+3), params[0]*params[1], modes[2])
fmt.Println("Network Queue", addr, coord, len(n.Messages)) c.Position += 4
if addr == 255 { case 3:
fmt.Printf("First packet to 255: {X: %d, Y: %d}\n", coord.X, coord.Y) c.write(c.read(c.Position+1), <-c.Channel, modes[0])
n.CrashOverride = true c.Position += 2
} case 4:
if n.CrashOverride { params := c.parseParameters(modes, 1)
return c.Channel <- params[0]
} c.Position += 2
fmt.Printf("Queuing Packet for %d: {X: %d, Y: %d}\n", addr, coord.X, coord.Y) case 5:
n.Messages[addr] = append(n.Messages[addr], coord) params := c.parseParameters(modes, 2)
} if params[0] != 0 {
c.Position = params[1]
func (n *Network) Retrieve(addr int) (h.Coordinate, error) {
fmt.Println("Network Retrieve", addr, len(n.Messages))
if n.CrashOverride {
return h.Coordinate{}, errors.New("Network is down")
}
var ret h.Coordinate
if len(n.Messages[addr]) > 0 {
ret, n.Messages[addr] = n.Messages[addr][0], n.Messages[addr][1:]
fmt.Printf("%d Retriving Packet: {X: %d, Y: %d}\n", addr, ret.X, ret.Y)
return ret, nil
} else { } else {
return h.Coordinate{}, errors.New("No message") c.Position += 3
} }
case 6:
params := c.parseParameters(modes, 2)
if params[0] == 0 {
c.Position = params[1]
} else {
c.Position += 3
}
case 7:
params := c.parseParameters(modes, 2)
if params[0] < params[1] {
c.write(c.read(c.Position+3), 1, modes[2])
} else {
c.write(c.read(c.Position+3), 0, modes[2])
}
c.Position += 4
case 8:
params := c.parseParameters(modes, 2)
if params[0] == params[1] {
c.write(c.read(c.Position+3), 1, modes[2])
} else {
c.write(c.read(c.Position+3), 0, modes[2])
}
c.Position += 4
case 9:
params := c.parseParameters(modes, 1)
c.Offset += params[0]
c.Position += 2
}
}
close(c.Channel)
} }
func (n Network) String() string { func NewComputer() Computer {
ret := fmt.Sprintf("Messages: %d\n", len(n.Messages)) var ints []int
for v := range n.Systems { if incache == nil {
ret = ret + n.Systems[v].String() input, _ := ioutil.ReadFile("input")
ints = make([]int, 0)
for _, v := range strings.Split(string(input), ",") {
i, _ := strconv.Atoi(v)
ints = append(ints, i)
} }
return ret incache = make([]int, len(ints))
copy(incache, ints)
} else {
ints = make([]int, len(incache))
copy(ints, incache)
}
c := Computer{Program: ints, Channel: make(chan int)}
go c.run()
return c
}
func NewNetwork() ([]Computer, [][]h.Coordinate) {
devices := make([]Computer, 50)
queue := make([][]h.Coordinate, len(devices))
for i := 0; i < len(devices); i++ {
devices[i] = NewComputer()
devices[i].Address = i
devices[i].Channel <- i
queue[i] = make([]h.Coordinate, 0)
}
return devices, queue
} }