2020-12-01 17:08:12 +01:00
package main
import (
"AoC2020/helper"
"errors"
"fmt"
"os"
"sort"
)
func main ( ) {
args := os . Args [ 1 : ]
input , err := helper . GetInput ( args [ 0 ] )
2020-12-02 17:47:40 +01:00
fmt . Println ( input )
2020-12-01 17:08:12 +01:00
if err != nil {
fmt . Println ( err )
}
2020-12-02 17:47:40 +01:00
numbers , err := helper . MapToNumber ( input )
if err != nil {
fmt . Println ( err )
}
2020-12-09 18:14:44 +01:00
fmt . Println ( day1 ( numbers , 2020 ) )
2020-12-02 17:47:40 +01:00
fmt . Println ( day1_part2 ( numbers ) )
2020-12-01 17:08:12 +01:00
}
2020-12-09 18:14:44 +01:00
func day1 ( numbers [ ] int , goal int ) int {
add1 , add2 , err := helper . FindAddendsForSum ( numbers , goal )
if err != nil {
return 0
2020-12-01 17:08:12 +01:00
}
2020-12-09 18:14:44 +01:00
return add1 * add2
2020-12-01 17:08:12 +01:00
}
func day1_part2 ( numbers [ ] int ) ( int , error ) {
goal := 2020
sort . Ints ( numbers )
for i , number := range numbers {
p1 := i + 1
p2 := len ( numbers ) - 1 - i
for p1 < p2 {
if numbers [ p1 ] + numbers [ p2 ] + number < goal {
p1 ++
}
if numbers [ p1 ] + numbers [ p2 ] + number > goal {
p2 --
}
if numbers [ p1 ] + numbers [ p2 ] + number == goal {
return numbers [ p1 ] * numbers [ p2 ] * number , nil
}
}
}
return 0 , errors . New ( "No Triplet Found" )
}