Skip to content

Commit

Permalink
Add output helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Lowe committed Dec 5, 2019
1 parent f4d06fb commit ebb267b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 53 deletions.
18 changes: 6 additions & 12 deletions challenge/day5/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package day5

import (
"fmt"
"sync"

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

Expand All @@ -19,19 +19,13 @@ var A = &cobra.Command{
}

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

lastCode := 0
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
for code := range output {
fmt.Printf("output: %d\n", code)
lastCode = code
}

wg.Done()
}()
wg := output.Each(outputs, func(code int) {
fmt.Printf("output: %d\n", code)
lastCode = code
})

cpu.Run()
wg.Wait()
Expand Down
11 changes: 3 additions & 8 deletions challenge/day5/b.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package day5

import (
"fmt"
"sync"

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

Expand All @@ -19,15 +19,10 @@ var B = &cobra.Command{
}

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

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

cpu.Run()
wg.Wait()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/nlowe/aoc2019
go 1.13

require (
github.com/magiconair/properties v1.8.1
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/pkg/profile v1.4.0
github.com/spf13/afero v1.2.2 // indirect
Expand Down
42 changes: 9 additions & 33 deletions intcode/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package intcode
import (
"fmt"
"strconv"
"sync"
"testing"

"github.com/nlowe/aoc2019/intcode/input"
"github.com/nlowe/aoc2019/intcode/output"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -55,14 +55,8 @@ func TestCPU_Indirection(t *testing.T) {
}

func TestCPU_IO(t *testing.T) {
sut, output := NewCPUForProgram("00003,0,00004,0,99", input.NewFixed(42))

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
require.Equal(t, 42, <-output)
wg.Done()
}()
sut, outputs := NewCPUForProgram("00003,0,00004,0,99", input.NewFixed(42))
wg := output.Expect(t, outputs, 42)

sut.Run()
wg.Wait()
Expand All @@ -86,14 +80,8 @@ func TestCPU_Compare(t *testing.T) {

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, outputs := NewCPUForProgram(tt.program, tt.input)
wg := output.Expect(t, outputs, tt.expected)

cpu.Run()
wg.Wait()
Expand All @@ -115,14 +103,8 @@ func TestCPU_Jump(t *testing.T) {

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, outputs := NewCPUForProgram(tt.program, tt.input)
wg := output.Expect(t, outputs, tt.expected)

cpu.Run()
wg.Wait()
Expand All @@ -143,14 +125,8 @@ func TestCPU_CmpAndJump(t *testing.T) {

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, outputs := NewCPUForProgram(tt.program, tt.input)
wg := output.Expect(t, outputs, tt.expected)

cpu.Run()
wg.Wait()
Expand Down
48 changes: 48 additions & 0 deletions intcode/output/expect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package output

import (
"sync"
"testing"

"github.com/magiconair/properties/assert"
)

func Each(outputs <-chan int, cb func(int)) *sync.WaitGroup {
wg := &sync.WaitGroup{}

go func() {
for code := range outputs {
cb(code)
}

wg.Done()
}()

return wg
}

func Expect(t *testing.T, outputs <-chan int, expected ...int) *sync.WaitGroup {
wg := &sync.WaitGroup{}
wg.Add(1)

go func() {
for v := range outputs {
assert.Equal(t, expected[0], v)
}
wg.Done()
}()

return wg
}

func Single(outputs <-chan int, result *int) *sync.WaitGroup {
wg := &sync.WaitGroup{}
wg.Add(1)

go func() {
*result = <-outputs
wg.Done()
}()

return wg
}

0 comments on commit ebb267b

Please sign in to comment.