refactor: massive refactor to have only one binary to call
This commit is contained in:
52
internal/2020/DayThree/code.go
Normal file
52
internal/2020/DayThree/code.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package daythree
|
||||
|
||||
import (
|
||||
"advent-of-code/internal/registry"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.Register("2020D3", ParseInput, PartOne, PartTwo)
|
||||
}
|
||||
|
||||
func ParseInput(filepath string) []string {
|
||||
content, _ := os.ReadFile(filepath)
|
||||
return strings.Split(string(content), "\n")
|
||||
}
|
||||
|
||||
func PartOne(input []string) int {
|
||||
trees := 0
|
||||
column := 0
|
||||
for row := range input {
|
||||
if len(input[row]) == 0 {
|
||||
continue
|
||||
}
|
||||
if input[row][column%len(input[row])] == '#' {
|
||||
trees++
|
||||
}
|
||||
column += 3
|
||||
}
|
||||
return trees
|
||||
}
|
||||
|
||||
func PartTwo(input []string) int {
|
||||
result := 1
|
||||
slopes := [][]int{{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}}
|
||||
for _, slope := range slopes {
|
||||
trees := 0
|
||||
column := 0
|
||||
for row := 0; row < len(input); row += slope[1] {
|
||||
if len(input[row]) == 0 {
|
||||
continue
|
||||
}
|
||||
if input[row][column%len(input[row])] == '#' {
|
||||
trees++
|
||||
}
|
||||
column += slope[0]
|
||||
}
|
||||
result *= trees
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
34
internal/2020/DayThree/code_test.go
Normal file
34
internal/2020/DayThree/code_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package daythree
|
||||
|
||||
import "testing"
|
||||
|
||||
var testInput = []string{
|
||||
"..##.......",
|
||||
"#...#...#..",
|
||||
".#....#..#.",
|
||||
"..#.#...#.#",
|
||||
".#...##..#.",
|
||||
"..#.##.....",
|
||||
".#.#.#....#",
|
||||
".#........#",
|
||||
"#.##...#...",
|
||||
"#...##....#",
|
||||
".#..#...#.#",
|
||||
}
|
||||
|
||||
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 := 336
|
||||
got := PartTwo(testInput)
|
||||
if got != expected {
|
||||
t.Errorf("PartTwo() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user