75 lines
1.2 KiB
Go
75 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"AoC2020/helper"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
var pointer int
|
||
|
var band []int
|
||
|
|
||
|
func main() {
|
||
|
args := os.Args[1:]
|
||
|
input, err := helper.GetInput(args[0])
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
}
|
||
|
numbers, err := helper.MapToNumber(input)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
}
|
||
|
fmt.Printf("%v \n",numbers)
|
||
|
band = numbers
|
||
|
breaker := part1(25)
|
||
|
fmt.Println(breaker)
|
||
|
fmt.Print(part2(breaker))
|
||
|
}
|
||
|
|
||
|
func part1(preambleLen int) int{
|
||
|
pointer = preambleLen
|
||
|
isValid := true
|
||
|
for isValid {
|
||
|
_,_,err := helper.FindAddendsForSum(band[pointer-preambleLen:pointer],band[pointer])
|
||
|
if err != nil {
|
||
|
return band[pointer]
|
||
|
isValid = false
|
||
|
}
|
||
|
pointer++
|
||
|
}
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
func part2(breaker int) int{
|
||
|
for p1 := 0; p1 < len(band); p1++ {
|
||
|
for p2 := p1 + 1; p2 < len(band); p2++ {
|
||
|
array := make([]int, len(band[p1:p2]))
|
||
|
copy(array,band[p1:p2])
|
||
|
sumArray := 0
|
||
|
for _,value := range array {
|
||
|
sumArray += value
|
||
|
}
|
||
|
if sumArray == breaker {
|
||
|
fmt.Printf("%v \n" ,array)
|
||
|
var max int
|
||
|
var min int
|
||
|
for i, val := range array {
|
||
|
if i==0 || val < min {
|
||
|
min = val
|
||
|
}
|
||
|
if i==0 || val > max {
|
||
|
max = val
|
||
|
}
|
||
|
}
|
||
|
fmt.Printf("%d , %d \n" ,min, max)
|
||
|
return min + max
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
func run(preambleLen int) {
|
||
|
|
||
|
}
|