2019-12-02 20:02:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
file, err := os.Open("input.txt")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
if !scanner.Scan() {
|
|
|
|
log.Fatalln("Problem while read line.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stringarr := strings.Split(scanner.Text(), ",")
|
|
|
|
var t2 = []int{}
|
|
|
|
|
|
|
|
for _, i := range stringarr {
|
|
|
|
j, err := strconv.Atoi(i)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
t2 = append(t2, j)
|
|
|
|
}
|
2019-12-03 07:46:39 +00:00
|
|
|
wip := make([]int, len(t2))
|
2019-12-02 20:02:53 +00:00
|
|
|
// ========= Finish now, we can solve the task ======
|
2019-12-03 07:46:39 +00:00
|
|
|
copy(wip, t2)
|
|
|
|
wip[1] = 12
|
|
|
|
wip[2] = 2
|
|
|
|
ParseArray(wip)
|
|
|
|
log.Println("Part1: " + strconv.Itoa(wip[0]))
|
2019-12-02 20:02:53 +00:00
|
|
|
|
2019-12-03 07:46:39 +00:00
|
|
|
noun, verb := Brutforce(t2, 19690720)
|
|
|
|
|
|
|
|
if noun == -1 || verb == -1 {
|
|
|
|
log.Fatal("Part2: No values found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Part2: noun=" + strconv.Itoa(noun) + " verb=" + strconv.Itoa(verb))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Brutforce(list []int, des int) (int, int) {
|
|
|
|
wip := make([]int, len(list))
|
|
|
|
copy(wip, list)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
copy(wip, list)
|
|
|
|
wip[1] = i
|
|
|
|
wip[2] = j
|
|
|
|
ParseArray(wip)
|
|
|
|
if wip[0] == des {
|
|
|
|
return i, j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, -1
|
2019-12-02 20:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseArray(list []int) error {
|
|
|
|
for i := 0; i < len(list); i += 4 {
|
|
|
|
cmd := list[i]
|
|
|
|
|
|
|
|
if cmd == 99 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
num1 := list[list[i+1]]
|
|
|
|
num2 := list[list[i+2]]
|
|
|
|
sum := 0
|
|
|
|
|
|
|
|
if cmd == 1 {
|
|
|
|
sum = num1 + num2
|
|
|
|
} else if cmd == 2 {
|
|
|
|
sum = num1 * num2
|
|
|
|
} else {
|
|
|
|
return errors.New("Wrong Command (" + strconv.Itoa(cmd) + ") at position " + strconv.Itoa(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
list[list[i+3]] = sum
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|