Day 1 part 1

master
Araozu 2024-02-24 20:20:02 -05:00
parent 37175fcbc1
commit 6f943e502c
2 changed files with 39 additions and 3 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea
inputs
test_inputs

39
main.go
View File

@ -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
}
}
}