feat: solve part one

This commit is contained in:
2025-12-01 20:47:02 +01:00
parent cdacf7ae06
commit 70189f4295

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
}