feat: solve p2

This commit is contained in:
2025-12-01 21:28:27 +01:00
parent 3756279dab
commit 345defec4d

View File

@@ -71,5 +71,55 @@ func PartOne(data []string) int {
} }
func PartTwo(data []string) int { func PartTwo(data []string) int {
return 0 grid := make([]int, 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]++
case "off":
if grid[idx] > 0 {
grid[idx]--
}
case "toggle":
grid[idx] += 2
}
}
}
}
total := 0
for _, brightness := range grid {
total += brightness
}
return total
} }