-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
132 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,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 | ||
} |
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,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) | ||
} | ||
} | ||
} |
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