Skip to content

Commit

Permalink
day 14p1
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 14, 2024
1 parent 7d19897 commit 737e80d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
66 changes: 66 additions & 0 deletions day14p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package day14p1

import (
"io"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
isTest := len(lines) == 12
boardWidth := 11
if !isTest {
boardWidth = 101
}
boardHeight := 7
if !isTest {
boardHeight = 103
}

endPoints := make(map[utils.Point]int)

for _, line := range lines {
ints := utils.GetInts(line)
px, py, vx, vy := ints[0], ints[1], ints[2], ints[3]
for range 100 {
px += vx
py += vy
if px < 0 {
px += boardWidth
} else if px >= boardWidth {
px -= boardWidth
}
if py < 0 {
py += boardHeight
} else if py >= boardHeight {
py -= boardHeight
}
}
endPoints[utils.Point{X: px, Y: py}]++
}

bots := make([]int, 4)

for point, count := range endPoints {
if point.X < boardWidth/2 && point.Y < boardHeight/2 {
bots[0] += count
} else if point.X > boardWidth/2 && point.Y < boardHeight/2 {
bots[1] += count
} else if point.X < boardWidth/2 && point.Y > boardHeight/2 {
bots[2] += count
} else if point.X > boardWidth/2 && point.Y > boardHeight/2 {
bots[3] += count
}
}

return multAll(bots)
}

func multAll(arr []int) int {
result := 1
for _, n := range arr {
result *= n
}
return result
}
44 changes: 44 additions & 0 deletions day14p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package day14p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `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`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 12},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}
22 changes: 22 additions & 0 deletions utils/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
)

// regex for integers
var intRegex = regexp.MustCompile(`-?\d+`)

// Greatest Common Denominator
func Gcd(a, b int64) int64 {
for b != 0 {
Expand Down Expand Up @@ -55,3 +60,20 @@ func ReadLines(r io.Reader) []string {

return result
}

func GetInts(s string) []int {
matches := intRegex.FindAllString(s, -1)
result := make([]int, len(matches))
for i, match := range matches {
result[i] = Atoi(match)
}

return result
}

func Atoi(s string) int {
result, err := strconv.Atoi(s)
Check(err, "error converting %s to int", s)

return result
}

0 comments on commit 737e80d

Please sign in to comment.