Organize code

master
Araozu 2024-02-24 20:39:28 -05:00
parent 6f943e502c
commit c9e8ca3669
3 changed files with 54 additions and 37 deletions

39
main.go
View File

@ -1,42 +1,7 @@
package main package main
import ( import "advent-20/solutions"
"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() { func main() {
input := readInput(false) solutions.Day01Part01()
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
}
}
} }

29
solutions/day01.go Normal file
View File

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

23
solutions/utils.go Normal file
View File

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