2024-02-25 01:39:28 +00:00
|
|
|
package solutions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-02-25 02:17:52 +00:00
|
|
|
func Day01Part01(isTest bool) int {
|
|
|
|
input := ReadInput("01", isTest)
|
2024-02-25 01:39:28 +00:00
|
|
|
values := strings.Split(input, "\n")
|
|
|
|
remainders := make(map[int]bool)
|
|
|
|
|
|
|
|
for _, valueStr := range values {
|
|
|
|
value, err := strconv.Atoi(valueStr)
|
|
|
|
if err != nil {
|
|
|
|
panic("Error converting to int")
|
|
|
|
}
|
|
|
|
|
|
|
|
currentRemainder := 2020 - value
|
|
|
|
|
|
|
|
if _, err := remainders[currentRemainder]; err {
|
2024-02-25 02:17:52 +00:00
|
|
|
return value * currentRemainder
|
2024-02-25 01:39:28 +00:00
|
|
|
} else {
|
|
|
|
remainders[value] = true
|
|
|
|
}
|
|
|
|
}
|
2024-02-25 02:17:52 +00:00
|
|
|
|
|
|
|
return -1
|
2024-02-25 01:39:28 +00:00
|
|
|
}
|
2024-02-25 02:13:14 +00:00
|
|
|
|
2024-02-25 02:17:52 +00:00
|
|
|
func Day01Part02(isTest bool) int {
|
|
|
|
input := ReadInput("01", isTest)
|
2024-02-25 02:13:14 +00:00
|
|
|
strValues := strings.Split(input, "\n")
|
|
|
|
values := make([]int, len(strValues))
|
|
|
|
for i, valueStr := range strValues {
|
|
|
|
value, err := strconv.Atoi(valueStr)
|
|
|
|
if err != nil {
|
|
|
|
panic("Error converting to int")
|
|
|
|
}
|
|
|
|
values[i] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
arrLen := len(values)
|
|
|
|
for i := 0; i < arrLen; i += 1 {
|
|
|
|
for j := i + 1; j < arrLen; j += 1 {
|
|
|
|
for k := j + 1; k < arrLen; k += 1 {
|
|
|
|
if values[i]+values[j]+values[k] == 2020 {
|
2024-02-25 02:21:42 +00:00
|
|
|
return values[i] * values[j] * values[k]
|
2024-02-25 02:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-25 02:17:52 +00:00
|
|
|
|
|
|
|
return -1
|
2024-02-25 02:13:14 +00:00
|
|
|
}
|