diff --git a/main.go b/main.go index f9d6a3a..24a3cb4 100644 --- a/main.go +++ b/main.go @@ -1,42 +1,7 @@ package main -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) -} +import "advent-20/solutions" func main() { - 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 - } - } + solutions.Day01Part01() } diff --git a/solutions/day01.go b/solutions/day01.go new file mode 100644 index 0000000..564d2ce --- /dev/null +++ b/solutions/day01.go @@ -0,0 +1,29 @@ +package solutions + +import ( + "fmt" + "strconv" + "strings" +) + +func Day01Part01() { + input := ReadInput("01", true) + 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 { + fmt.Println(value * currentRemainder) + break + } else { + remainders[value] = true + } + } +} diff --git a/solutions/utils.go b/solutions/utils.go new file mode 100644 index 0000000..1595270 --- /dev/null +++ b/solutions/utils.go @@ -0,0 +1,23 @@ +package solutions + +import ( + "fmt" + "os" +) + +const DIR = "/home/fernando/GolandProjects/advent-20/" + +func ReadInput(day string, isTest bool) string { + testStr := "" + if isTest { + testStr = "test_" + } + + bytes, err := os.ReadFile(DIR + testStr + "inputs/" + day + ".txt") + if err != nil { + fmt.Println(err) + panic("Error reading file.") + } + + return string(bytes) +}