refactor: create parseInstruction and instruction{} to use it in both parts

This commit is contained in:
2025-12-01 21:31:51 +01:00
parent 345defec4d
commit a680e0ba48

View File

@@ -16,40 +16,55 @@ func ParseInput(filepath string) []string {
return strings.Split(strings.TrimSpace(string(content)), "\n") return strings.Split(strings.TrimSpace(string(content)), "\n")
} }
func PartOne(data []string) int { type instruction struct {
grid := make([]bool, 1000*1000) op string
x1, y1 int
x2, y2 int
}
for _, line := range data { func parseInstruction(line string) (instruction, bool) {
var op string var op string
var x1, y1, x2, y2 int var remaining string
switch { switch {
case strings.HasPrefix(line, "turn on "): case strings.HasPrefix(line, "turn on "):
op = "on" op = "on"
line = strings.TrimPrefix(line, "turn on ") remaining = strings.TrimPrefix(line, "turn on ")
case strings.HasPrefix(line, "turn off "): case strings.HasPrefix(line, "turn off "):
op = "off" op = "off"
line = strings.TrimPrefix(line, "turn off ") remaining = strings.TrimPrefix(line, "turn off ")
case strings.HasPrefix(line, "toggle "): case strings.HasPrefix(line, "toggle "):
op = "toggle" op = "toggle"
line = strings.TrimPrefix(line, "toggle ") remaining = strings.TrimPrefix(line, "toggle ")
default: default:
continue return instruction{}, false
} }
parts := strings.Split(line, " through ") parts := strings.Split(remaining, " through ")
firstCoordinates := strings.Split(parts[0], ",") firstCoordinates := strings.Split(parts[0], ",")
secondCoordinates := strings.Split(parts[1], ",") secondCoordinates := strings.Split(parts[1], ",")
x1, _ = strconv.Atoi(firstCoordinates[0]) x1, _ := strconv.Atoi(firstCoordinates[0])
y1, _ = strconv.Atoi(firstCoordinates[1]) y1, _ := strconv.Atoi(firstCoordinates[1])
x2, _ = strconv.Atoi(secondCoordinates[0]) x2, _ := strconv.Atoi(secondCoordinates[0])
y2, _ = strconv.Atoi(secondCoordinates[1]) y2, _ := strconv.Atoi(secondCoordinates[1])
for y := y1; y <= y2; y++ { return instruction{op: op, x1: x1, y1: y1, x2: x2, y2: y2}, true
for x := x1; x <= x2; x++ { }
func PartOne(data []string) int {
grid := make([]bool, 1000*1000)
for _, line := range data {
instruction, ok := parseInstruction(line)
if !ok {
continue
}
for y := instruction.y1; y <= instruction.y2; y++ {
for x := instruction.x1; x <= instruction.x2; x++ {
idx := y*1000 + x idx := y*1000 + x
switch op { switch instruction.op {
case "on": case "on":
grid[idx] = true grid[idx] = true
case "off": case "off":
@@ -74,36 +89,15 @@ func PartTwo(data []string) int {
grid := make([]int, 1000*1000) grid := make([]int, 1000*1000)
for _, line := range data { for _, line := range data {
var op string inst, ok := parseInstruction(line)
var x1, y1, x2, y2 int if !ok {
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 continue
} }
parts := strings.Split(line, " through ") for y := inst.y1; y <= inst.y2; y++ {
firstCoordinates := strings.Split(parts[0], ",") for x := inst.x1; x <= inst.x2; x++ {
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 idx := y*1000 + x
switch op { switch inst.op {
case "on": case "on":
grid[idx]++ grid[idx]++
case "off": case "off":