Skip to content

Commit

Permalink
Solution for 2024, day 14
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianoq committed Dec 14, 2024
1 parent 2ef7a1f commit 9ffaeee
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 2024/14/Makefile
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
24 changes: 24 additions & 0 deletions 2024/14/common.go
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
}
36 changes: 36 additions & 0 deletions 2024/14/main1.go
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])
}
41 changes: 41 additions & 0 deletions 2024/14/main2.go
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()
}
}

0 comments on commit 9ffaeee

Please sign in to comment.