refactor: massive refactor to have only one binary to call
This commit is contained in:
43
internal/2021/DayOne/code.go
Normal file
43
internal/2021/DayOne/code.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package dayone
|
||||
|
||||
import (
|
||||
"advent-of-code/internal/registry"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.Register("2021D1", ParseInput, PartOne, PartTwo)
|
||||
}
|
||||
|
||||
func ParseInput(filepath string) []int {
|
||||
content, _ := os.ReadFile(filepath)
|
||||
var data []int
|
||||
for line := range strings.SplitSeq(string(content), "\n") {
|
||||
num, _ := strconv.Atoi(line)
|
||||
data = append(data, num)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func PartOne(data []int) int {
|
||||
count := 0
|
||||
for i := 1; i < len(data); i++ {
|
||||
if data[i] > data[i-1] {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func PartTwo(data []int) int {
|
||||
count := 0
|
||||
for i := 3; i < len(data); i++ {
|
||||
if data[i] > data[i-3] {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
22
internal/2021/DayOne/code_test.go
Normal file
22
internal/2021/DayOne/code_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package dayone
|
||||
|
||||
import "testing"
|
||||
|
||||
var testInput = []int{199, 200, 208, 210, 200, 207, 240, 269, 260, 263}
|
||||
|
||||
func TestPartOne(t *testing.T) {
|
||||
expected := 7
|
||||
got := PartOne(testInput)
|
||||
if got != expected {
|
||||
t.Errorf("PartOne() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartTwo(t *testing.T) {
|
||||
expected := 5
|
||||
got := PartTwo(testInput)
|
||||
if got != expected {
|
||||
t.Errorf("PartTwo() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user