Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit e96bdb5

Browse files
committed
Refactor solution day 14 part 1
1 parent babe72e commit e96bdb5

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

solutions/day14/main.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,55 @@ package main
33
import (
44
"fmt"
55
"github.com/terminalnode/adventofcode2024/common"
6+
"github.com/terminalnode/adventofcode2024/common/util"
67
)
78

9+
const maxX = 100
10+
const maxY = 102
11+
const horizontalLine = maxY / 2
12+
const verticalLine = maxX / 2
13+
814
func main() {
915
common.Setup(14, part1, nil)
1016
}
1117

1218
func part1(
1319
input string,
1420
) string {
15-
return solve(input, 100, 100, 102)
16-
}
17-
18-
func solve(
19-
input string,
20-
seconds int,
21-
maxX int,
22-
maxY int,
23-
) string {
24-
// Lines assume that the board is an uneven number of tiles
25-
horizontalLine := maxY / 2
26-
verticalLine := maxX / 2
27-
2821
robots, err := parseRobots(input)
2922
if err != nil {
3023
return fmt.Sprintf("Failed to parse robots: %v", err)
3124
}
3225

3326
q1, q2, q3, q4 := 0, 0, 0, 0
34-
for _, r := range robots {
35-
move := r.move.Multiply(seconds, seconds)
36-
newPos := r.init.Add(move.X, move.Y).PositiveModulo(maxX+1, maxY+1)
37-
38-
if newPos.X < verticalLine {
39-
if newPos.Y < horizontalLine {
27+
for _, p := range getNewPositions(robots, 100) {
28+
if p.X < verticalLine {
29+
if p.Y < horizontalLine {
4030
q1++
41-
} else if newPos.Y > horizontalLine {
31+
} else if p.Y > horizontalLine {
4232
q2++
4333
}
44-
} else if newPos.X > verticalLine {
45-
if newPos.Y < horizontalLine {
34+
} else if p.X > verticalLine {
35+
if p.Y < horizontalLine {
4636
q3++
47-
} else if newPos.Y > horizontalLine {
37+
} else if p.Y > horizontalLine {
4838
q4++
4939
}
5040
}
5141
}
5242

53-
safetyFactor := q1 * q2 * q3 * q4
54-
return fmt.Sprintf("After 100 seconds, area has a safety factor of %d", safetyFactor)
43+
return fmt.Sprintf("After 100 seconds, area has a safety factor of %d", q1*q2*q3*q4)
44+
}
45+
46+
func getNewPositions(
47+
robots []robot,
48+
seconds int,
49+
) []util.Coordinate {
50+
out := make([]util.Coordinate, len(robots))
51+
for i, r := range robots {
52+
move := r.move.Multiply(seconds, seconds)
53+
out[i] = r.init.Add(move.X, move.Y).PositiveModulo(maxX+1, maxY+1)
54+
55+
}
56+
return out
5557
}

0 commit comments

Comments
 (0)