More slight streamlining

This commit is contained in:
2022-06-07 10:01:29 -05:00
parent dd5c072730
commit 8cd4994300
3 changed files with 14 additions and 7 deletions

View File

@ -7,19 +7,19 @@ import (
) )
type Day02 struct { type Day02 struct {
program []int64 program utilities.IntcodeProgram
} }
func (d *Day02) Parse() { func (d *Day02) Parse() {
d.program = utilities.ParseIntcodeProgram(utilities.GetStringContents("02p")) d.program = utilities.LoadIntcodeProgram("02p")
} }
func (d Day02) Num() int { func (d Day02) Num() int {
return 2 return 2
} }
func (d *Day02) getProgramWithParams(param1, param2 int64) []int64 { func (d *Day02) getProgramWithParams(param1, param2 int64) utilities.IntcodeProgram {
program := make([]int64, len(d.program)) program := make(utilities.IntcodeProgram, len(d.program))
copy(program, d.program) copy(program, d.program)
program[1] = param1 program[1] = param1
program[2] = param2 program[2] = param2

View File

@ -11,9 +11,11 @@ const (
opEnd = 99 opEnd = 99
) )
func ParseIntcodeProgram(programStr string) []int64 { type IntcodeProgram []int64
func ParseIntcodeProgram(programStr string) IntcodeProgram {
nums := strings.Split(programStr, ",") nums := strings.Split(programStr, ",")
program := make([]int64, len(nums)) program := make(IntcodeProgram, len(nums))
for idx, num := range nums { for idx, num := range nums {
iNum, err := strconv.ParseInt(num, 10, 64) iNum, err := strconv.ParseInt(num, 10, 64)
if err != nil { if err != nil {
@ -25,7 +27,7 @@ func ParseIntcodeProgram(programStr string) []int64 {
return program return program
} }
func RunIntcodeProgram(program []int64) { func RunIntcodeProgram(program IntcodeProgram) {
for instructionPointer := 0; instructionPointer < len(program); { for instructionPointer := 0; instructionPointer < len(program); {
opcode := program[instructionPointer] opcode := program[instructionPointer]
switch opcode { switch opcode {

View File

@ -24,6 +24,11 @@ func getData(filename string, lineHandler func(line string)) {
} }
} }
func LoadIntcodeProgram(filename string) IntcodeProgram {
programStr := GetStringContents(filename)
return ParseIntcodeProgram(programStr)
}
func GetStringContents(filename string) string { func GetStringContents(filename string) string {
var retval string var retval string
getData(filename, func(line string) { getData(filename, func(line string) {