aboutsummaryrefslogtreecommitdiff
path: root/2019/go/day02.go
blob: 8108c87ea2d9ae580935ed3b3059ce7d1e38d6cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

func main() {
	var slice []int
	start, size := 0, 0
	if len(os.Args) < 2 {
		panic("runtime error: missing operand")
	}
	file, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		panic(err)
	}
	for i := 0; i < len(file); i++ {
		if file[i] == ',' || file[i] == '\n' {
			arg, err := strconv.Atoi(string(file[start:i])) // i-1??
			if err != nil {
				panic(err)
			}
			slice = append(slice, arg)
			start = i + 1
			size++
		}
	}
	noun, verb := edocpo(slice, 19690720)
	slice[1] = 12
	slice[2] = 2
	fmt.Println(opcode(slice)[0])
	fmt.Println(100*noun + verb)
}

func opcode(slice []int) []int {
	for i := 0; i < len(slice); i += 4 {
		if slice[i] == 1 {
			slice[slice[i+3]] = slice[slice[i+1]] + slice[slice[i+2]]
		} else if slice[i] == 2 {
			slice[slice[i+3]] = slice[slice[i+1]] * slice[slice[i+2]]
		} else if slice[i] == 99 {
			return slice
		} else {
			fmt.Println("Unsupported code", slice[i], "at", i)
			os.Exit(0)
		}
	}
	return slice
}

func edocpo(slice []int, output int) (int, int) {
	var ecils []int
	for i := 0; i < len(slice); i++ {
		for j := 0; j < len(slice); j++ {
			ecils = append([]int(nil), slice...) // reset ecils to slice
			ecils[1], ecils[2] = i, j
			if opcode(ecils)[0] == output {
				return i, j
			}
		}
	}
	return -1, -1
}