Day 2 initial solution
There's room to optimize part 2, but I wanted to commit my original brute-force solution first.
This commit is contained in:
47
utilities/intcode.go
Normal file
47
utilities/intcode.go
Normal file
@ -0,0 +1,47 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ParseIntcodeProgram(programStr string) []int64 {
|
||||
nums := strings.Split(programStr, ",")
|
||||
program := make([]int64, len(nums))
|
||||
for idx, num := range nums {
|
||||
iNum, err := strconv.ParseInt(num, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
program[idx] = iNum
|
||||
}
|
||||
|
||||
return program
|
||||
}
|
||||
|
||||
func RunIntcodeProgram(program []int64) {
|
||||
for instructionPointer := 0; instructionPointer < len(program); {
|
||||
opcode := program[instructionPointer]
|
||||
switch opcode {
|
||||
case 1:
|
||||
param1 := program[instructionPointer+1]
|
||||
param2 := program[instructionPointer+2]
|
||||
param3 := program[instructionPointer+3]
|
||||
program[param3] = program[param1] + program[param2]
|
||||
|
||||
instructionPointer += 4
|
||||
break
|
||||
case 2:
|
||||
param1 := program[instructionPointer+1]
|
||||
param2 := program[instructionPointer+2]
|
||||
param3 := program[instructionPointer+3]
|
||||
program[param3] = program[param1] * program[param2]
|
||||
|
||||
instructionPointer += 4
|
||||
break
|
||||
case 99:
|
||||
instructionPointer = len(program)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
@ -24,6 +24,17 @@ func getData(filename string, lineHandler func(line string)) {
|
||||
}
|
||||
}
|
||||
|
||||
func GetStringContents(filename string) string {
|
||||
var retval string
|
||||
getData(filename, func(line string) {
|
||||
if len(retval) != 0 {
|
||||
panic("tried to parse multi-line file as a single line")
|
||||
}
|
||||
retval = line
|
||||
})
|
||||
return retval
|
||||
}
|
||||
|
||||
func GetStringLines(filename string) []string {
|
||||
retval := make([]string, 0)
|
||||
getData(filename, func(line string) {
|
||||
|
Reference in New Issue
Block a user