From a7ba598a54a8883c83cb9ef2b04d653eecd92e28 Mon Sep 17 00:00:00 2001 From: j-james Date: Sun, 11 Dec 2022 18:15:44 -0800 Subject: Reorganize 2019 solutions --- 2019/go/day01.go | 40 ++++++++++++++ 2019/go/day02.go | 66 ++++++++++++++++++++++ 2019/go/day03.go | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2019/go/day04.go | 68 +++++++++++++++++++++++ 2019/go/day05.go | 131 +++++++++++++++++++++++++++++++++++++++++++ 2019/go/day06.go | 25 +++++++++ 2019/go/day07.go | 141 ++++++++++++++++++++++++++++++++++++++++++++++ 2019/go/day08.go | 87 +++++++++++++++++++++++++++++ 2019/go/day09.go | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2019/go/day10.go | 47 ++++++++++++++++ 10 files changed, 927 insertions(+) create mode 100644 2019/go/day01.go create mode 100644 2019/go/day02.go create mode 100644 2019/go/day03.go create mode 100644 2019/go/day04.go create mode 100644 2019/go/day05.go create mode 100644 2019/go/day06.go create mode 100644 2019/go/day07.go create mode 100644 2019/go/day08.go create mode 100644 2019/go/day09.go create mode 100644 2019/go/day10.go (limited to '2019/go') diff --git a/2019/go/day01.go b/2019/go/day01.go new file mode 100644 index 0000000..cf27820 --- /dev/null +++ b/2019/go/day01.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strconv" +) + +func main() { + start, sum, fuelsum := 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] == '\n' { + arg, err := strconv.Atoi(string(file[start:i])) // i-1?? + if err != nil { + panic(err) + } + start = i + 1 + sum += (arg / 3) - 2 + fuelsum += tyranny(arg) + } + } + fmt.Println(sum) + fmt.Println(fuelsum) +} + +func tyranny(mass int) int { + subtotal := 0 + for fuel := (mass / 3) - 2; fuel >= 0; fuel = (fuel / 3) - 2 { + subtotal += fuel + } + return subtotal +} diff --git a/2019/go/day02.go b/2019/go/day02.go new file mode 100644 index 0000000..8108c87 --- /dev/null +++ b/2019/go/day02.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strconv" +) + +func main() { + var slice []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) + } + slice = append(slice, arg) + start = i + 1 + size++ + } + } + noun, verb := edocpo(slice, 19690720) + slice[1] = 12 + slice[2] = 2 + fmt.Println(opcode(slice)[0]) + fmt.Println(100*noun + verb) +} + +func opcode(slice []int) []int { + for i := 0; i < len(slice); i += 4 { + if slice[i] == 1 { + slice[slice[i+3]] = slice[slice[i+1]] + slice[slice[i+2]] + } else if slice[i] == 2 { + slice[slice[i+3]] = slice[slice[i+1]] * slice[slice[i+2]] + } else if slice[i] == 99 { + return slice + } else { + fmt.Println("Unsupported code", slice[i], "at", i) + os.Exit(0) + } + } + return slice +} + +func edocpo(slice []int, output int) (int, int) { + var ecils []int + for i := 0; i < len(slice); i++ { + for j := 0; j < len(slice); j++ { + ecils = append([]int(nil), slice...) // reset ecils to slice + ecils[1], ecils[2] = i, j + if opcode(ecils)[0] == output { + return i, j + } + } + } + return -1, -1 +} diff --git a/2019/go/day03.go b/2019/go/day03.go new file mode 100644 index 0000000..2c79a18 --- /dev/null +++ b/2019/go/day03.go @@ -0,0 +1,156 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" +) + +func main() { + var directions [][]string + var temp []string + start := 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 string(file[i]) == "," || file[i] == '\n' { + temp = append(temp, string(file[start:i])) + start = i + 1 + if file[i] == '\n' { + directions[] = append(directions[], temp) + temp = nil + } + } + } + fmt.Println(directions) + + // grid [][]int := boolgrid(directions) + +} + +// ax, ay, at := taxi(directions[0]) +// bx, by, bt := taxi(directions[1]) +// for i := 0; i < at; i++ { +// for j := 0; j < at; j++ { +// for k := 0; k < +// ax[i][j] +// } +// } +// abx, aby + +// grid = taxi(directions, taxi(directions2, grid)) +// min, x, y := 4000, 0, 0 +// for i := 0; i < len(grid); i++ { +// for j := 0; j < len(grid); j++ { +// x, y = int(math.Abs(float64(i-500))), int(math.Abs(float64(j-500))) +// if grid[i][j] == 2 && x+y < min { +// fmt.Println(i-500, j-500) +// min = x + y +// } + +// } +// } +// fmt.Println("Manhattan distance to intersection:", min) +// } + +// func taxi(directions []string) ([]int, []int, int) { +// var horislice, vertslice []int +// total := 0 +// for i := 0; i < len(directions); i++ { +// direction := directions[i][0] +// displacement, err := strconv.Atoi(directions[i][1:]) +// if err != nil { +// panic(err) +// } +// switch direction { +// case 'L': +// for j := 0; j < displacement; j++ { +// horislice = append(horislice, horislice[total]-j) +// vertslice = append(vertslice, vertslice[total]) +// total++ +// } +// case 'R': +// for j := 0; j < displacement; j++ { +// horislice = append(horislice, horislice[total]+j) +// vertslice = append(vertslice, vertslice[total]) +// total++ +// } +// case 'U': +// for j := 0; j < displacement; j++ { +// vertslice = append(vertslice, vertslice[total]+j) +// horislice = append(horislice, horislice[total]) +// total++ +// } +// case 'D': +// for j := 0; j < displacement; j++ { +// vertslice = append(vertslice, vertslice[total]-j) +// horislice = append(horislice, horislice[total]) +// total++ +// } +// default: +// fmt.Println("Unhandled direction", string(direction)) +// os.Exit(0) +// } +// } +// return horislice, vertslice, total +// } + +// func taxi(directions []string, grid [4000][4000]int) [4000][4000]int { // sign convention: an array, left to right, top to bottom, beginning in the top left corner +// x, y := len(grid)/2, len(grid[0])/2 // start in center +// var boolgrid [4000][4000]bool +// for i := 0; i < len(directions); i++ { +// direction := directions[i][0] +// displacement, err := strconv.Atoi(directions[i][1:]) +// if err != nil { +// panic(err) +// } +// switch direction { +// case 'L': +// for j := 0; j < displacement; j++ { +// x-- +// if !boolgrid[x][y] { +// boolgrid[x][y] = true +// } +// } +// case 'R': +// for j := 0; j < displacement; j++ { +// x++ +// if !boolgrid[x][y] { +// boolgrid[x][y] = true +// } +// } +// case 'U': +// for j := 0; j < displacement; j++ { +// y-- +// if !boolgrid[x][y] { +// boolgrid[x][y] = true +// } +// } +// case 'D': +// for j := 0; j < displacement; j++ { +// y++ +// if !boolgrid[x][y] { +// boolgrid[x][y] = true +// } +// } +// default: +// fmt.Println("Unhandled direction", string(direction)) +// os.Exit(0) +// } +// fmt.Println(string(direction), displacement, x, y) +// } +// for i := 0; i < len(grid); i++ { +// for j := 0; j < len(grid[0]); j++ { +// if boolgrid[i][j] { +// grid[i][j]++ +// } +// } +// } +// return grid // each element is equal to the numbers of wires on it +// } diff --git a/2019/go/day04.go b/2019/go/day04.go new file mode 100644 index 0000000..7c901dc --- /dev/null +++ b/2019/go/day04.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "io/ioutil" + "math" + "os" + "strconv" +) + +func main() { + smallest, largest, total, details := -1, -1, 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 string(file[i]) == "-" { + smallest, err = strconv.Atoi(string(file[0:i])) + if err != nil { + panic(err) + } + largest, err = strconv.Atoi(string(file[i+1 : len(file)-1])) + if err != nil { + panic(err) + } + break + } + } + + for i := smallest; i <= largest; i++ { + if criteria(i, false) { + total++ + } + if criteria(i, true) { + details++ + } + } + fmt.Println(total) + fmt.Println(details) +} + +func criteria(password int, stage bool) bool { + prev := 0 + for i := 0; i < 6; i++ { + if password/int(math.Pow(10, float64(5-i)))%10 < prev { + return false + } + prev = password / int(math.Pow(10, float64(5-i))) % 10 + } + pascii := strconv.Itoa(password) + for i := 0; i < len(pascii)-1; i++ { + if string(pascii[i]) == string(pascii[i+1]) { + if !(len(pascii)-i > 2 && string(pascii[i]) == string(pascii[i+2])) { + if !stage { // Gross hack + return true + } + if !(i > 0 && string(pascii[i]) == string(pascii[i-1])) { + return true + } + } + } + } + return false +} diff --git a/2019/go/day05.go b/2019/go/day05.go new file mode 100644 index 0000000..9ea4699 --- /dev/null +++ b/2019/go/day05.go @@ -0,0 +1,131 @@ +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 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/2019/go/day06.go b/2019/go/day06.go new file mode 100644 index 0000000..646313d --- /dev/null +++ b/2019/go/day06.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/2019/go/day07.go b/2019/go/day07.go new file mode 100644 index 0000000..d71d827 --- /dev/null +++ b/2019/go/day07.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/2019/go/day08.go b/2019/go/day08.go new file mode 100644 index 0000000..74be0e1 --- /dev/null +++ b/2019/go/day08.go @@ -0,0 +1,87 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strconv" +) + +func main() { + if len(os.Args) < 2 { + panic("runtime error: missing operand") + } + file, err := ioutil.ReadFile(os.Args[1]) + if err != nil { + panic(err) + } + image := sliceify(file, 25, 6, len(file)/(25*6)) + + checksum(image) + decode(image) +} + +func sliceify(file []byte, width int, height int, depth int) [][][]int { + var image [][][]int + pointer := 0 + + for i := 0; i < depth; i++ { + var b [][]int + for j := 0; j < height; j++ { + var a []int + for k := 0; k < width; k++ { + element, err := strconv.Atoi(string(file[pointer])) + if err != nil { + panic(err) + } + a = append(a, element) + pointer++ + } + b = append(b, a) + } + image = append(image, b) + } + return image +} + +func count(layer [][]int, val int) int { + total := 0 + for i := 0; i < len(layer); i++ { + for j := 0; j < len(layer[i]); j++ { + if layer[i][j] == val { + total++ + } + } + } + return total +} + +func checksum(image [][][]int) { + min := count(image[0], 0) + layer := -1 + for i := 0; i < len(image); i++ { + total := count(image[i], 0) + if total < min { + min = total + layer = i + } + } + fmt.Println(count(image[layer], 1) * count(image[layer], 2)) +} + +func decode(image [][][]int) { + for i := 0; i < len(image[0]); i++ { + for j := 0; j < len(image[0][0]); j++ { + for k := 0; k < len(image); k++ { + if image[k][i][j] == 0 { + fmt.Print(" ") + break + } else if image[k][i][j] == 1 { + fmt.Print("X") + break + } + } + } + fmt.Println() + } +} diff --git a/2019/go/day09.go b/2019/go/day09.go new file mode 100644 index 0000000..432d780 --- /dev/null +++ b/2019/go/day09.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/2019/go/day10.go b/2019/go/day10.go new file mode 100644 index 0000000..562299a --- /dev/null +++ b/2019/go/day10.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