day 9 part 2
This commit is contained in:
parent
6260392b4e
commit
5a8cd089c9
2
main.go
2
main.go
@ -32,7 +32,7 @@ func main() {
|
|||||||
runAndBenchmark("08", "2", false, solutions.Day08Part02)
|
runAndBenchmark("08", "2", false, solutions.Day08Part02)
|
||||||
|
|
||||||
runAndBenchmark("09", "1", false, solutions.Day09Part01)
|
runAndBenchmark("09", "1", false, solutions.Day09Part01)
|
||||||
runAndBenchmark("09", "2", true, solutions.Day09Part02)
|
runAndBenchmark("09", "2", false, solutions.Day09Part02)
|
||||||
}
|
}
|
||||||
|
|
||||||
type execute func(bool) int
|
type execute func(bool) int
|
||||||
|
@ -1,28 +1,14 @@
|
|||||||
package solutions
|
package solutions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Day09Part01(isTest bool) int {
|
func findInvalidNumber(preambleSize int, n *[]int) int {
|
||||||
input := ReadInput("09", isTest)
|
numbers := *n
|
||||||
groups := strings.Split(input, "\n")
|
dataSize := len(numbers)
|
||||||
|
|
||||||
dataSize := len(groups)
|
|
||||||
numbers := make([]int, dataSize)
|
|
||||||
|
|
||||||
for i, str := range groups {
|
|
||||||
value, err := strconv.Atoi(str)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
numbers[i] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// loop
|
|
||||||
preambleSize := 25
|
|
||||||
|
|
||||||
out:
|
out:
|
||||||
for i := 0; i < dataSize-preambleSize; i += 1 {
|
for i := 0; i < dataSize-preambleSize; i += 1 {
|
||||||
@ -52,9 +38,89 @@ out:
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Day09Part01(isTest bool) int {
|
||||||
|
input := ReadInput("09", isTest)
|
||||||
|
groups := strings.Split(input, "\n")
|
||||||
|
|
||||||
|
dataSize := len(groups)
|
||||||
|
numbers := make([]int, dataSize)
|
||||||
|
|
||||||
|
for i, str := range groups {
|
||||||
|
value, err := strconv.Atoi(str)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
numbers[i] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
preambleSize := 25
|
||||||
|
if isTest {
|
||||||
|
preambleSize = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
return findInvalidNumber(preambleSize, &numbers)
|
||||||
|
}
|
||||||
|
|
||||||
func Day09Part02(isTest bool) int {
|
func Day09Part02(isTest bool) int {
|
||||||
// input := ReadInput("09", isTest)
|
input := ReadInput("09", isTest)
|
||||||
// groups := strings.Split(input, "\n")
|
groups := strings.Split(input, "\n")
|
||||||
|
|
||||||
|
dataSize := len(groups)
|
||||||
|
numbers := make([]int, dataSize)
|
||||||
|
|
||||||
|
for i, str := range groups {
|
||||||
|
value, err := strconv.Atoi(str)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
numbers[i] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
preambleSize := 25
|
||||||
|
if isTest {
|
||||||
|
preambleSize = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidNumber := findInvalidNumber(preambleSize, &numbers)
|
||||||
|
|
||||||
|
for i := 0; i < len(groups); i += 1 {
|
||||||
|
firstNumber := numbers[i]
|
||||||
|
sum := firstNumber
|
||||||
|
|
||||||
|
resultNumbers := make([]int, 0)
|
||||||
|
resultNumbers = append(resultNumbers, firstNumber)
|
||||||
|
|
||||||
|
for j := i + 1; j < len(groups); j += 1 {
|
||||||
|
currentNumber := numbers[j]
|
||||||
|
sum += currentNumber
|
||||||
|
|
||||||
|
if sum < invalidNumber {
|
||||||
|
resultNumbers = append(resultNumbers, currentNumber)
|
||||||
|
} else if sum == invalidNumber {
|
||||||
|
resultNumbers = append(resultNumbers, currentNumber)
|
||||||
|
|
||||||
|
// Search the smallest & largest numbers in the slice
|
||||||
|
|
||||||
|
smallest := math.MaxInt
|
||||||
|
largest := math.MinInt
|
||||||
|
|
||||||
|
for _, value := range resultNumbers {
|
||||||
|
if value < smallest {
|
||||||
|
smallest = value
|
||||||
|
}
|
||||||
|
if value > largest {
|
||||||
|
largest = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return smallest + largest
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user