mirror of
https://github.com/Noettore/AdventOfCode.git
synced 2025-10-15 03:36:39 +02:00
121
2019/day_05/day_05.go
Normal file
121
2019/day_05/day_05.go
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
opcodeAdd = 1
|
||||||
|
opcodeMultiply = 2
|
||||||
|
opcodeInput = 3
|
||||||
|
opcodeOutput = 4
|
||||||
|
opcodeJumpIfTrue = 5
|
||||||
|
opcodeJumpIfFalse = 6
|
||||||
|
opcodeLessThan = 7
|
||||||
|
opcodeEquals = 8
|
||||||
|
opcodeHalt = 99
|
||||||
|
|
||||||
|
paramModePosition = 0
|
||||||
|
paramModeImmediate = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
func readValues(fileName string) []int {
|
||||||
|
file, err := os.Open(fileName)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var values []int
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lineElements := strings.Split(scanner.Text(), ",")
|
||||||
|
for _, elem := range lineElements {
|
||||||
|
n, err := strconv.Atoi(elem)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Cannot convert to int: %s\n", elem)
|
||||||
|
}
|
||||||
|
values = append(values, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
|
func runIntcode(values []int) {
|
||||||
|
instructionPointer := 0
|
||||||
|
for values[instructionPointer] != opcodeHalt {
|
||||||
|
instruction := values[instructionPointer]
|
||||||
|
opCode := instruction % 100
|
||||||
|
|
||||||
|
paramAddress := func(n int) int {
|
||||||
|
mode := (instruction % int(math.Pow10(n+2))) / int(math.Pow10(n+1))
|
||||||
|
switch mode {
|
||||||
|
case paramModeImmediate:
|
||||||
|
return instructionPointer + n
|
||||||
|
default:
|
||||||
|
return values[instructionPointer+n]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch opCode {
|
||||||
|
case opcodeInput:
|
||||||
|
var input int
|
||||||
|
fmt.Print("Input: ")
|
||||||
|
_, err := fmt.Scanf("%d", &input)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Cannot read integer from stdin: %s", err)
|
||||||
|
}
|
||||||
|
values[paramAddress(1)] = input
|
||||||
|
instructionPointer += 2
|
||||||
|
case opcodeOutput:
|
||||||
|
fmt.Printf("Output: %d\n", values[paramAddress(1)])
|
||||||
|
instructionPointer += 2
|
||||||
|
case opcodeAdd:
|
||||||
|
values[paramAddress(3)] = values[paramAddress(1)] + values[paramAddress(2)]
|
||||||
|
instructionPointer += 4
|
||||||
|
case opcodeMultiply:
|
||||||
|
values[paramAddress(3)] = values[paramAddress(1)] * values[paramAddress(2)]
|
||||||
|
instructionPointer += 4
|
||||||
|
case opcodeJumpIfTrue:
|
||||||
|
if values[paramAddress(1)] != 0 {
|
||||||
|
instructionPointer = values[paramAddress(2)]
|
||||||
|
} else {
|
||||||
|
instructionPointer += 3
|
||||||
|
}
|
||||||
|
case opcodeJumpIfFalse:
|
||||||
|
if values[paramAddress(1)] == 0 {
|
||||||
|
instructionPointer = values[paramAddress(2)]
|
||||||
|
} else {
|
||||||
|
instructionPointer += 3
|
||||||
|
}
|
||||||
|
case opcodeLessThan:
|
||||||
|
if values[paramAddress(1)] < values[paramAddress(2)] {
|
||||||
|
values[paramAddress(3)] = 1
|
||||||
|
} else {
|
||||||
|
values[paramAddress(3)] = 0
|
||||||
|
}
|
||||||
|
instructionPointer += 4
|
||||||
|
case opcodeEquals:
|
||||||
|
if values[paramAddress(1)] == values[paramAddress(2)] {
|
||||||
|
values[paramAddress(3)] = 1
|
||||||
|
} else {
|
||||||
|
values[paramAddress(3)] = 0
|
||||||
|
}
|
||||||
|
instructionPointer += 4
|
||||||
|
default:
|
||||||
|
log.Fatalf("Unknown opCode: %d\n", opCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
values := readValues("./input")
|
||||||
|
runIntcode(values)
|
||||||
|
}
|
1
2019/day_05/input
Normal file
1
2019/day_05/input
Normal file
@@ -0,0 +1 @@
|
|||||||
|
3,225,1,225,6,6,1100,1,238,225,104,0,1102,89,49,225,1102,35,88,224,101,-3080,224,224,4,224,102,8,223,223,1001,224,3,224,1,223,224,223,1101,25,33,224,1001,224,-58,224,4,224,102,8,223,223,101,5,224,224,1,223,224,223,1102,78,23,225,1,165,169,224,101,-80,224,224,4,224,102,8,223,223,101,7,224,224,1,224,223,223,101,55,173,224,1001,224,-65,224,4,224,1002,223,8,223,1001,224,1,224,1,223,224,223,2,161,14,224,101,-3528,224,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1002,61,54,224,1001,224,-4212,224,4,224,102,8,223,223,1001,224,1,224,1,223,224,223,1101,14,71,225,1101,85,17,225,1102,72,50,225,1102,9,69,225,1102,71,53,225,1101,10,27,225,1001,158,34,224,101,-51,224,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,102,9,154,224,101,-639,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,226,226,224,102,2,223,223,1006,224,329,101,1,223,223,1007,677,677,224,1002,223,2,223,1005,224,344,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,359,1001,223,1,223,108,226,677,224,1002,223,2,223,1005,224,374,1001,223,1,223,107,226,677,224,102,2,223,223,1006,224,389,101,1,223,223,1107,226,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,1107,677,226,224,102,2,223,223,1005,224,419,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,434,1001,223,1,223,1108,677,226,224,1002,223,2,223,1005,224,449,101,1,223,223,1008,226,226,224,102,2,223,223,1005,224,464,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,479,101,1,223,223,1008,226,677,224,1002,223,2,223,1006,224,494,101,1,223,223,1107,226,677,224,1002,223,2,223,1005,224,509,1001,223,1,223,1108,226,226,224,1002,223,2,223,1006,224,524,101,1,223,223,7,226,226,224,102,2,223,223,1006,224,539,1001,223,1,223,107,226,226,224,102,2,223,223,1006,224,554,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,569,101,1,223,223,1008,677,677,224,1002,223,2,223,1006,224,584,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,599,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,614,101,1,223,223,108,677,677,224,102,2,223,223,1005,224,629,1001,223,1,223,8,677,677,224,1002,223,2,223,1005,224,644,1001,223,1,223,7,677,226,224,102,2,223,223,1006,224,659,1001,223,1,223,1007,226,677,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226
|
Reference in New Issue
Block a user