mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
updated day02 solutions
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
Repo for all of my advent of code 2019 challenges in GoLang.
|
||||
|
||||
https://adventofcode.com/2019
|
||||
# Advent of Code 2019
|
||||
Language: GoLang.
|
||||
[https://adventofcode.com/2019](https://adventofcode.com/2019)
|
||||
|
||||
---
|
||||
## Summary of Days
|
||||
Day | Name | Type of Algo
|
||||
--- | --- | ---
|
||||
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 |
|
||||
@@ -0,0 +1 @@
|
||||
1, 12, 2, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 6, 19, 1, 19, 5, 23, 2, 13, 23, 27, 1, 10, 27, 31, 2, 6, 31, 35, 1, 9, 35, 39, 2, 10, 39, 43, 1, 43, 9, 47, 1, 47, 9, 51, 2, 10, 51, 55, 1, 55, 9, 59, 1, 59, 5, 63, 1, 63, 6, 67, 2, 6, 67, 71, 2, 10, 71, 75, 1, 75, 5, 79, 1, 9, 79, 83, 2, 83, 10, 87, 1, 87, 6, 91, 1, 13, 91, 95, 2, 10, 95, 99, 1, 99, 6, 103, 2, 13, 103, 107, 1, 107, 2, 111, 1, 111, 9, 0, 99, 2, 14, 0, 0
|
||||
@@ -1,67 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func calcValue(input []int, target int, noun int, verb int) bool {
|
||||
// update the 1 and 2 values here
|
||||
input[1] = noun
|
||||
input[2] = verb
|
||||
|
||||
// fmt.Println(input)
|
||||
|
||||
// loop through all "instructions"
|
||||
for i := 0; i < len(input); i += 4 {
|
||||
// check opertor type (1 2 or 99)
|
||||
operator := input[i]
|
||||
// if it's not 99 (don't terminate, and run a calculation)
|
||||
if operator != 99 {
|
||||
// grab the two values to be added or multiplied
|
||||
value1 := input[input[i+1]]
|
||||
value2 := input[input[i+2]]
|
||||
|
||||
// grab the index that will get the new value
|
||||
indexToUpdate := input[i+3]
|
||||
|
||||
// perform calcuation based on operator value
|
||||
if operator == 1 {
|
||||
input[indexToUpdate] = value1 + value2
|
||||
} else if operator == 2 {
|
||||
input[indexToUpdate] = value1 * value2
|
||||
}
|
||||
} else {
|
||||
// if it is 99, break out of this loop
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// if the target value is found, return true
|
||||
if input[0] == target {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
// defualt input slice, index 1 and 2 will be replaced
|
||||
input := []int{1, 12, 2, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 6, 19, 1, 19, 5, 23, 2, 13, 23, 27, 1, 10, 27, 31, 2, 6, 31, 35, 1, 9, 35, 39, 2, 10, 39, 43, 1, 43, 9, 47, 1, 47, 9, 51, 2, 10, 51, 55, 1, 55, 9, 59, 1, 59, 5, 63, 1, 63, 6, 67, 2, 6, 67, 71, 2, 10, 71, 75, 1, 75, 5, 79, 1, 9, 79, 83, 2, 83, 10, 87, 1, 87, 6, 91, 1, 13, 91, 95, 2, 10, 95, 99, 1, 99, 6, 103, 2, 13, 103, 107, 1, 107, 2, 111, 1, 111, 9, 0, 99, 2, 14, 0, 0}
|
||||
|
||||
// brute force to try all options for nouns and verbs
|
||||
outer:
|
||||
for i := 0; i < 100; i++ {
|
||||
for j := 0; j < 100; j++ {
|
||||
// crete a copy of the input slice
|
||||
clone := make([]int, 120)
|
||||
copy(clone, input)
|
||||
|
||||
// if the calcValue function returns true, break out of the loops and print the values to the console
|
||||
if calcValue(clone, 19690720, i, j) == true {
|
||||
// print answers to console (manually add to advent of code)
|
||||
fmt.Println("noun is", i, "verb is", j)
|
||||
fmt.Println("actual result value noun * 10 + verb = ", i*100+j)
|
||||
|
||||
// use labeled outer loop to break out of both for loops
|
||||
break outer
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// read the input file and place into slice of strings
|
||||
inputFile := util.ReadFile("../input.txt")
|
||||
splitStrings := strings.Split(inputFile, ", ")
|
||||
|
||||
// convert to slice of numbers
|
||||
inputNumbers := make([]int, len(splitStrings))
|
||||
for i, v := range splitStrings {
|
||||
inputNumbers[i], _ = strconv.Atoi(v)
|
||||
}
|
||||
|
||||
// start running the step function
|
||||
step(inputNumbers, 0)
|
||||
fmt.Println("Final value at address 0 is:", inputNumbers[0])
|
||||
}
|
||||
|
||||
// step will read the next 4 values in the input `sli` and make updates according to the opcodes
|
||||
func step(sli []int, index int) bool {
|
||||
if sli[index] == 99 {
|
||||
return false
|
||||
}
|
||||
opcode, two, three, four := read(sli, index)
|
||||
switch opcode {
|
||||
case 1:
|
||||
sli[four] = sli[two] + sli[three]
|
||||
case 2:
|
||||
sli[four] = sli[two] * sli[three]
|
||||
}
|
||||
// recursively call itself & increment index value...
|
||||
return step(sli, index+4)
|
||||
}
|
||||
|
||||
// this read function may be necessary later as the intcode thingy becomes more complex
|
||||
func read(sli []int, index int) (int, int, int, int) {
|
||||
return sli[index], sli[index+1], sli[index+2], sli[index+3]
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"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)
|
||||
}
|
||||
|
||||
// brute force to try all options for nouns and verbs
|
||||
for i := 0; i < 100; i++ {
|
||||
for j := 0; j < 100; j++ {
|
||||
// crete a copy of the inputNumbers slice
|
||||
clone := make([]int, 120)
|
||||
copy(clone, inputNumbers)
|
||||
clone[1] = i
|
||||
clone[2] = j
|
||||
|
||||
// run step on the cloned slice
|
||||
step(clone, 0)
|
||||
// check if the zero address is equal to the AoC value
|
||||
if clone[0] == 19690720 {
|
||||
// print answers to console (manually add to advent of code)
|
||||
fmt.Println("noun is", i, "verb is", j)
|
||||
fmt.Println("actual result value noun * 10 + verb = ", i*100+j)
|
||||
|
||||
// return to end main function
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// step will read the next 4 values in the input `sli` and make updates
|
||||
// according to the opcodes
|
||||
func step(sli []int, index int) bool {
|
||||
if sli[index] == 99 {
|
||||
return false
|
||||
}
|
||||
opcode, two, three, four := read(sli, index)
|
||||
switch opcode {
|
||||
case 1:
|
||||
sli[four] = sli[two] + sli[three]
|
||||
case 2:
|
||||
sli[four] = sli[two] * sli[three]
|
||||
}
|
||||
// recursively call itself & increment index value...
|
||||
return step(sli, index+4)
|
||||
}
|
||||
|
||||
func read(sli []int, index int) (int, int, int, int) {
|
||||
return sli[index], sli[index+1], sli[index+2], sli[index+3]
|
||||
}
|
||||
Reference in New Issue
Block a user