From fa785e8d1fc8679a5243a3aea3aa55a690adfdf0 Mon Sep 17 00:00:00 2001 From: j-james Date: Tue, 23 Jun 2020 11:56:21 -0700 Subject: Days Six through Ten, in varying states of completeness --- README.md | 1 + nine.go | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ seven.go | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ six.go | 25 ++++++++++ ten.go | 47 ++++++++++++++++++ 5 files changed, 380 insertions(+) create mode 100644 README.md create mode 100644 nine.go create mode 100644 seven.go create mode 100644 six.go create mode 100644 ten.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..ebf136e --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# advent of code diff --git a/nine.go b/nine.go new file mode 100644 index 0000000..432d780 --- /dev/null +++ b/nine.go @@ -0,0 +1,166 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strconv" +) + +func main() { + var memory []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) + } + memory = append(memory, arg) + start = i + 1 + size++ + } + } + // fmt.Println(execute(memory, size)) + // noun, verb := etucexe(memory, size, 19690720) + // fmt.Println(100*noun + verb) + execute(memory, size) +} + +func split(memory []int, i, size, relative int) (int, int, int) { + mode := memory[i] / 100 + three, two, one := i+3, i+2, i+1 + if size-i > 1 { + if mode%10 == 0 { + one = memory[i+1] + } else if mode%10 == 2 { + one = relative + memory[i+1] + } + if size-i > 2 { + if mode/10%10 == 0 { + two = memory[i+2] + } else if mode%10 == 2 { + two = relative + memory[i+2] + } + if size-i > 3 { + if mode/100 == 0 { + three = memory[i+3] + } else if mode%10 == 2 { + three = relative + memory[i+3] + } + } + } + } + return three, two, one +} + +func execute(memory []int, size int) []int { + /* todo: said "memory" functionality could be [pos, value]int + * i.e. when memory outside initial bounds wants to be accessed, + * it writes to a new element of this slice + * to access elements of the array, iterate through while checking each position + * additionally, to ensure there aren't conflicting "positions" in the array, + * do the above iteration process, if not, appends (this requires a total int) + */ + /* + var swap [][2]int + */ + + // off-topic todo: https://www.golangprograms.com/example-arrays-of-arrays-arrays-of-slices-slices-of-arrays-and-slices-of-slices.html + // does this mean i can remove total / size because if so YES + for i := 0; i < 4000; i++ { // bad (example: no auto-termination due to 0 replacing nothingness) + memory = append(memory, 0) + } + relative := 0 // initial value of the relative base + for i := 0; i < len(memory); { + // bounds check + opcode := memory[i] % 100 + three, two, one := split(memory, i, size, relative) + //fmt.Println("test:", opcode) + // actually what might work better than replacing all below would be + // checking if the next three values are out of bounds or not + // and then replacing them + // no bad idea large addition exists + switch opcode { + case 1: // adds + memory[three] = memory[one] + memory[two] + i += 4 + case 2: // multiplies + // if out of bounds + // swap = append(swap, {memory[three], memory[one] * memory[two]}) + // else + memory[three] = memory[one] * memory[two] + i += 4 + case 3: // input + var input int + fmt.Print("Input: ") + resp, err := fmt.Scanf("%d", &input) // interesting note: anything following a valid integer is run by bash + if err != nil { + fmt.Println(resp, err) + os.Exit(0) + } + // out of bounds check + memory[one] = input // not affected by modes + i += 2 + case 4: // output + fmt.Println(memory[one]) + i += 2 + case 5: // jump-if-true + if memory[one] != 0 { + i = memory[two] + } else { + i += 3 + } + case 6: // jump-if-false + if memory[one] == 0 { + i = memory[two] + } else { + i += 3 + } + case 7: // less than + if memory[one] < memory[two] { + memory[memory[i+3]] = 1 // instructions: _position_ given by the third parameter + } else { + memory[memory[i+3]] = 0 + } + i += 4 + case 8: // equals + if memory[one] == memory[two] { + memory[memory[i+3]] = 1 + } else { + memory[memory[i+3]] = 0 + } + i += 4 + case 9: // adjusts the relative base + relative += memory[one] // interesting behavior when changing to i+1 + i += 2 + case 99: // terminate + return memory + default: + fmt.Println("Unsupported code", opcode, "at", i) + os.Exit(0) + } + } + return memory +} + +func etucexe(memory []int, size, output int) (int, int) { + var volatile []int + for i := 0; i < len(memory); i++ { + for j := 0; j < len(memory); j++ { + volatile = append([]int(nil), memory...) // reset volatile to memory + volatile[1], volatile[2] = i, j + if execute(volatile, size)[0] == output { + return i, j + } + } + } + return -1, -1 +} diff --git a/seven.go b/seven.go new file mode 100644 index 0000000..d71d827 --- /dev/null +++ b/seven.go @@ -0,0 +1,141 @@ +package main + +import ( + "fmt" + "os" + "strconv" +) + +func main() { + var memory []int + size := 0 + for i := 1; i < len(os.Args); i++ { + arg, err := strconv.Atoi(os.Args[i]) + if err != nil { + panic(err) + } + memory = append(memory, arg) + size++ + } + // fmt.Println(execute(memory, size)) + // noun, verb := etucexe(memory, size, 19690720) + // fmt.Println(100*noun + verb) + // execute(memory, size) + fmt.Println(thrust(memory, size)) +} + +func thrust(memory []int, size int) int { // 120 total + max := 0 + for a := 0; a < 5; a++ { + for b:=0; b < 4; b++ { + for c:=0; + } + + + + a, b, c, d, e := i/10000, i/1000%10, i/100%100, i/10%1000, i%10000 + a = execute(memory, size) // stdin fill this with a, 0 + b = execute(memory, size) // stdin fill this with b, 0 + c = execute(memory, size) // stdin fill this with c, 0 + d = execute(memory, size) // stdin fill this with d, 0 + e = execute(memory, size) // stdin fill this with e, 0 + if (e > max) { + max = e + } + return max +} + +func split(memory []int, i, size int) (int, int, int) { + mode := memory[i] / 100 + three, two, one := i+3, i+2, i+1 + if size-i > 1 { + if mode%10 == 0 { + one = memory[i+1] + } + if size-i > 2 { + if mode/10%10 == 0 { + two = memory[i+2] + } + if size-i > 3 { + if mode/100 == 0 { + three = memory[i+3] + } + } + } + } + return three, two, one +} + +func execute(memory []int, size int) []int { + for i := 0; i < len(memory); { + opcode := memory[i] % 100 + three, two, one := split(memory, i, size) + switch opcode { + case 1: // adds + memory[three] = memory[one] + memory[two] + i += 4 + case 2: // multiplies + memory[three] = memory[one] * memory[two] + i += 4 + case 3: // input + var input int + fmt.Print("Input: ") + resp, err := fmt.Scanf("%d", &input) + if err != nil { + fmt.Println(resp, err) + os.Exit(0) + } + memory[one] = input // not affected by modes + i += 2 + case 4: // output + fmt.Println(memory[one]) + i += 2 + case 5: // jump-if-true + if memory[one] != 0 { + i = memory[two] // ??? + } else { + i += 3 + } + case 6: // jump-if-false + if memory[one] == 0 { + i = memory[two] // ??? + } else { + i += 3 + } + case 7: // less than + if memory[one] < memory[two] { + memory[memory[i+3]] = 1 + } else { + memory[memory[i+3]] = 0 + } + i += 4 + case 8: // equals + if memory[one] == memory[two] { + memory[memory[i+3]] = 1 + } else { + memory[memory[i+3]] = 0 + } + i += 4 + case 99: // terminate + return memory + default: + fmt.Println("Unsupported code", opcode, "at", i) + os.Exit(0) + } + } + return memory +} + +func etucexe(memory []int, size, output int) (int, int) { + var volatile []int + for i := 0; i < len(memory); i++ { + for j := 0; j < len(memory); j++ { + volatile = append([]int(nil), memory...) // reset volatile to memory + volatile[1], volatile[2] = i, j + if execute(volatile, size)[0] == output { + return i, j + } + } + } + return -1, -1 +} diff --git a/six.go b/six.go new file mode 100644 index 0000000..646313d --- /dev/null +++ b/six.go @@ -0,0 +1,25 @@ +package main + +import ( + "io/ioutil" + "os" +) + +func main() { + 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 +} + +func orbits(planet string) []string { + + var chain []string + + return chain +} diff --git a/ten.go b/ten.go new file mode 100644 index 0000000..562299a --- /dev/null +++ b/ten.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" +) + +func main() { + var region [][]bool + x, y, size := 0, 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] == '#' { + region[x][y] = true + x++ + } else if file[i] == '\n' { + x = 0 + y++ + } + } + max := 0 + fmt.Println("Maximum detectable asteroids:", detect(region, x, y)) +} + +func detect(region [][]bool, width, height int) int { + for i:= 0; i < width; i++ { + for j := 0; j < height; j++ { + if region[i][j] { + for k:= 0; k < width; k++ { + for l := 0; l < width; l++ { + + } + } + } + } + } + + slope := + return +} -- cgit v1.2.3-70-g09d2