mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
day4. day5 is weird.
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func numMatching(sliceToEnd []rune) int {
|
||||||
|
// fmt.Println(sliceToEnd)
|
||||||
|
// gets in a slice from a character to the end, returns an int of the matching characters
|
||||||
|
result := 1
|
||||||
|
for i := 1; i < len(sliceToEnd); i++ {
|
||||||
|
if sliceToEnd[i] == sliceToEnd[0] {
|
||||||
|
result += 1
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// fmt.Println(result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func testNumber(num int) bool {
|
||||||
|
// cast to string to iterate through digits?
|
||||||
|
strNum := strconv.Itoa(num)
|
||||||
|
runesSlice := []rune(strNum)
|
||||||
|
|
||||||
|
duplicate := false
|
||||||
|
|
||||||
|
for i := 0; i < len(runesSlice)-1; {
|
||||||
|
if runesSlice[i] > runesSlice[i+1] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
matches := numMatching(runesSlice[i:len(runesSlice)])
|
||||||
|
if matches == 2 {
|
||||||
|
duplicate = true
|
||||||
|
i++
|
||||||
|
} else if matches == 1 {
|
||||||
|
i++
|
||||||
|
} else if matches > 2 {
|
||||||
|
i += (matches - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the entire for loop passes, return if there was a duplicate or not
|
||||||
|
return duplicate
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
start, end := 138307, 654504
|
||||||
|
possibleCombinations := 0
|
||||||
|
|
||||||
|
for i := start; i <= end; i++ {
|
||||||
|
if testNumber(i) {
|
||||||
|
possibleCombinations++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(possibleCombinations)
|
||||||
|
// fmt.Println(testNumber(111122))
|
||||||
|
// fmt.Println(testNumber(123444))
|
||||||
|
// fmt.Println(testNumber(112233))
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user