add day 1 and 2 solutions
This commit is contained in:
@@ -0,0 +1 @@
|
||||
package debugger
|
||||
@@ -0,0 +1,61 @@
|
||||
package interpreter
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Interpreter struct {
|
||||
program []int
|
||||
instructionPointer int
|
||||
}
|
||||
|
||||
func Init(program []int) *Interpreter {
|
||||
return &Interpreter {
|
||||
program: program,
|
||||
instructionPointer: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Interpreter) Next() bool {
|
||||
opcode := i.getValueAtIndex(i.instructionPointer)
|
||||
ipIncrement := 1
|
||||
|
||||
switch opcode {
|
||||
case 1:
|
||||
i.add()
|
||||
ipIncrement = 4
|
||||
case 2:
|
||||
i.mul()
|
||||
ipIncrement = 4
|
||||
case 99:
|
||||
return true
|
||||
default:
|
||||
panic(fmt.Sprintf("unhandled opcode %v at IP %v", opcode, i.instructionPointer))
|
||||
}
|
||||
|
||||
i.instructionPointer += ipIncrement
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (i *Interpreter) getValueAtIndex(index int) int {
|
||||
return i.program[index]
|
||||
}
|
||||
|
||||
func (i *Interpreter) setValueAtIndex(index, value int) {
|
||||
i.program[index] = value
|
||||
}
|
||||
|
||||
func (i *Interpreter) add() {
|
||||
resIndex := i.getValueAtIndex(i.instructionPointer + 3)
|
||||
arg1Index := i.getValueAtIndex(i.instructionPointer + 1)
|
||||
arg2Index := i.getValueAtIndex(i.instructionPointer + 2)
|
||||
|
||||
i.setValueAtIndex(resIndex, i.getValueAtIndex(arg1Index) + i.getValueAtIndex(arg2Index))
|
||||
}
|
||||
|
||||
func (i *Interpreter) mul() {
|
||||
resIndex := i.getValueAtIndex(i.instructionPointer + 3)
|
||||
arg1Index := i.getValueAtIndex(i.instructionPointer + 1)
|
||||
arg2Index := i.getValueAtIndex(i.instructionPointer + 2)
|
||||
|
||||
i.setValueAtIndex(resIndex, i.getValueAtIndex(arg1Index) * i.getValueAtIndex(arg2Index))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package interpreter
|
||||
@@ -0,0 +1,48 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const DefaultFile = "input"
|
||||
|
||||
func ParseFile(input *string) ([]int, error) {
|
||||
if input == nil {
|
||||
|
||||
file := DefaultFile
|
||||
|
||||
if len(os.Args) == 2 {
|
||||
file = os.Args[1]
|
||||
}
|
||||
|
||||
inputFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not parse input file: %v", err)
|
||||
}
|
||||
|
||||
fileData, err := io.ReadAll(inputFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not read input file: %v", err)
|
||||
}
|
||||
|
||||
fileStr := strings.TrimSpace(string(fileData))
|
||||
input = &fileStr
|
||||
}
|
||||
|
||||
strNums := strings.Split(*input, ",")
|
||||
opcodes := make([]int, len(strNums))
|
||||
|
||||
for i, v := range strNums {
|
||||
num, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARNING: error parsing int from input, substring was %v", v)
|
||||
}
|
||||
opcodes[i] = num
|
||||
}
|
||||
|
||||
return opcodes, nil
|
||||
}
|
||||
Reference in New Issue
Block a user