-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
input: | ||
http "https://adventofcode.com/2024/day/14/input" "Cookie:session=${AOC_SESSION};" >input | ||
|
||
main1: | ||
go build -o main1 main1.go common.go | ||
|
||
main2: | ||
go build -o main2 main2.go common.go | ||
|
||
.PHONY: run1 run2 clean | ||
|
||
run1: main1 input | ||
./main1 <input | ||
|
||
run2: main2 input | ||
./main2 <input | ||
|
||
clean: | ||
rm -f main1 main2 input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
var max = P{101, 103} | ||
|
||
type P struct{ x, y int } | ||
type Robot struct{ Pos, Vel P } | ||
|
||
func parseInput() []Robot { | ||
robots := []Robot{} | ||
scanner := bufio.NewScanner(os.Stdin) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
var x, y, vx, vy int | ||
fmt.Sscanf(line, "p=%d,%d v=%d,%d", &x, &y, &vx, &vy) | ||
robots = append(robots, Robot{P{x, y}, P{vx, vy}}) | ||
} | ||
return robots | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
robots := parseInput() | ||
|
||
for t := 0; t < 100; t++ { | ||
for i := 0; i < len(robots); i++ { | ||
robots[i].Pos.x = (robots[i].Pos.x + robots[i].Vel.x + max.x) % max.x | ||
robots[i].Pos.y = (robots[i].Pos.y + robots[i].Vel.y + max.y) % max.y | ||
} | ||
} | ||
|
||
q := [4]int{} | ||
for _, r := range robots { | ||
if r.Pos.x < max.x/2 { | ||
if r.Pos.y < max.y/2 { | ||
q[0]++ | ||
} | ||
if r.Pos.y > max.y/2 { | ||
q[1]++ | ||
} | ||
} | ||
if r.Pos.x > max.x/2 { | ||
if r.Pos.y < max.y/2 { | ||
q[2]++ | ||
} | ||
if r.Pos.y > max.y/2 { | ||
q[3]++ | ||
} | ||
} | ||
} | ||
|
||
fmt.Println(q[0] * q[1] * q[2] * q[3]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
robots := parseInput() | ||
|
||
for t := 0; ; t++ { | ||
for i := 0; i < len(robots); i++ { | ||
robots[i].Pos.x = (robots[i].Pos.x + robots[i].Vel.x + max.x) % max.x | ||
robots[i].Pos.y = (robots[i].Pos.y + robots[i].Vel.y + max.y) % max.y | ||
} | ||
|
||
// robots gather vertically at t=3 | ||
// robots gather horizontally at t=75 | ||
// period is known | ||
|
||
if ((t+max.x-3)%max.x == 0) && (t+max.y-75)%max.y == 0 { | ||
Print(robots) | ||
fmt.Println(t + 1) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func Print(robots []Robot) { | ||
m := map[P]struct{}{} | ||
for _, r := range robots { | ||
m[r.Pos] = struct{}{} | ||
} | ||
for y := 50; y < max.y-16; y++ { | ||
for x := 25; x < max.x-39; x++ { | ||
if _, ok := m[P{x, y}]; ok { | ||
fmt.Print("#") | ||
} else { | ||
fmt.Print(".") | ||
} | ||
} | ||
fmt.Println() | ||
} | ||
} |