Make D8 more general

This commit is contained in:
Christian
2024-12-08 09:30:12 +01:00
parent 7c4328ca42
commit 66575c4320
3 changed files with 4 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ case class Point(x: Int, y: Int):
def +(o: Point): Point = Point(x + o.x, y + o.y)
def -(o: Point): Point = Point(x - o.x, y - o.y)
def *(n: Int): Point = Point(n * x, n * y)
def reduce: Point = { val g = gcd(x.abs, y.abs); Point(x / g, y / g) }
enum Direction:
case Up, Right, Down, Left

View File

@@ -1,3 +1,4 @@
package dev.ctsk.aoc
def gcd(a: Int, b: Int): Int =
if b == 0 then a else gcd(b, a % b)

View File

@@ -16,7 +16,7 @@ object Day08 extends Solver(8):
def antinodes(a: Point, b: Point): Iterator[Point] =
def it(a: Point, b: Point) =
Iterator.iterate(a)(_ + a - b).takeWhile(grid.contains)
Iterator.iterate(a)(_ + (a - b).reduce).takeWhile(grid.contains)
it(a, b) ++ it(b, a)
def part1: Int =