Organize code
This commit is contained in:
parent
6f943e502c
commit
c9e8ca3669
39
main.go
39
main.go
@ -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
29
solutions/day01.go
Normal 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
23
solutions/utils.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user