mirror of
https://github.com/Noettore/AdventOfCode.git
synced 2025-10-15 19:56:39 +02:00
AoC 2020: day4
Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
95
2019-go/day_02/day_02.go
Normal file
95
2019-go/day_02/day_02.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func readValues(fileName string) []int {
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
r := csv.NewReader(file)
|
||||
|
||||
recordsMatrix, err := r.ReadAll()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
values := make([]int, len(recordsMatrix[0]))
|
||||
|
||||
for i, value := range recordsMatrix[0] {
|
||||
values[i], err = strconv.Atoi(value)
|
||||
if err != nil {
|
||||
log.Printf("Error converting line to int: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
func restore1202(values []int) []int {
|
||||
restoredValues := append(values[:0:0], values...)
|
||||
|
||||
restoredValues[1] = 12
|
||||
restoredValues[2] = 2
|
||||
|
||||
return restoredValues
|
||||
}
|
||||
|
||||
func findNounVerb(values []int) int {
|
||||
testValues := append(values[:0:0], values...)
|
||||
for noun := 0; noun < 100; noun++ {
|
||||
for verb := 0; verb < 100; verb++ {
|
||||
testValues[1] = noun
|
||||
testValues[2] = verb
|
||||
output := runIntcode(testValues)
|
||||
if output == 19690720 {
|
||||
return 100*noun + verb
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func runIntcode(values []int) int {
|
||||
processedValues := append(values[:0:0], values...)
|
||||
loop:
|
||||
for i := 0; i < len(processedValues); i += 4 {
|
||||
num1 := processedValues[processedValues[i+1]]
|
||||
num2 := processedValues[processedValues[i+2]]
|
||||
pos := processedValues[i+3]
|
||||
|
||||
switch processedValues[i] {
|
||||
case 1:
|
||||
processedValues[pos] = num1 + num2
|
||||
case 2:
|
||||
processedValues[pos] = num1 * num2
|
||||
case 99:
|
||||
break loop
|
||||
default:
|
||||
log.Printf("Error evaluating opcode %d\n", processedValues[i])
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return processedValues[0]
|
||||
}
|
||||
|
||||
func main() {
|
||||
values := readValues("./input")
|
||||
|
||||
// Part 1
|
||||
restoredValues := restore1202(values)
|
||||
initialValue := runIntcode(restoredValues)
|
||||
fmt.Printf("Part 1: %d\n", initialValue)
|
||||
|
||||
//Part 2
|
||||
nounVerb := findNounVerb(values)
|
||||
fmt.Printf("Part 2: %d\n", nounVerb)
|
||||
}
|
1
2019-go/day_02/input
Normal file
1
2019-go/day_02/input
Normal file
@@ -0,0 +1 @@
|
||||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,1,19,6,23,2,23,6,27,2,6,27,31,2,13,31,35,1,9,35,39,2,10,39,43,1,6,43,47,1,13,47,51,2,6,51,55,2,55,6,59,1,59,5,63,2,9,63,67,1,5,67,71,2,10,71,75,1,6,75,79,1,79,5,83,2,83,10,87,1,9,87,91,1,5,91,95,1,95,6,99,2,10,99,103,1,5,103,107,1,107,6,111,1,5,111,115,2,115,6,119,1,119,6,123,1,123,10,127,1,127,13,131,1,131,2,135,1,135,5,0,99,2,14,0,0
|
Reference in New Issue
Block a user