Skip to content

Commit 9ffaeee

Browse files
committed
Solution for 2024, day 14
1 parent 2ef7a1f commit 9ffaeee

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

2024/14/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
input:
2+
http "https://adventofcode.com/2024/day/14/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input

2024/14/common.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
)
8+
9+
var max = P{101, 103}
10+
11+
type P struct{ x, y int }
12+
type Robot struct{ Pos, Vel P }
13+
14+
func parseInput() []Robot {
15+
robots := []Robot{}
16+
scanner := bufio.NewScanner(os.Stdin)
17+
for scanner.Scan() {
18+
line := scanner.Text()
19+
var x, y, vx, vy int
20+
fmt.Sscanf(line, "p=%d,%d v=%d,%d", &x, &y, &vx, &vy)
21+
robots = append(robots, Robot{P{x, y}, P{vx, vy}})
22+
}
23+
return robots
24+
}

2024/14/main1.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
robots := parseInput()
7+
8+
for t := 0; t < 100; t++ {
9+
for i := 0; i < len(robots); i++ {
10+
robots[i].Pos.x = (robots[i].Pos.x + robots[i].Vel.x + max.x) % max.x
11+
robots[i].Pos.y = (robots[i].Pos.y + robots[i].Vel.y + max.y) % max.y
12+
}
13+
}
14+
15+
q := [4]int{}
16+
for _, r := range robots {
17+
if r.Pos.x < max.x/2 {
18+
if r.Pos.y < max.y/2 {
19+
q[0]++
20+
}
21+
if r.Pos.y > max.y/2 {
22+
q[1]++
23+
}
24+
}
25+
if r.Pos.x > max.x/2 {
26+
if r.Pos.y < max.y/2 {
27+
q[2]++
28+
}
29+
if r.Pos.y > max.y/2 {
30+
q[3]++
31+
}
32+
}
33+
}
34+
35+
fmt.Println(q[0] * q[1] * q[2] * q[3])
36+
}

2024/14/main2.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
robots := parseInput()
7+
8+
for t := 0; ; t++ {
9+
for i := 0; i < len(robots); i++ {
10+
robots[i].Pos.x = (robots[i].Pos.x + robots[i].Vel.x + max.x) % max.x
11+
robots[i].Pos.y = (robots[i].Pos.y + robots[i].Vel.y + max.y) % max.y
12+
}
13+
14+
// robots gather vertically at t=3
15+
// robots gather horizontally at t=75
16+
// period is known
17+
18+
if ((t+max.x-3)%max.x == 0) && (t+max.y-75)%max.y == 0 {
19+
Print(robots)
20+
fmt.Println(t + 1)
21+
return
22+
}
23+
}
24+
}
25+
26+
func Print(robots []Robot) {
27+
m := map[P]struct{}{}
28+
for _, r := range robots {
29+
m[r.Pos] = struct{}{}
30+
}
31+
for y := 50; y < max.y-16; y++ {
32+
for x := 25; x < max.x-39; x++ {
33+
if _, ok := m[P{x, y}]; ok {
34+
fmt.Print("#")
35+
} else {
36+
fmt.Print(".")
37+
}
38+
}
39+
fmt.Println()
40+
}
41+
}

0 commit comments

Comments
 (0)