mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
day05 solutions updated
This commit is contained in:
@@ -4,10 +4,10 @@ Language: GoLang.
|
||||
|
||||
---
|
||||
## Summary of Days
|
||||
Day | Name | Type of Algo
|
||||
Day | Name | Type of Algo & Notes
|
||||
--- | --- | ---
|
||||
1 | The Tyranny of the Rocket Equation | - Simple math problem
|
||||
2 | Program Alarm | - Intro to the crazy Intcode problems that are half the AoC days... <br> - Array (slice...) manipulation <br> - I used recursion
|
||||
3 | Crossed Wires | - Geometry kind of algo, finding intersections of lines on a grid
|
||||
4 | Secure Container | - May appear math-y, but it's really a string manipulation problem
|
||||
5 |
|
||||
5 | Sunny with a Chance of Asteroids | - Yay more Intcode!........
|
||||
+1
-1
@@ -1 +1 @@
|
||||
3,225,1,225,6,6,1100,1,238,225,104,0,1,192,154,224,101,-161,224,224,4,224,102,8,223,223,101,5,224,224,1,223,224,223,1001,157,48,224,1001,224,-61,224,4,224,102,8,223,223,101,2,224,224,1,223,224,223,1102,15,28,225,1002,162,75,224,1001,224,-600,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,102,32,57,224,1001,224,-480,224,4,224,102,8,223,223,101,1,224,224,1,224,223,223,1101,6,23,225,1102,15,70,224,1001,224,-1050,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,101,53,196,224,1001,224,-63,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1101,64,94,225,1102,13,23,225,1101,41,8,225,2,105,187,224,1001,224,-60,224,4,224,1002,223,8,223,101,6,224,224,1,224,223,223,1101,10,23,225,1101,16,67,225,1101,58,10,225,1101,25,34,224,1001,224,-59,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,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,1108,226,226,224,102,2,223,223,1005,224,329,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,344,1001,223,1,223,107,677,226,224,102,2,223,223,1005,224,359,101,1,223,223,7,677,226,224,102,2,223,223,1005,224,374,101,1,223,223,108,226,226,224,102,2,223,223,1006,224,389,101,1,223,223,1007,677,677,224,102,2,223,223,1005,224,404,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,419,101,1,223,223,1107,226,677,224,1002,223,2,223,1005,224,434,1001,223,1,223,1108,226,677,224,102,2,223,223,1005,224,449,101,1,223,223,108,226,677,224,102,2,223,223,1005,224,464,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,494,101,1,223,223,1008,226,677,224,102,2,223,223,1006,224,509,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,524,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,539,1001,223,1,223,1107,226,226,224,1002,223,2,223,1006,224,554,1001,223,1,223,7,226,226,224,1002,223,2,223,1006,224,569,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,584,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,599,101,1,223,223,1007,226,677,224,1002,223,2,223,1006,224,614,1001,223,1,223,8,677,677,224,1002,223,2,223,1005,224,629,101,1,223,223,107,677,677,224,102,2,223,223,1005,224,644,101,1,223,223,1108,677,226,224,102,2,223,223,1005,224,659,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226
|
||||
3,225,1,225,6,6,1100,1,238,225,104,0,1,192,154,224,101,-161,224,224,4,224,102,8,223,223,101,5,224,224,1,223,224,223,1001,157,48,224,1001,224,-61,224,4,224,102,8,223,223,101,2,224,224,1,223,224,223,1102,15,28,225,1002,162,75,224,1001,224,-600,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,102,32,57,224,1001,224,-480,224,4,224,102,8,223,223,101,1,224,224,1,224,223,223,1101,6,23,225,1102,15,70,224,1001,224,-1050,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,101,53,196,224,1001,224,-63,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1101,64,94,225,1102,13,23,225,1101,41,8,225,2,105,187,224,1001,224,-60,224,4,224,1002,223,8,223,101,6,224,224,1,224,223,223,1101,10,23,225,1101,16,67,225,1101,58,10,225,1101,25,34,224,1001,224,-59,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,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,1108,226,226,224,102,2,223,223,1005,224,329,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,344,1001,223,1,223,107,677,226,224,102,2,223,223,1005,224,359,101,1,223,223,7,677,226,224,102,2,223,223,1005,224,374,101,1,223,223,108,226,226,224,102,2,223,223,1006,224,389,101,1,223,223,1007,677,677,224,102,2,223,223,1005,224,404,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,419,101,1,223,223,1107,226,677,224,1002,223,2,223,1005,224,434,1001,223,1,223,1108,226,677,224,102,2,223,223,1005,224,449,101,1,223,223,108,226,677,224,102,2,223,223,1005,224,464,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,494,101,1,223,223,1008,226,677,224,102,2,223,223,1006,224,509,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,524,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,539,1001,223,1,223,1107,226,226,224,1002,223,2,223,1006,224,554,1001,223,1,223,7,226,226,224,1002,223,2,223,1006,224,569,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,584,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,599,101,1,223,223,1007,226,677,224,1002,223,2,223,1006,224,614,1001,223,1,223,8,677,677,224,1002,223,2,223,1005,224,629,101,1,223,223,107,677,677,224,102,2,223,223,1005,224,644,101,1,223,223,1108,677,226,224,102,2,223,223,1005,224,659,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226
|
||||
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// read the input file, modify it to a slice of numbers
|
||||
inputFile := util.ReadFile("../input.txt")
|
||||
splitStrings := strings.Split(inputFile, ",")
|
||||
|
||||
inputNumbers := make([]int, len(splitStrings))
|
||||
for i, v := range splitStrings {
|
||||
inputNumbers[i], _ = strconv.Atoi(v)
|
||||
}
|
||||
|
||||
input := 1
|
||||
step(inputNumbers, 0, input)
|
||||
}
|
||||
|
||||
// step will read the next 4 values in the input `sli` and make updates
|
||||
// according to the opcodes
|
||||
func step(sli []int, index, input int) int {
|
||||
// read the instruction, opcode and the indexes where the params point to
|
||||
opcode, paramIndexes := getOpCodeAndIndexes(sli, index)
|
||||
|
||||
var output, jump int
|
||||
switch opcode {
|
||||
// 99: Terminates program
|
||||
case 99:
|
||||
fmt.Println("Terminating")
|
||||
return -1
|
||||
case 1: // 1: Add next two paramIndexes, store in third
|
||||
sli[paramIndexes[2]] = sli[paramIndexes[0]] + sli[paramIndexes[1]]
|
||||
jump = 4
|
||||
case 2: // 2: Multiply next two and store in third
|
||||
sli[paramIndexes[2]] = sli[paramIndexes[0]] * sli[paramIndexes[1]]
|
||||
jump = 4
|
||||
case 3: // 3: Takes one input and saves it to position of one parameter
|
||||
sli[paramIndexes[0]] = input
|
||||
jump = 2
|
||||
case 4: // 4: outputs its input value
|
||||
output = sli[paramIndexes[0]]
|
||||
fmt.Printf("Opcode 4 output: %v\n", output)
|
||||
jump = 2
|
||||
default:
|
||||
log.Fatal("Error: unknown opcode: ", opcode)
|
||||
}
|
||||
// recursively call itself & jump index value...
|
||||
return step(sli, index+jump, output)
|
||||
}
|
||||
|
||||
/*
|
||||
getOpCodeAndIndexes will parse the instruction at sli[index]
|
||||
- opcode will be the left two digits, mod by 100 will get that
|
||||
- rest of instructions will be grabbed via mod 10
|
||||
- these also have to be parsed for the
|
||||
*/
|
||||
func getOpCodeAndIndexes(sli []int, index int) (int, [3]int) {
|
||||
instruction := sli[index]
|
||||
|
||||
// opcode is the lowest two digits, so mod by 100
|
||||
opcode := instruction % 100
|
||||
instruction /= 100
|
||||
|
||||
// assign the indexes that need to be read by reading the parameter modes
|
||||
var paramIndexes [3]int
|
||||
for i := 1; i <= 3 && index+i < len(sli); i++ {
|
||||
// grab the mode with a mod, last digit
|
||||
mode := instruction % 10
|
||||
instruction /= 10
|
||||
|
||||
switch mode {
|
||||
case 1: // immediate mode, the index itself
|
||||
paramIndexes[i-1] = index + i
|
||||
case 0: // position mode, index will be the value at the index
|
||||
paramIndexes[i-1] = sli[index+i]
|
||||
}
|
||||
}
|
||||
|
||||
return opcode, paramIndexes
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// read the input file, modify it to a slice of numbers
|
||||
inputFile := util.ReadFile("../input.txt")
|
||||
// inputFile := "3,3,1107,-1,8,3,4,3,99"
|
||||
|
||||
splitStrings := strings.Split(inputFile, ",")
|
||||
|
||||
inputNumbers := make([]int, len(splitStrings))
|
||||
for i, v := range splitStrings {
|
||||
inputNumbers[i], _ = strconv.Atoi(v)
|
||||
}
|
||||
|
||||
// system ID is 5, that is the input to the intcode computer
|
||||
input := 5
|
||||
step(inputNumbers, 0, input)
|
||||
}
|
||||
|
||||
// step will read the next 4 values in the input `sli` and make updates
|
||||
// according to the opcodes
|
||||
func step(sli []int, index, input int) int {
|
||||
// read the instruction, opcode and the indexes where the params point to
|
||||
opcode, paramIndexes := getOpCodeAndIndexes(sli, index)
|
||||
|
||||
var output int
|
||||
switch opcode {
|
||||
case 99: // 99: Terminates program
|
||||
fmt.Println("Terminating...")
|
||||
return input
|
||||
case 1: // 1: Add next two paramIndexes, store in third
|
||||
sli[paramIndexes[2]] = sli[paramIndexes[0]] + sli[paramIndexes[1]]
|
||||
return step(sli, index+4, output)
|
||||
case 2: // 2: Multiply next two and store in third
|
||||
sli[paramIndexes[2]] = sli[paramIndexes[0]] * sli[paramIndexes[1]]
|
||||
return step(sli, index+4, output)
|
||||
case 3: // 3: Takes one input and saves it to position of one parameter
|
||||
sli[paramIndexes[0]] = input
|
||||
return step(sli, index+2, output)
|
||||
case 4: // 4: outputs its input value
|
||||
output = sli[paramIndexes[0]]
|
||||
fmt.Printf("Opcode 4 output: %v\n", output)
|
||||
return step(sli, index+2, output)
|
||||
// 5: jump-if-true: if first param != 0, move pointer to second param, else nothing
|
||||
case 5:
|
||||
if sli[paramIndexes[0]] != 0 {
|
||||
return step(sli, sli[paramIndexes[1]], output)
|
||||
}
|
||||
return step(sli, index+3, output)
|
||||
// 6: jump-if-false, if first param == 0 then set instruction pointer to 2nd param, else nothing
|
||||
case 6:
|
||||
if sli[paramIndexes[0]] == 0 {
|
||||
return step(sli, sli[paramIndexes[1]], output)
|
||||
}
|
||||
return step(sli, index+3, output)
|
||||
// 7: less-than, if param1 < param2 then store 1 in postion of 3rd param, else store 0
|
||||
case 7:
|
||||
if sli[paramIndexes[0]] < sli[paramIndexes[1]] {
|
||||
sli[paramIndexes[2]] = 1
|
||||
} else {
|
||||
sli[paramIndexes[2]] = 0
|
||||
}
|
||||
return step(sli, index+4, output)
|
||||
// 8: equals, if param1 == param2 then set position of 3rd param to 1, else store 0
|
||||
case 8:
|
||||
if sli[paramIndexes[0]] == sli[paramIndexes[1]] {
|
||||
sli[paramIndexes[2]] = 1
|
||||
} else {
|
||||
sli[paramIndexes[2]] = 0
|
||||
}
|
||||
return step(sli, index+4, output)
|
||||
default:
|
||||
log.Fatal("Error: unknown opcode: ", opcode)
|
||||
}
|
||||
// this should never be called b/c switch statement will always return
|
||||
return -1
|
||||
}
|
||||
|
||||
/*
|
||||
getOpCodeAndIndexes will parse the instruction at sli[index]
|
||||
- opcode will be the left two digits, mod by 100 will get that
|
||||
- rest of instructions will be grabbed via mod 10
|
||||
- these also have to be parsed for the
|
||||
*/
|
||||
func getOpCodeAndIndexes(sli []int, index int) (int, [3]int) {
|
||||
instruction := sli[index]
|
||||
|
||||
// opcode is the lowest two digits, so mod by 100
|
||||
opcode := instruction % 100
|
||||
instruction /= 100
|
||||
|
||||
// assign the indexes that need to be read by reading the parameter modes
|
||||
var paramIndexes [3]int
|
||||
for i := 1; i <= 3 && index+i < len(sli); i++ {
|
||||
// grab the mode with a mod, last digit
|
||||
mode := instruction % 10
|
||||
instruction /= 10
|
||||
|
||||
switch mode {
|
||||
case 1: // immediate mode, the index itself
|
||||
paramIndexes[i-1] = index + i
|
||||
case 0: // position mode, index will be the value at the index
|
||||
paramIndexes[i-1] = sli[index+i]
|
||||
}
|
||||
}
|
||||
|
||||
return opcode, paramIndexes
|
||||
}
|
||||
Reference in New Issue
Block a user