|
| 1 | +package day15 |
| 2 | + |
| 3 | +import locations.Directory.currentDir |
| 4 | +import inputs.Input.loadFileSync |
| 5 | + |
| 6 | +@main def part1: Unit = |
| 7 | + println(s"The solution is ${part1(loadInput())}") |
| 8 | + |
| 9 | +@main def part2: Unit = |
| 10 | + println(s"The solution is ${part2(loadInput())}") |
| 11 | + |
| 12 | +def loadInput(): String = loadFileSync(s"$currentDir/../input/day15") |
| 13 | + |
| 14 | +case class Position(x: Int, y: Int) |
| 15 | + |
| 16 | +def parse(input: String): List[(Position, Position)] = |
| 17 | + input.split("\n").toList.map{ |
| 18 | + case s"Sensor at x=$sx, y=$sy: closest beacon is at x=$bx, y=$by" => |
| 19 | + (Position(sx.toInt, sy.toInt), Position(bx.toInt, by.toInt)) |
| 20 | + } |
| 21 | + |
| 22 | +def distance(p1: Position, p2: Position): Int = |
| 23 | + Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y) |
| 24 | + |
| 25 | +def distanceToLine(p: Position, y: Int): Int = |
| 26 | + Math.abs(p.y - y) |
| 27 | + |
| 28 | +def lineCoverage(sensor: Position, radius: Int, lineY: Int): Range = |
| 29 | + val radiusInLine = radius - distanceToLine(sensor, lineY) |
| 30 | + |
| 31 | + // if radiusInLine is smaller than 0, the range will be empty |
| 32 | + (sensor.x - radiusInLine) to (sensor.x + radiusInLine) |
| 33 | + |
| 34 | +def coverOfLine(sensorsWithDistances: List[(Position, Int)], line: Int) = |
| 35 | + sensorsWithDistances.map( (sensor, radius) => lineCoverage(sensor, radius, line) ).filter(_.nonEmpty) |
| 36 | + |
| 37 | +def smartDiff(r1: Range, r2: Range): List[Range] = |
| 38 | + val innit = r1.start to Math.min(r2.start - 1, r1.last) |
| 39 | + val tail = Math.max(r1.start, r2.last + 1) to r1.last |
| 40 | + val res = if innit == tail then |
| 41 | + List(innit) |
| 42 | + else |
| 43 | + List(innit, tail) |
| 44 | + res.filter(_.nonEmpty).toList |
| 45 | + |
| 46 | +def remainingSpots(target: Range, cover: List[Range]): Set[Int] = |
| 47 | + |
| 48 | + def rec(partialTarget: List[Range], remainingCover: List[Range]): List[Range] = |
| 49 | + if remainingCover.isEmpty then |
| 50 | + partialTarget |
| 51 | + else |
| 52 | + val (curr: Range) :: rest = remainingCover: @unchecked |
| 53 | + rec( |
| 54 | + partialTarget = partialTarget.flatMap( r => smartDiff(r, curr) ), |
| 55 | + remainingCover = rest |
| 56 | + ) |
| 57 | + |
| 58 | + rec(List(target), cover).flatten.toSet |
| 59 | + |
| 60 | +def part1(input: String): Int = |
| 61 | + val parsed: List[(Position, Position)] = parse(input) |
| 62 | + val beacons: Set[Position] = parsed.map(_._2).toSet |
| 63 | + val sensorsWithDistances: List[(Position, Int)] = |
| 64 | + parsed.map( (sensor, beacon) => (sensor, distance(sensor, beacon)) ) |
| 65 | + |
| 66 | + val line = 2000000 |
| 67 | + val cover: List[Range] = coverOfLine(sensorsWithDistances, line) |
| 68 | + val beaconsOnLine: Set[Position] = beacons.filter(_.y == line) |
| 69 | + val count: Int = cover.map(_.size).sum - beaconsOnLine.size |
| 70 | + count |
| 71 | + |
| 72 | +def part2(input: String): Any = |
| 73 | + |
| 74 | + val parsed: List[(Position, Position)] = parse(input) |
| 75 | + val beacons: Set[Position] = parsed.map(_._2).toSet |
| 76 | + val sensorsWithDistances: List[(Position, Int)] = |
| 77 | + parsed.map( (sensor, beacon) => (sensor, distance(sensor, beacon)) ) |
| 78 | + |
| 79 | + val target: Range = 0 until 4_000_000 |
| 80 | + val spots: Seq[Position] = target.flatMap{ |
| 81 | + line => |
| 82 | + val cover: List[Range] = coverOfLine(sensorsWithDistances, line) |
| 83 | + val beaconsOnLine: Set[Position] = beacons.filter(_.y == line) |
| 84 | + |
| 85 | + val remainingRanges: List[Range] = cover.foldLeft(List(target)){ |
| 86 | + case (acc: List[Range], range: Range) => |
| 87 | + acc.flatMap( r => smartDiff(r, range) ) |
| 88 | + } |
| 89 | + val potential = remainingRanges.flatten.toSet |
| 90 | + |
| 91 | + val spotsOnLine = potential diff beaconsOnLine.map( b => b.x ) |
| 92 | + spotsOnLine.map( x => Position(x, line) ) |
| 93 | + } |
| 94 | + def tuningFrequency(p: Position): BigInt = BigInt(p.x) * 4_000_000 + p.y |
| 95 | + |
| 96 | + println(spots.mkString(", ")) |
| 97 | + assert(spots.size == 1) |
| 98 | + tuningFrequency(spots.head) |
0 commit comments