Finish part 1 & part 2 of day 5

This commit is contained in:
Alphyron 2019-12-06 08:02:39 +01:00
parent 10b836e46a
commit d31498216f
3 changed files with 207 additions and 0 deletions

1
day5/input.txt Normal file
View File

@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1101,33,37,225,101,6,218,224,1001,224,-82,224,4,224,102,8,223,223,101,7,224,224,1,223,224,223,1102,87,62,225,1102,75,65,224,1001,224,-4875,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,1102,49,27,225,1101,6,9,225,2,69,118,224,101,-300,224,224,4,224,102,8,223,223,101,6,224,224,1,224,223,223,1101,76,37,224,1001,224,-113,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1101,47,50,225,102,43,165,224,1001,224,-473,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1002,39,86,224,101,-7482,224,224,4,224,102,8,223,223,1001,224,6,224,1,223,224,223,1102,11,82,225,1,213,65,224,1001,224,-102,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,1001,14,83,224,1001,224,-120,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1102,53,39,225,1101,65,76,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1107,677,226,224,1002,223,2,223,1005,224,329,101,1,223,223,8,677,226,224,102,2,223,223,1006,224,344,1001,223,1,223,108,677,677,224,1002,223,2,223,1006,224,359,1001,223,1,223,1108,226,677,224,102,2,223,223,1006,224,374,1001,223,1,223,1008,677,226,224,102,2,223,223,1005,224,389,101,1,223,223,7,226,677,224,102,2,223,223,1005,224,404,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,419,101,1,223,223,107,677,226,224,102,2,223,223,1006,224,434,101,1,223,223,7,677,677,224,1002,223,2,223,1005,224,449,101,1,223,223,108,677,226,224,1002,223,2,223,1006,224,464,101,1,223,223,1008,226,226,224,1002,223,2,223,1006,224,479,101,1,223,223,107,677,677,224,1002,223,2,223,1006,224,494,1001,223,1,223,1108,677,226,224,102,2,223,223,1005,224,509,101,1,223,223,1007,226,677,224,102,2,223,223,1005,224,524,1001,223,1,223,1008,677,677,224,102,2,223,223,1005,224,539,1001,223,1,223,1107,677,677,224,1002,223,2,223,1006,224,554,1001,223,1,223,1007,226,226,224,1002,223,2,223,1005,224,569,1001,223,1,223,7,677,226,224,1002,223,2,223,1006,224,584,1001,223,1,223,108,226,226,224,102,2,223,223,1005,224,599,1001,223,1,223,8,677,677,224,102,2,223,223,1005,224,614,1001,223,1,223,1107,226,677,224,102,2,223,223,1005,224,629,1001,223,1,223,8,226,677,224,102,2,223,223,1006,224,644,1001,223,1,223,1108,226,226,224,1002,223,2,223,1006,224,659,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,674,1001,223,1,223,4,223,99,226

205
day5/main.go Normal file
View File

@ -0,0 +1,205 @@
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
}

1
day5/main_test.go Normal file
View File

@ -0,0 +1 @@
package main