refactor: massive refactor to have only one binary to call

This commit is contained in:
2025-11-26 14:03:32 +01:00
parent 314da54495
commit 3723f84d1a
41 changed files with 272 additions and 188 deletions

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

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

View File

@@ -0,0 +1,117 @@
package daythree
import (
"advent-of-code/internal/registry"
"os"
"strings"
)
func init() {
registry.Register("2021D3", ParseInput, PartOne, PartTwo)
}
func ParseInput(filepath string) []string {
content, _ := os.ReadFile(filepath)
return strings.Split(strings.TrimSpace(string(content)), "\n")
}
func PartOne(data []string) int {
bitlen := len(data[0])
ones := make([]int, bitlen)
for _, line := range data {
for i := range bitlen {
if line[i] == '1' {
ones[i]++
}
}
}
var gamma int
total := len(data)
for idx := range bitlen {
gamma <<= 1
if ones[idx] > total-ones[idx] {
gamma |= 1
}
}
mask := (1 << bitlen) - 1
epsilon := mask ^ gamma
return gamma * epsilon
}
func PartTwo(data []string) int {
bitlen := len(data[0])
oxygenIndices := make([]int, len(data))
for idx := range oxygenIndices {
oxygenIndices[idx] = idx
}
co2Indices := make([]int, len(data))
for idx := range co2Indices {
co2Indices[idx] = idx
}
for indice := 0; indice < bitlen && len(oxygenIndices) > 1; indice++ {
ones := 0
for _, idx := range oxygenIndices {
if data[idx][indice] == '1' {
ones++
}
}
target := byte('1')
if ones*2 < len(oxygenIndices) {
target = '0'
}
writeIdx := 0
for readIdx := 0; readIdx < len(oxygenIndices); readIdx++ {
if data[oxygenIndices[readIdx]][indice] == target {
oxygenIndices[writeIdx] = oxygenIndices[readIdx]
writeIdx++
}
}
oxygenIndices = oxygenIndices[:writeIdx]
}
for idx := 0; idx < bitlen && len(co2Indices) > 1; idx++ {
ones := 0
for _, indice := range co2Indices {
if data[indice][idx] == '1' {
ones++
}
}
target := byte('0')
if ones*2 < len(co2Indices) {
target = '1'
}
writeIdx := 0
for readIdx := 0; readIdx < len(co2Indices); readIdx++ {
if data[co2Indices[readIdx]][idx] == target {
co2Indices[writeIdx] = co2Indices[readIdx]
writeIdx++
}
}
co2Indices = co2Indices[:writeIdx]
}
oxygenRating := 0
for _, char := range data[oxygenIndices[0]] {
oxygenRating <<= 1
if char == '1' {
oxygenRating |= 1
}
}
co2Rating := 0
for _, char := range data[co2Indices[0]] {
co2Rating <<= 1
if char == '1' {
co2Rating |= 1
}
}
return oxygenRating * co2Rating
}

View File

@@ -0,0 +1,35 @@
package daythree
import "testing"
var testInput = []string{
"00100",
"11110",
"10110",
"10111",
"10101",
"01111",
"00111",
"11100",
"10000",
"11001",
"00010",
"01010",
}
func TestPartOne(t *testing.T) {
expected := 198
got := PartOne(testInput)
if got != expected {
t.Errorf("PartOne() = %d, want %d", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := 230
got := PartTwo(testInput)
if got != expected {
t.Errorf("PartTwo() = %d, want %d", got, expected)
}
}

View File

@@ -0,0 +1,60 @@
package daytwo
import (
"advent-of-code/internal/registry"
"os"
"strconv"
"strings"
)
func init() {
registry.Register("2021D2", ParseInput, PartOne, PartTwo)
}
func ParseInput(filepath string) []string {
content, _ := os.ReadFile(filepath)
return strings.Split(string(content), "\n")
}
func PartOne(data []string) int {
horizontal := 0
depth := 0
for _, line := range data {
parts := strings.Split(line, " ")
direction := parts[0]
amount, _ := strconv.Atoi(parts[1])
switch direction {
case "forward":
horizontal += amount
case "down":
depth += amount
case "up":
depth -= amount
}
}
return horizontal * depth
}
func PartTwo(data []string) int {
horizontal := 0
depth := 0
aim := 0
for _, line := range data {
parts := strings.Split(line, " ")
direction := parts[0]
amount, _ := strconv.Atoi(parts[1])
switch direction {
case "forward":
horizontal += amount
depth += aim * amount
case "down":
aim += amount
case "up":
aim -= amount
}
}
return horizontal * depth
}

View File

@@ -0,0 +1,29 @@
package daytwo
import "testing"
var testInput = []string{
"forward 5",
"down 5",
"forward 8",
"up 3",
"down 8",
"forward 2",
}
func TestPartOne(t *testing.T) {
expected := 150
got := PartOne(testInput)
if got != expected {
t.Errorf("PartOne() = %d, want %d", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := 900
got := PartTwo(testInput)
if got != expected {
t.Errorf("PartTwo() = %d, want %d", got, expected)
}
}