advent-20/main.go

43 lines
728 B
Go
Raw Normal View History

2024-02-25 00:55:09 +00:00
package main
2024-02-25 01:20:02 +00:00
import (
"fmt"
"os"
"strconv"
"strings"
)
const DIR = "/home/fernando/GolandProjects/advent-20/"
func readInput(isTest bool) string {
bytes, err := os.ReadFile(DIR + "inputs/01.txt")
if err != nil {
fmt.Println(err)
panic("Error reading file.")
}
return string(bytes)
}
2024-02-25 00:55:09 +00:00
func main() {
2024-02-25 01:20:02 +00:00
input := readInput(false)
values := strings.Split(input, "\n")
remainders := make(map[int]bool)
for _, valueStr := range values {
value, ok := strconv.Atoi(valueStr)
if ok != nil {
panic("Error converting to int")
}
currentRemainder := 2020 - value
if _, ok := remainders[currentRemainder]; ok {
fmt.Println(value * currentRemainder)
break
} else {
remainders[value] = true
}
}
2024-02-25 00:55:09 +00:00
}