Compare commits

..

2 Commits

Author SHA1 Message Date
70189f4295 feat: solve part one 2025-12-01 20:47:02 +01:00
cdacf7ae06 test: add unit test for p1 from paper calculation 2025-12-01 20:46:55 +01:00
2 changed files with 92 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
package daysix
import (
"advent-of-code/internal/registry"
"os"
"strconv"
"strings"
)
func init() {
registry.Register("2015D6", 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 {
grid := make([]bool, 1000*1000)
for _, line := range data {
var op string
var x1, y1, x2, y2 int
switch {
case strings.HasPrefix(line, "turn on "):
op = "on"
line = strings.TrimPrefix(line, "turn on ")
case strings.HasPrefix(line, "turn off "):
op = "off"
line = strings.TrimPrefix(line, "turn off ")
case strings.HasPrefix(line, "toggle "):
op = "toggle"
line = strings.TrimPrefix(line, "toggle ")
default:
continue
}
parts := strings.Split(line, " through ")
firstCoordinates := strings.Split(parts[0], ",")
secondCoordinates := strings.Split(parts[1], ",")
x1, _ = strconv.Atoi(firstCoordinates[0])
y1, _ = strconv.Atoi(firstCoordinates[1])
x2, _ = strconv.Atoi(secondCoordinates[0])
y2, _ = strconv.Atoi(secondCoordinates[1])
for y := y1; y <= y2; y++ {
for x := x1; x <= x2; x++ {
idx := y*1000 + x
switch op {
case "on":
grid[idx] = true
case "off":
grid[idx] = false
case "toggle":
grid[idx] = !grid[idx]
}
}
}
}
count := 0
for _, lit := range grid {
if lit {
count++
}
}
return count
}
func PartTwo(data []string) int {
return 0
}

View File

@@ -0,0 +1,17 @@
package daysix
import "testing"
var testInput = []string{
"turn on 0,0 through 999,999",
"toggle 0,0 through 999,0",
"turn off 499,499 through 500,500",
}
func TestPartOne(t *testing.T) {
expected := 998996
got := PartOne(testInput)
if got != expected {
t.Errorf("PartOne() = %d, want %d", got, expected)
}
}