Advent of Code 2022: Day 15

the Maths with Sensors and beacons 🧭💻

by Amy L

HTML

<link rel="stylesheet" href="https://adventofcode.com/static/style.css?30">
<h1><a href="https://adventofcode.com/2022/day/15" target="_blank">Day 15</a></h1>
<diV class="puzzle-input">
  <label for="INPUT_DATA">Input</label>
  <textarea id="INPUT_DATA" autocomplete="off" placeholder="paste your input here" rows="7" cols="50"></textarea>
</diV>
<dl>
<dt>Part 1</dt>
  <dd>
    <label>Answer:
      <input type="text" id="answer1" readonly />
    </label>
  </dd>
  
  <dt>Part 2</dt>
  <dd>
    <label>Answer:
    <input type="text" id="answer2" readonly />
    </label>
  </dd>
</dl>

<canvas id="map1"></canvas>

JavaScript 1.7

const SYMBOLS = {
  BEACON: 'B',
  SENSOR: 'S'
};
const GLOBALS = {
  minX: null,
  minY: null,
  maxX: null,
  maxY: null
}
function Day15(locationData) {
  console.log(locationData)
  const { beaconsAndSensors, sensors, maxDistance } = buildSensorAndBeaconMap(locationData);
  GLOBALS.minX = locationData.min.x;
  GLOBALS.minY = locationData.min.y;
  GLOBALS.maxX = locationData.max.x;
  GLOBALS.maxY = locationData.max.y;
  console.log(GLOBALS)
  console.log({ sensors, maxDistance })

  async function solvePart1(y) {
    const confirmedNotBeaconCoordinates = [];
    const end = GLOBALS.maxX + maxDistance;
    const start = GLOBALS.minX - maxDistance;
    for (let x = start; x <= end; x++) {
      if (x % 500000 === 0) console.log(`x=${x} ${(((x - start) / (end - start))*100).toFixed(0)}%`);
      const nonBeaconsInRange = isConfirmedNoBeacon(beaconsAndSensors, x, y, sensors);
      if (nonBeaconsInRange.length) {
        confirmedNotBeaconCoordinates.push({x,y});
      }
    }
    return getUnique(confirmedNotBeaconCoordinates).length;
  }

  async function solvePart2(upperBound) {
    const sensorsPerimeters = [];
    for (let sensor of sensors) {
      const plusOne = sensor.distanceToBeacon + 1;
      getPerimeterCoordinates(sensorsPerimeters, sensor, 1, 1, plusOne, upperBound);
      getPerimeterCoordinates(sensorsPerimeters, sensor, -1, 1, plusOne, upperBound);
      getPerimeterCoordinates(sensorsPerimeters, sensor, 1, -1, plusOne, upperBound);
      getPerimeterCoordinates(sensorsPerimeters, sensor, -1, -1, plusOne, upperBound);
    }
    console.log('Possible options based on perimeters of sensors', sensorsPerimeters.length);

    const possible = getUnique(sensorsPerimeters);
    console.log('After removing the duplicate entries, theres ', possible.length, 'possible options remaining')

    const pointsOutOfRangeFromAnySensor = possible.filter(onlyPointsOutOfRangeOfSensor(sensors)).filter(removeDuplicates);
    console.log('Should be only 1 left',...