|
@@ -9,7 +9,7 @@ import (
|
|
|
|
|
|
type Robot struct {
|
|
type Robot struct {
|
|
x, y int
|
|
x, y int
|
|
- vX, Vy int
|
|
|
|
|
|
+ vX, vY int
|
|
}
|
|
}
|
|
|
|
|
|
func readInput(file *os.File) []Robot {
|
|
func readInput(file *os.File) []Robot {
|
|
@@ -23,7 +23,7 @@ func readInput(file *os.File) []Robot {
|
|
}
|
|
}
|
|
|
|
|
|
var robot Robot
|
|
var robot Robot
|
|
- n, err := fmt.Sscanf(line, "p=%d,%d v=%d,%d", &robot.x, &robot.y, &robot.vX, &robot.Vy)
|
|
|
|
|
|
+ n, err := fmt.Sscanf(line, "p=%d,%d v=%d,%d", &robot.x, &robot.y, &robot.vX, &robot.vY)
|
|
if n != 4 || err != nil {
|
|
if n != 4 || err != nil {
|
|
log.Fatalf("Not able to parse robot '%s': %s", line, err)
|
|
log.Fatalf("Not able to parse robot '%s': %s", line, err)
|
|
}
|
|
}
|
|
@@ -34,6 +34,46 @@ func readInput(file *os.File) []Robot {
|
|
return robots
|
|
return robots
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func getMaxXY(robots []Robot) (int, int) {
|
|
|
|
+ var maxX, maxY int
|
|
|
|
+ for _, robot := range robots {
|
|
|
|
+ if robot.x > maxX {
|
|
|
|
+ maxX = robot.x
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if robot.y > maxY {
|
|
|
|
+ maxY = robot.y
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return maxX + 1, maxY + 1
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func robotsAfter(robots []Robot, maxX, maxY, after int) []Robot {
|
|
|
|
+ for _, robot := range robots {
|
|
|
|
+ robot.x = (robot.vX*after + robot.x) % maxX
|
|
|
|
+ robot.y = (robot.vY*after + robot.y) % maxY
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return robots
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func part1(robots []Robot, maxX, maxY, after int) int {
|
|
|
|
+ midX := maxX / 2
|
|
|
|
+ midY := maxY / 2
|
|
|
|
+
|
|
|
|
+ var count int
|
|
|
|
+ robotsMoved := robotsAfter(robots, maxX, maxY, after)
|
|
|
|
+ fmt.Println(robotsMoved)
|
|
|
|
+ for _, robot := range robotsMoved {
|
|
|
|
+ if robot.x != midX && robot.y != midY {
|
|
|
|
+ count++
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return count
|
|
|
|
+}
|
|
|
|
+
|
|
func main() {
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
if len(os.Args) < 2 {
|
|
log.Fatal("You need to specify a file!")
|
|
log.Fatal("You need to specify a file!")
|
|
@@ -46,5 +86,7 @@ func main() {
|
|
}
|
|
}
|
|
|
|
|
|
robots := readInput(file)
|
|
robots := readInput(file)
|
|
- fmt.Println(robots)
|
|
|
|
|
|
+ maxX, maxY := getMaxXY(robots)
|
|
|
|
+ fmt.Println(maxX, maxY)
|
|
|
|
+ fmt.Println("Part1:", part1(robots, maxX, maxY, 100))
|
|
}
|
|
}
|