Skip to content

Commit

Permalink
Implementation for 5b
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Lowe committed Dec 5, 2019
1 parent 6807744 commit 2cdc6a0
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 1 deletion.
35 changes: 35 additions & 0 deletions challenge/day5/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package day5

import (
"fmt"
"sync"

"github.com/nlowe/aoc2019/challenge"
"github.com/nlowe/aoc2019/intcode"
"github.com/nlowe/aoc2019/intcode/input"
"github.com/spf13/cobra"
)

var B = &cobra.Command{
Use: "5b",
Short: "Day 5, Problem B",
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("Answer: %d\n", b(challenge.FromFile()))
},
}

func b(challenge *challenge.Input) int {
cpu, output := intcode.NewCPUForProgram(<-challenge.Lines(), input.NewFixed(5))

code := 0
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
code = <-output
wg.Done()
}()

cpu.Run()
wg.Wait()
return code
}
32 changes: 32 additions & 0 deletions intcode/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const (
OpMul = 2
OpIn = 3
OpOut = 4
OpJT = 5
OpJF = 6
OpLT = 7
OpEQ = 8
OpHalt = 99

ModeIndirect = 0
Expand Down Expand Up @@ -66,6 +70,34 @@ func (c *CPU) Step() {
case OpOut:
c.outputs <- c.read(m1, c.Memory[c.pc+1])
c.pc += 2
case OpJT:
if c.read(m1, c.Memory[c.pc+1]) != 0 {
c.pc = c.read(m2, c.Memory[c.pc+2])
} else {
c.pc += 3
}
case OpJF:
if c.read(m1, c.Memory[c.pc+1]) == 0 {
c.pc = c.read(m2, c.Memory[c.pc+2])
} else {
c.pc += 3
}
case OpLT:
if c.read(m1, c.Memory[c.pc+1]) < c.read(m2, c.Memory[c.pc+2]) {
c.write(m3, c.Memory[c.pc+3], 1)
} else {
c.write(m3, c.Memory[c.pc+3], 0)
}

c.pc += 4
case OpEQ:
if c.read(m1, c.Memory[c.pc+1]) == c.read(m2, c.Memory[c.pc+2]) {
c.write(m3, c.Memory[c.pc+3], 1)
} else {
c.write(m3, c.Memory[c.pc+3], 0)
}

c.pc += 4
case OpHalt:
close(c.outputs)
return
Expand Down
92 changes: 92 additions & 0 deletions intcode/cpu_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package intcode

import (
"fmt"
"strconv"
"sync"
"testing"

Expand Down Expand Up @@ -65,3 +67,93 @@ func TestCPU_IO(t *testing.T) {
sut.Run()
wg.Wait()
}

func TestCPU_Compare(t *testing.T) {
tests := []struct {
program string
input <-chan int
expected int
}{
{program: "3,9,8,9,10,9,4,9,99,-1,8", input: input.NewFixed(8), expected: 1},
{program: "3,9,8,9,10,9,4,9,99,-1,8", input: input.NewFixed(42), expected: 0},
{program: "3,9,7,9,10,9,4,9,99,-1,8", input: input.NewFixed(7), expected: 1},
{program: "3,9,7,9,10,9,4,9,99,-1,8", input: input.NewFixed(42), expected: 0},
{program: "3,3,1108,-1,8,3,4,3,99", input: input.NewFixed(8), expected: 1},
{program: "3,3,1108,-1,8,3,4,3,99", input: input.NewFixed(42), expected: 0},
{program: "3,3,1107,-1,8,3,4,3,99", input: input.NewFixed(7), expected: 1},
{program: "3,3,1107,-1,8,3,4,3,99", input: input.NewFixed(42), expected: 0},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%s=%d", tt.program, tt.expected), func(t *testing.T) {
cpu, output := NewCPUForProgram(tt.program, tt.input)

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
require.Equal(t, tt.expected, <-output)
wg.Done()
}()

cpu.Run()
wg.Wait()
})
}
}

func TestCPU_Jump(t *testing.T) {
tests := []struct {
program string
input <-chan int
expected int
}{
{program: "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9", input: input.NewFixed(0), expected: 0},
{program: "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9", input: input.NewFixed(42), expected: 1},
{program: "3,3,1105,-1,9,1101,0,0,12,4,12,99,1", input: input.NewFixed(0), expected: 0},
{program: "3,3,1105,-1,9,1101,0,0,12,4,12,99,1", input: input.NewFixed(42), expected: 1},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%s=%d", tt.program, tt.expected), func(t *testing.T) {
cpu, output := NewCPUForProgram(tt.program, tt.input)

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
require.Equal(t, tt.expected, <-output)
wg.Done()
}()

cpu.Run()
wg.Wait()
})
}
}

func TestCPU_CmpAndJump(t *testing.T) {
tests := []struct {
program string
input <-chan int
expected int
}{
{program: "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99", input: input.NewFixed(7), expected: 999},
{program: "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99", input: input.NewFixed(8), expected: 1000},
{program: "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99", input: input.NewFixed(9), expected: 1001},
}

for _, tt := range tests {
t.Run(strconv.Itoa(tt.expected), func(t *testing.T) {
cpu, output := NewCPUForProgram(tt.program, tt.input)

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
require.Equal(t, tt.expected, <-output)
wg.Done()
}()

cpu.Run()
wg.Wait()
})
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {
day2.A, day2.B,
day3.A, day3.B,
day4.A, day4.B,
day5.A,
day5.A, day5.B,
)

flags := rootCmd.PersistentFlags()
Expand Down

0 comments on commit 2cdc6a0

Please sign in to comment.