|
| 1 | +package AdventOfCode2019 |
| 2 | + |
| 3 | +object Day10: |
| 4 | + case class Point(x: Int, y: Int): |
| 5 | + def to(other: Point): Line = Line(y - other.y, other.x - x, x * other.y - other.x * y) |
| 6 | + |
| 7 | + case class Line(a: Int, b: Int, c: Int): |
| 8 | + def distance(p: Point): Int = a * p.x + b * p.y + c |
| 9 | + def normal(p: Point): Line = Line(b, -a, a * p.y - b * p.x) |
| 10 | + |
| 11 | + def parse(input: Seq[String]): Set[Point] = |
| 12 | + val height = input.size |
| 13 | + val width = input.head.size |
| 14 | + val points = for y <- 0 until height; x <- 0 until width if input(y)(x) == '#' yield Point(x, y) |
| 15 | + points.toSet |
| 16 | + |
| 17 | + def countVisible(points: Set[Point]): Set[(Point, Int)] = |
| 18 | + points.map { origin => |
| 19 | + val visible = groupByLine(points, origin).values.map { points => |
| 20 | + val distances = points.values |
| 21 | + distances.count(_ < 0).min(1) + distances.count(_ > 0).min(1) |
| 22 | + } |
| 23 | + origin -> visible.sum |
| 24 | + } |
| 25 | + |
| 26 | + def groupByLine(points: Set[Point], origin: Point): Map[Line, Map[Point, Int]] = |
| 27 | + (points - origin).foldLeft(Map.empty[Line, List[Point]]) { (lines, point) => |
| 28 | + lines.keys.find(_.distance(point) == 0) match |
| 29 | + case Some(line) => lines.updated(line, point :: lines(line)) |
| 30 | + case None => lines.updated(origin.to(point), List(point)) |
| 31 | + } |
| 32 | + .map { (line, points) => |
| 33 | + val normal = line.normal(origin) |
| 34 | + line -> points.zip(points.map(normal.distance)).toMap |
| 35 | + } |
| 36 | + |
| 37 | + def orderedClockwise(quadrant: Int, origin: Point, points: Set[Point]): Map[(Int, Int, Int), Point] = |
| 38 | + groupByLine(points, origin).flatMap { case (line, distances) => |
| 39 | + val toLeft = points.count(point => line.distance(point) < 0) |
| 40 | + val inOrder = distances.values.toSeq.sorted.zipWithIndex.toMap |
| 41 | + distances.map((point, distance) => (inOrder(distance), quadrant, toLeft) -> point) |
| 42 | + } |
| 43 | + |
| 44 | + def part1(input: Seq[String]): Int = |
| 45 | + val points = parse(input) |
| 46 | + val (origin, visible) = countVisible(points).maxBy(_._2) |
| 47 | + visible |
| 48 | + |
| 49 | + def part2(input: Seq[String]): Int = |
| 50 | + val points = parse(input) |
| 51 | + val (origin, visible) = countVisible(points).maxBy(_._2) |
| 52 | + |
| 53 | + val topRight = orderedClockwise(0, origin, points.filter(p => p.x >= origin.x && p.y < origin.y)) |
| 54 | + val bottomRight = orderedClockwise(1, origin, points.filter(p => p.x > origin.x && p.y >= origin.y)) |
| 55 | + val bottomLeft = orderedClockwise(2, origin, points.filter(p => p.x <= origin.x && p.y > origin.y)) |
| 56 | + val topLeft = orderedClockwise(3, origin, points.filter(p => p.x < origin.x && p.y <= origin.y)) |
| 57 | + |
| 58 | + val all = topRight ++ bottomRight ++ bottomLeft ++ topLeft |
| 59 | + val key = all.keys.toSeq.sorted.apply(199) |
| 60 | + val result = all(key) |
| 61 | + |
| 62 | + 100 * result.x + result.y |
| 63 | + end part2 |
| 64 | + |
| 65 | + def main(args: Array[String]): Unit = |
| 66 | + val data = io.Source.fromResource("AdventOfCode2019/Day10.txt").getLines().toSeq |
| 67 | + println(part1(data)) |
| 68 | + println(part2(data)) |
0 commit comments