2019/day2/main.go
2019-12-02 21:02:53 +01:00

67 lines
1.0 KiB
Go

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)
}
// ========= Finish now, we can solve the task ======
t2[1] = 12
t2[2] = 2
ParseArray(t2)
log.Println("Part1: " + strconv.Itoa(t2[0]))
}
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
}