Make D8 more general
This commit is contained in:
@@ -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 -(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 *(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:
|
enum Direction:
|
||||||
case Up, Right, Down, Left
|
case Up, Right, Down, Left
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
package dev.ctsk.aoc
|
package dev.ctsk.aoc
|
||||||
|
|
||||||
|
def gcd(a: Int, b: Int): Int =
|
||||||
|
if b == 0 then a else gcd(b, a % b)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ object Day08 extends Solver(8):
|
|||||||
|
|
||||||
def antinodes(a: Point, b: Point): Iterator[Point] =
|
def antinodes(a: Point, b: Point): Iterator[Point] =
|
||||||
def it(a: Point, b: 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)
|
it(a, b) ++ it(b, a)
|
||||||
|
|
||||||
def part1: Int =
|
def part1: Int =
|
||||||
|
|||||||
Reference in New Issue
Block a user