diff --git a/.gitignore b/.gitignore index c6ef218..2843aa0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea - +inputs +test_inputs diff --git a/main.go b/main.go index a3dd973..f9d6a3a 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,42 @@ package main -import "fmt" +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) +} func main() { - fmt.Println("Hello, World!") + 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 + } + } }