Skip to content

Commit

Permalink
2024 day 14
Browse files Browse the repository at this point in the history
| #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  #  #  #  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  #  #  #  #  #  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  #  #  #  #  #  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  .  #
| #  .  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  .  #
| #  .  .  .  .  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  #  #  #  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  #  #  #  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  #  #  #  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  #
| #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
  • Loading branch information
dbut2 committed Dec 14, 2024
1 parent 38ff547 commit fab63ac
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
64 changes: 64 additions & 0 deletions 2024/14/1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"github.com/dbut2/advent-of-code/pkg/harness"
)

func solve(input [][]int) int {
type robot struct {
x, y int
dx, dy int
}

var rs []robot
for _, line := range input {
rs = append(rs, robot{
x: line[0],
y: line[1],
dx: line[2],
dy: line[3],
})
}

width := 0
height := 0
for _, r := range rs {
width = max(width, r.x+1)
height = max(height, r.y+1)
}

for range 100 {
for i := range rs {
rs[i].x = (rs[i].x + rs[i].dx + width) % width
rs[i].y = (rs[i].y + rs[i].dy + height) % height
}
}

quads := [4]int{}
for _, r := range rs {
left := r.x < width/2
right := r.x > width/2

up := r.y < height/2
down := r.y > height/2

switch {
case left && up:
quads[0]++
case left && down:
quads[1]++
case right && up:
quads[2]++
case right && down:
quads[3]++
}
}

return quads[0] * quads[1] * quads[2] * quads[3]
}

func main() {
h := harness.New(solve)
h.Expect(1, 12)
h.Run()
}
71 changes: 71 additions & 0 deletions 2024/14/2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"github.com/dbut2/advent-of-code/pkg/harness"
"github.com/dbut2/advent-of-code/pkg/sets"
)

func solve(input [][]int) int {
type robot struct {
x, y int
dx, dy int
}

var rs []robot
for _, line := range input {
rs = append(rs, robot{
x: line[0],
y: line[1],
dx: line[2],
dy: line[3],
})
}

width := 0
height := 0
for _, r := range rs {
width = max(width, r.x+1)
height = max(height, r.y+1)
}

c := 0
for {
c++
tiles := sets.Set[[2]int]{}
for i := range rs {
r := rs[i]
r.x = (r.x + r.dx + width) % width
r.y = (r.y + r.dy + height) % height
rs[i] = r
tiles.Add([2]int{r.x, r.y})
}

// Check grid for any 3x3 filled in square, assuming to be a tree
hasSquare := false
for x := range width - 3 {
for y := range height - 3 {
thisHas := true
for dx := range 3 {
for dy := range 3 {
if !tiles.Contains([2]int{x + dx, y + dy}) {
thisHas = false
break
}
}
}
if thisHas {
hasSquare = true
}
}
}
if hasSquare {
break
}
}
return c
}

func main() {
h := harness.New(solve)
h.Run()
}
12 changes: 12 additions & 0 deletions 2024/14/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3
Empty file added 2024/14/test2.txt
Empty file.

0 comments on commit fab63ac

Please sign in to comment.