:work: Start with part 1 of day 7
This commit is contained in:
parent
461c4a493d
commit
194981f938
1
day7/input.txt
Normal file
1
day7/input.txt
Normal file
@ -0,0 +1 @@
|
||||
3,8,1001,8,10,8,105,1,0,0,21,46,63,76,97,118,199,280,361,442,99999,3,9,102,4,9,9,101,2,9,9,1002,9,5,9,101,4,9,9,102,2,9,9,4,9,99,3,9,101,5,9,9,102,3,9,9,101,3,9,9,4,9,99,3,9,1001,9,2,9,102,3,9,9,4,9,99,3,9,1002,9,5,9,101,4,9,9,1002,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,5,9,101,3,9,9,1002,9,5,9,1001,9,5,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99
|
205
day7/main.go
Normal file
205
day7/main.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user