@@ -3,53 +3,55 @@ package main
3
3
import (
4
4
"fmt"
5
5
"github.com/terminalnode/adventofcode2024/common"
6
+ "github.com/terminalnode/adventofcode2024/common/util"
6
7
)
7
8
9
+ const maxX = 100
10
+ const maxY = 102
11
+ const horizontalLine = maxY / 2
12
+ const verticalLine = maxX / 2
13
+
8
14
func main () {
9
15
common .Setup (14 , part1 , nil )
10
16
}
11
17
12
18
func part1 (
13
19
input string ,
14
20
) 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
-
28
21
robots , err := parseRobots (input )
29
22
if err != nil {
30
23
return fmt .Sprintf ("Failed to parse robots: %v" , err )
31
24
}
32
25
33
26
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 {
40
30
q1 ++
41
- } else if newPos .Y > horizontalLine {
31
+ } else if p .Y > horizontalLine {
42
32
q2 ++
43
33
}
44
- } else if newPos .X > verticalLine {
45
- if newPos .Y < horizontalLine {
34
+ } else if p .X > verticalLine {
35
+ if p .Y < horizontalLine {
46
36
q3 ++
47
- } else if newPos .Y > horizontalLine {
37
+ } else if p .Y > horizontalLine {
48
38
q4 ++
49
39
}
50
40
}
51
41
}
52
42
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
55
57
}
0 commit comments