Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (41 sloc) 857 Bytes
package intcode
import (
"bufio"
"fmt"
"io"
"os"
)
func RunProgram(data []int64, in <-chan int64) <-chan int64 {
return Run(data, in, false)
}
func Run(data []int64, in <-chan int64, dbg bool) <-chan int64 {
out := make(chan int64)
m := newMachine(data, in, out, dbg)
go m.run()
return out
}
func ReadProgram(path string) ([]int64, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var data []int64
for r := bufio.NewReader(f); ; {
tok, err := r.ReadString(',')
if err != nil && err != io.EOF {
return nil, fmt.Errorf("bad int in %s: %w", path, err)
}
if len(tok) > 0 {
var i int64
if _, err := fmt.Sscanf(tok, "%d", &i); err != nil {
return nil, fmt.Errorf("bad int in %s: %w", path, err)
}
data = append(data, i)
}
if err == io.EOF {
break
}
}
return data, nil
}
You can’t perform that action at this time.