2019/day5/main.go
2019-12-06 08:02:39 +01:00

206 lines
3.4 KiB
Go

package main
import (
"bufio"
"errors"
"fmt"
"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)
}
wip := make([]int, len(t2))
// ========= Finish now, we can solve the task ======
copy(wip, t2)
err = ParseArray(wip)
if err != nil {
log.Println(err)
}
}
func ParseArray(list []int) error {
for i := 0; i < len(list); i += 4 {
//log.Println(strconv.Itoa(i) + " -> " + strconv.Itoa(list[i]))
cmd := list[i]
if cmd == 99 {
return nil
}
sum := 0
mode1 := 0
mode2 := 0
//mode3 := 0
count := CountDigits(cmd)
if count > 2 {
cmdStr := strconv.Itoa(cmd)
cmd, _ = strconv.Atoi(cmdStr[len(cmdStr)-2:])
if count >= 3 {
mode1, _ = strconv.Atoi(cmdStr[len(cmdStr)-3 : len(cmdStr)-2])
}
if count >= 4 {
mode2, _ = strconv.Atoi(cmdStr[len(cmdStr)-4 : len(cmdStr)-3])
}
if count >= 5 {
//mode3, _ = strconv.Atoi(cmdStr[len(cmdStr)-4:len(cmdStr)-3])
}
}
if cmd == 1 {
num1 := -1
num2 := -1
if mode1 == 1 {
num1 = list[i+1]
} else {
num1 = list[list[i+1]]
}
if mode2 == 1 {
num2 = list[i+2]
} else {
num2 = list[list[i+2]]
}
sum = num1 + num2
list[list[i+3]] = sum
} else if cmd == 2 {
num1 := -1
num2 := -1
if mode1 == 1 {
num1 = list[i+1]
} else {
num1 = list[list[i+1]]
}
if mode2 == 1 {
num2 = list[i+2]
} else {
num2 = list[list[i+2]]
}
sum = num1 * num2
list[list[i+3]] = sum
} else if cmd == 3 {
var input int
fmt.Scanf("%d", &input)
list[list[i+1]] = input
i = i - 2
} else if cmd == 4 {
if mode1 == 1 {
fmt.Print(list[i+1])
} else {
fmt.Print(list[list[i+1]])
}
i = i - 2
} else if cmd == 5 {
num1 := -1
if mode1 == 1 {
num1 = list[i+1]
} else {
num1 = list[list[i+1]]
}
num2 := -1
if mode2 == 1 {
num2 = list[i+2]
} else {
num2 = list[list[i+2]]
}
if num1 != 0 {
i = num2 - 4
} else {
i -= 1
}
} else if cmd == 6 {
num1 := -1
if mode1 == 1 {
num1 = list[i+1]
} else {
num1 = list[list[i+1]]
}
num2 := -1
if mode2 == 1 {
num2 = list[i+2]
} else {
num2 = list[list[i+2]]
}
if num1 == 0 {
i = num2 - 4
} else {
i -= 1
}
} else if cmd == 7 {
num1 := -1
if mode1 == 1 {
num1 = list[i+1]
} else {
num1 = list[list[i+1]]
}
num2 := -1
if mode2 == 1 {
num2 = list[i+2]
} else {
num2 = list[list[i+2]]
}
if num1 < num2 {
list[list[i+3]] = 1
} else {
list[list[i+3]] = 0
}
} else if cmd == 8 {
num1 := -1
if mode1 == 1 {
num1 = list[i+1]
} else {
num1 = list[list[i+1]]
}
num2 := -1
if mode2 == 1 {
num2 = list[i+2]
} else {
num2 = list[list[i+2]]
}
if num1 == num2 {
list[list[i+3]] = 1
} else {
list[list[i+3]] = 0
}
} else {
return errors.New("Wrong Command (" + strconv.Itoa(list[i]) + ") at position " + strconv.Itoa(i))
}
}
return nil
}
func CountDigits(i int) int {
count := 0
for i != 0 {
i /= 10
count = count + 1
}
return count
}