78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"AoC2020/helper"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
)
|
|
|
|
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)
|
|
}
|
|
part2(numbers)
|
|
}
|
|
|
|
func part2(numbers []int) {
|
|
sort.Ints(numbers)
|
|
numbers = prependInt(numbers,0)
|
|
numbers = append(numbers,numbers[len(numbers)-1]+3)
|
|
fmt.Printf("%v \n",numbers)
|
|
possibilities := make([][3]int, len(numbers))
|
|
possibilities[len(numbers)-1] = [3]int{0,0,1}
|
|
for i:= len(numbers)-1; i >= 0; i-- {
|
|
diff := 0
|
|
j := 1
|
|
for diff <= 3 && i+j<len(numbers){
|
|
diff = numbers[i+j] - numbers[i]
|
|
switch diff {
|
|
case 1:
|
|
possibilities[i][0] = sum(possibilities[i+j])
|
|
case 2:
|
|
possibilities[i][1] = sum(possibilities[i+j])
|
|
case 3:
|
|
possibilities[i][2] = sum(possibilities[i+j])
|
|
}
|
|
j++
|
|
}
|
|
}
|
|
fmt.Printf("%v \n",possibilities)
|
|
fmt.Printf("%d \n",sum(possibilities[0]))
|
|
}
|
|
|
|
func part1(numbers []int) {
|
|
sort.Ints(numbers)
|
|
diffCounter := make([]int, 3)
|
|
numbers = prependInt(numbers,0)
|
|
numbers = append(numbers,numbers[len(numbers)-1]+3)
|
|
fmt.Printf("%v \n",numbers)
|
|
for i:= 0; i < len(numbers)-1; i++ {
|
|
diffCounter[numbers[i+1] - numbers[i]-1]++
|
|
}
|
|
fmt.Printf("%v \n",diffCounter)
|
|
fmt.Print(diffCounter[0] * diffCounter[2])
|
|
}
|
|
|
|
func prependInt(x []int, y int) []int {
|
|
x = append(x, 0)
|
|
copy(x[1:], x)
|
|
x[0] = y
|
|
return x
|
|
}
|
|
|
|
func sum(array [3]int) int {
|
|
result := 0
|
|
for _, v := range array {
|
|
result += v
|
|
}
|
|
return result
|
|
}
|
|
|