2019 Day 5 Complete
* Also: Move helpers to their own submodule, for importing
This commit is contained in:
parent
589790fcda
commit
3d955a924d
1
2019/day05/input
Normal file
1
2019/day05/input
Normal file
@ -0,0 +1 @@
|
||||
3,225,1,225,6,6,1100,1,238,225,104,0,1102,91,92,225,1102,85,13,225,1,47,17,224,101,-176,224,224,4,224,1002,223,8,223,1001,224,7,224,1,223,224,223,1102,79,43,225,1102,91,79,225,1101,94,61,225,1002,99,42,224,1001,224,-1890,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,102,77,52,224,1001,224,-4697,224,4,224,102,8,223,223,1001,224,7,224,1,224,223,223,1101,45,47,225,1001,43,93,224,1001,224,-172,224,4,224,102,8,223,223,1001,224,1,224,1,224,223,223,1102,53,88,225,1101,64,75,225,2,14,129,224,101,-5888,224,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,101,60,126,224,101,-148,224,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1102,82,56,224,1001,224,-4592,224,4,224,1002,223,8,223,101,4,224,224,1,224,223,223,1101,22,82,224,1001,224,-104,224,4,224,1002,223,8,223,101,4,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,226,677,224,102,2,223,223,1005,224,329,1001,223,1,223,1007,226,226,224,1002,223,2,223,1006,224,344,101,1,223,223,108,226,226,224,1002,223,2,223,1006,224,359,1001,223,1,223,107,226,677,224,102,2,223,223,1006,224,374,101,1,223,223,8,677,677,224,102,2,223,223,1006,224,389,1001,223,1,223,1008,226,677,224,1002,223,2,223,1006,224,404,101,1,223,223,7,677,677,224,1002,223,2,223,1005,224,419,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,434,101,1,223,223,1108,226,226,224,102,2,223,223,1005,224,449,1001,223,1,223,107,226,226,224,102,2,223,223,1005,224,464,101,1,223,223,1007,677,677,224,102,2,223,223,1006,224,479,101,1,223,223,1007,226,677,224,102,2,223,223,1005,224,494,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,509,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,524,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,108,226,677,224,1002,223,2,223,1005,224,554,101,1,223,223,1008,677,677,224,1002,223,2,223,1006,224,569,1001,223,1,223,1107,677,677,224,102,2,223,223,1005,224,584,1001,223,1,223,7,677,226,224,102,2,223,223,1005,224,599,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,614,1001,223,1,223,7,226,677,224,1002,223,2,223,1006,224,629,101,1,223,223,1107,677,226,224,1002,223,2,223,1005,224,644,1001,223,1,223,1107,226,677,224,102,2,223,223,1006,224,659,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,674,101,1,223,223,4,223,99,226
|
38
2019/day05/main.go
Normal file
38
2019/day05/main.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
|
||||
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := helpers.StdinToString()
|
||||
var prog []int
|
||||
for _, v := range strings.Split(inp, ",") {
|
||||
prog = append(prog, helpers.Atoi(v))
|
||||
}
|
||||
p := intcode.NewProgram(prog)
|
||||
go p.Run()
|
||||
// Part 1:
|
||||
// p.Input() <- 1
|
||||
// Part 2:
|
||||
p.Input() <- 5
|
||||
go func() {
|
||||
for {
|
||||
out := <-p.Output()
|
||||
fmt.Println(out)
|
||||
if p.State() != intcode.RET_OK {
|
||||
os.Exit(0)
|
||||
}
|
||||
time.Sleep(50)
|
||||
}
|
||||
}()
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
@ -3,11 +3,23 @@ package intcodeprocessor
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
const (
|
||||
MODE_POS = iota
|
||||
MODE_IMM
|
||||
)
|
||||
|
||||
const (
|
||||
OP_ADD = 1
|
||||
OP_MLT = 2
|
||||
OP_INP = 3
|
||||
OP_OUT = 4
|
||||
OP_JIT = 5
|
||||
OP_JIF = 6
|
||||
OP_ILT = 7
|
||||
OP_IEQ = 8
|
||||
OP_EXT = 99
|
||||
)
|
||||
|
||||
@ -23,11 +35,16 @@ type Program struct {
|
||||
|
||||
state int
|
||||
error error
|
||||
|
||||
input chan int
|
||||
output chan int
|
||||
}
|
||||
|
||||
func NewProgram(prog []int) *Program {
|
||||
p := new(Program)
|
||||
p.code = make([]int, len(prog))
|
||||
p.input = make(chan int, 2)
|
||||
p.output = make(chan int, 2)
|
||||
copy(p.code, prog)
|
||||
return p
|
||||
}
|
||||
@ -58,19 +75,74 @@ func (p *Program) Step() int {
|
||||
p.error = errors.New("Pointer Exception")
|
||||
return RET_ERR
|
||||
}
|
||||
switch p.readNext() {
|
||||
intcode := p.readNext()
|
||||
p.ptr++
|
||||
switch p.opCode(intcode) {
|
||||
case OP_ADD:
|
||||
p.add(p.readNextThree())
|
||||
v1, v2, v3 := p.readNextThree()
|
||||
p.ptr = p.ptr + 3
|
||||
p.opAdd(intcode, v1, v2, v3)
|
||||
if p.error != nil {
|
||||
return RET_ERR
|
||||
}
|
||||
return RET_OK
|
||||
case OP_MLT:
|
||||
p.mult(p.readNextThree())
|
||||
v1, v2, v3 := p.readNextThree()
|
||||
p.ptr = p.ptr + 3
|
||||
p.opMult(intcode, v1, v2, v3)
|
||||
if p.error != nil {
|
||||
return RET_ERR
|
||||
}
|
||||
return RET_OK
|
||||
case OP_INP:
|
||||
v1 := p.readNext()
|
||||
p.ptr++
|
||||
p.opInp(intcode, v1)
|
||||
if p.error != nil {
|
||||
return RET_ERR
|
||||
}
|
||||
return RET_OK
|
||||
case OP_OUT:
|
||||
v1 := p.readNext()
|
||||
p.ptr++
|
||||
p.opOut(intcode, v1)
|
||||
if p.error != nil {
|
||||
return RET_ERR
|
||||
}
|
||||
return RET_OK
|
||||
case OP_JIT:
|
||||
v1, v2 := p.readNextTwo()
|
||||
p.ptr = p.ptr + 2
|
||||
p.opJumpIfTrue(intcode, v1, v2)
|
||||
if p.error != nil {
|
||||
return RET_ERR
|
||||
}
|
||||
return RET_OK
|
||||
case OP_JIF:
|
||||
v1, v2 := p.readNextTwo()
|
||||
p.ptr = p.ptr + 2
|
||||
p.opJumpIfFalse(intcode, v1, v2)
|
||||
if p.error != nil {
|
||||
return RET_ERR
|
||||
}
|
||||
return RET_OK
|
||||
case OP_ILT:
|
||||
v1, v2, dest := p.readNextThree()
|
||||
p.ptr = p.ptr + 3
|
||||
p.opIfLessThan(intcode, v1, v2, dest)
|
||||
if p.error != nil {
|
||||
return RET_ERR
|
||||
}
|
||||
return RET_OK
|
||||
case OP_IEQ:
|
||||
v1, v2, dest := p.readNextThree()
|
||||
p.ptr = p.ptr + 3
|
||||
p.opIfEqual(intcode, v1, v2, dest)
|
||||
if p.error != nil {
|
||||
return RET_ERR
|
||||
}
|
||||
return RET_OK
|
||||
|
||||
case OP_EXT:
|
||||
return RET_DONE
|
||||
}
|
||||
@ -85,28 +157,116 @@ func (p *Program) SetProgramValueAt(idx, val int) {
|
||||
p.code[idx] = val
|
||||
}
|
||||
|
||||
func (p *Program) opCode(intcode int) int {
|
||||
return intcode % 100
|
||||
}
|
||||
|
||||
func (p *Program) paramMode(intcode, pNum int) int {
|
||||
plc := math.Pow10(pNum + 2)
|
||||
return ((intcode - p.opCode(intcode)) / int(plc)) % 10
|
||||
}
|
||||
|
||||
func (p *Program) readNext() int {
|
||||
if len(p.code) <= p.ptr {
|
||||
p.error = errors.New("Pointer Exception")
|
||||
}
|
||||
p.ptr++
|
||||
return p.code[p.ptr-1]
|
||||
return p.code[p.ptr]
|
||||
}
|
||||
|
||||
func (p *Program) readNextTwo() (int, int) {
|
||||
return p.readNext(), p.readNext()
|
||||
return p.code[p.ptr], p.code[p.ptr+1]
|
||||
}
|
||||
|
||||
func (p *Program) readNextThree() (int, int, int) {
|
||||
return p.readNext(), p.readNext(), p.readNext()
|
||||
return p.code[p.ptr], p.code[p.ptr+1], p.code[p.ptr+2]
|
||||
}
|
||||
|
||||
func (p *Program) add(a1, a2, dest int) {
|
||||
p.code[dest] = p.code[a1] + p.code[a2]
|
||||
func (p *Program) get(mode, v int) int {
|
||||
if mode == MODE_POS {
|
||||
return p.code[v]
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (p *Program) mult(a1, a2, dest int) {
|
||||
p.code[dest] = p.code[a1] * p.code[a2]
|
||||
func (p *Program) Input() chan int {
|
||||
return p.input
|
||||
}
|
||||
|
||||
func (p *Program) Output() chan int {
|
||||
return p.output
|
||||
}
|
||||
|
||||
func (p *Program) opAdd(intcode, a1, a2, dest int) {
|
||||
a1md, a2md, destmd := p.paramMode(intcode, 0), p.paramMode(intcode, 1), p.paramMode(intcode, 2)
|
||||
if destmd != MODE_POS {
|
||||
p.error = errors.New("Invalid Destination Mode")
|
||||
p.state = RET_ERR
|
||||
}
|
||||
p.code[dest] = p.get(a1md, a1) + p.get(a2md, a2)
|
||||
}
|
||||
|
||||
func (p *Program) opMult(intcode, a1, a2, dest int) {
|
||||
a1md, a2md, destmd := p.paramMode(intcode, 0), p.paramMode(intcode, 1), p.paramMode(intcode, 2)
|
||||
if destmd != MODE_POS {
|
||||
p.error = errors.New("Invalid Destination Mode")
|
||||
p.state = RET_ERR
|
||||
}
|
||||
p.code[dest] = p.get(a1md, a1) * p.get(a2md, a2)
|
||||
}
|
||||
|
||||
func (p *Program) opInp(intcode, dest int) {
|
||||
destmd := p.paramMode(intcode, 0)
|
||||
if destmd != MODE_POS {
|
||||
p.error = errors.New("Invalid Destination Mode")
|
||||
p.state = RET_ERR
|
||||
}
|
||||
p.code[dest] = <-p.input
|
||||
}
|
||||
|
||||
func (p *Program) opOut(intcode, val int) {
|
||||
valmd := p.paramMode(intcode, 0)
|
||||
ret := p.get(valmd, val)
|
||||
p.output <- ret
|
||||
}
|
||||
|
||||
func (p *Program) opJumpIfTrue(intcode, v1, v2 int) {
|
||||
v1md, v2md := p.paramMode(intcode, 0), p.paramMode(intcode, 1)
|
||||
if p.get(v1md, v1) != 0 {
|
||||
p.ptr = p.get(v2md, v2)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Program) opJumpIfFalse(intcode, v1, v2 int) {
|
||||
v1md, v2md := p.paramMode(intcode, 0), p.paramMode(intcode, 1)
|
||||
if p.get(v1md, v1) == 0 {
|
||||
p.ptr = p.get(v2md, v2)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Program) opIfLessThan(intcode, v1, v2, dest int) {
|
||||
v1md, v2md, destmd := p.paramMode(intcode, 0), p.paramMode(intcode, 1), p.paramMode(intcode, 2)
|
||||
if destmd != MODE_POS {
|
||||
p.error = errors.New("Invalid Destination Mode")
|
||||
p.state = RET_ERR
|
||||
}
|
||||
if p.get(v1md, v1) < p.get(v2md, v2) {
|
||||
p.code[dest] = 1
|
||||
} else {
|
||||
p.code[dest] = 0
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Program) opIfEqual(intcode, v1, v2, dest int) {
|
||||
v1md, v2md, destmd := p.paramMode(intcode, 0), p.paramMode(intcode, 1), p.paramMode(intcode, 2)
|
||||
if destmd != MODE_POS {
|
||||
p.error = errors.New("Invalid Destination Mode")
|
||||
p.state = RET_ERR
|
||||
}
|
||||
if p.get(v1md, v1) == p.get(v2md, v2) {
|
||||
p.code[dest] = 1
|
||||
} else {
|
||||
p.code[dest] = 0
|
||||
}
|
||||
}
|
||||
|
||||
func (p Program) String() string {
|
||||
|
@ -106,7 +106,7 @@ func PrintProgress(curr, total int) {
|
||||
pct := int(float64(curr)/float64(total)) * 100
|
||||
for i := 0; i < 100; i += 10 {
|
||||
if pct > i {
|
||||
fmt.Print(FillChar)
|
||||
fmt.Print(FILL_CHAR)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user