day 9 part 1
This commit is contained in:
parent
0042d2fbcd
commit
6260392b4e
3
main.go
3
main.go
@ -30,6 +30,9 @@ func main() {
|
|||||||
|
|
||||||
runAndBenchmark("08", "1", false, solutions.Day08Part01)
|
runAndBenchmark("08", "1", false, solutions.Day08Part01)
|
||||||
runAndBenchmark("08", "2", false, solutions.Day08Part02)
|
runAndBenchmark("08", "2", false, solutions.Day08Part02)
|
||||||
|
|
||||||
|
runAndBenchmark("09", "1", false, solutions.Day09Part01)
|
||||||
|
runAndBenchmark("09", "2", true, solutions.Day09Part02)
|
||||||
}
|
}
|
||||||
|
|
||||||
type execute func(bool) int
|
type execute func(bool) int
|
||||||
|
60
solutions/day09.go
Normal file
60
solutions/day09.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package solutions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// loop
|
||||||
|
preambleSize := 25
|
||||||
|
|
||||||
|
out:
|
||||||
|
for i := 0; i < dataSize-preambleSize; i += 1 {
|
||||||
|
sum := numbers[i+preambleSize]
|
||||||
|
// A slice that stores: sum - i
|
||||||
|
indexes := make([]int, preambleSize)
|
||||||
|
|
||||||
|
// Iterate over every `preambleSize` items
|
||||||
|
for j := 0; j < preambleSize; j += 1 {
|
||||||
|
number := numbers[j+i]
|
||||||
|
|
||||||
|
// On every iteration check if the current value is present in indexes
|
||||||
|
for _, value := range indexes {
|
||||||
|
// If so, a pair is found
|
||||||
|
if value == number {
|
||||||
|
continue out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
indexes = append(indexes, sum-number)
|
||||||
|
}
|
||||||
|
|
||||||
|
// When this is reached no sum was found
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func Day09Part02(isTest bool) int {
|
||||||
|
// input := ReadInput("09", isTest)
|
||||||
|
// groups := strings.Split(input, "\n")
|
||||||
|
|
||||||
|
return -1
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user