LogoWCF

dallinstevens's Solution

2025 Solution Deno

Output

Path: 8N9E2N2E4N11E3N3E1N25E
interface Coordinates {
  x: number;
  y: number;
}

interface Node {
  path: string;
  coordinates: Coordinates;
}

class Mapper {
  private map: string[];
  private visitedMap: Map<string, boolean>;
  private startingCoordinates: Coordinates = { x: 0, y: 0 };
  private endingCoordinates: Coordinates = { x: 0, y: 0 };

  constructor(mapSpec: string[]) {
    this.map = mapSpec;
    this.startingCoordinates = this.findCoordinates("S");
    this.endingCoordinates = this.findCoordinates("E");
    this.visitedMap = new Map();
  }

  private coordKey(x: number, y: number) {
    return `${x},${y}`;
  }

  private findCoordinates(symbol: string): Coordinates {
    let coordinates: Coordinates = { x: 0, y: 0 };
    for (let y = 0; y < this.map.length; y++) {
      for (let x = 0; x < this.map[y].length; x++) {
        if (this.map[y][x] === symbol) {
          coordinates = { x, y };
        }
      }
    }
    return coordinates;
  }

  private isValidMove(x: number, y: number): boolean {
    if (x < 0 || y < 0 || y >= this.map.length || x >= this.map[0].length) {
      return false;
    }
    if (this.map[y][x] === "X" || this.map[y][x] === "S") {
      return false;
    }
    if (this.visitedMap.get(this.coordKey(x, y)) === true) {
      return false;
    }
    return true;
  }

  public getPath(): string {
    const path = this.findPath([
      { path: "", coordinates: this.startingCoordinates },
    ]);
    let directions = "";
    for (let i = 0; i < path.length; ) {
      const curLetter = path[i];
      let letterCounter = 0;
      while (true) {
        if (path[i] === curLetter) {
          letterCounter++;
          i++;
        } else {
          break;
        }
      }
      directions += String(letterCounter) + curLetter;
    }
    return directions;
  }

  private findPath(checkList: Node[]): string {
    const newCheckList: Node[] = [];
    for (const path of checkList) {
      if (
        path.coordinates.x === this.endingCoordinates.x &&
        path.coordinates.y === this.endingCoordinates.y
      ) {
        return path.path;
      }
      if (this.isValidMove(path.coordinates.x, path.coordinates.y - 1)) {
        // check up
        newCheckList.push({
          path: path.path + "N",
          coordinates: { x: path.coordinates.x, y: path.coordinates.y - 1 },
        });
        this.visitedMap.set(
          this.coordKey(path.coordinates.x, path.coordinates.y - 1),
          true
        );
      }
      if (this.isValidMove(path.coordinates.x, path.coordinates.y + 1)) {
        //check down
        newCheckList.push({
          path: path.path + "S",
          coordinates: { x: path.coordinates.x, y: path.coordinates.y + 1 },
        });
        this.visitedMap.set(
          this.coordKey(path.coordinates.x, path.coordinates.y + 1),
          true
        );
      }
      if (this.isValidMove(path.coordinates.x - 1, path.coordinates.y)) {
        //check left
        newCheckList.push({
          path: path.path + "W",
          coordinates: { x: path.coordinates.x - 1, y: path.coordinates.y },
        });
        this.visitedMap.set(
          this.coordKey(path.coordinates.x - 1, path.coordinates.y),
          true
        );
      }
      if (this.isValidMove(path.coordinates.x + 1, path.coordinates.y)) {
        //check right
        newCheckList.push({
          path: path.path + "E",
          coordinates: { x: path.coordinates.x + 1, y: path.coordinates.y },
        });
        this.visitedMap.set(
          this.coordKey(path.coordinates.x + 1, path.coordinates.y),
          true
        );
      }
    }
    return this.findPath(newCheckList);
  }
}

if (import.meta.main) {
  const map = (await Deno.readTextFile("map.txt")).split("\n");
  const mapper = new Mapper(map);
  console.log(mapper.getPath());
}

Day 7: Buddy's PathFinder

A large amount of planning goes into Santa’s trek every year. There is a common misconception that his sleigh flies as high as he wants it to, but that is simply not true. The higher he gets in the air, the thinner the air is, and the reindeer begin to have trouble getting enough oxygen to produce the lift that Santa’s sleigh requires.

Creating Santa’s travel plans takes a great deal of effort, and Buddy (lead elf over sleigh maintenance and travel) needs you to help out mapping the path from Salt Lake to Denver. The terrain was measured using a tool called a PathFinder that checks the topography at the specific altitude that Santa is planned to be flying at. The Rocky Mountains are very tall, so a path through the canyons will need to be discovered.

Problem: Included is a file that contains the map of the area output by the PathFinder. The map in map.txt shows mountains as “X“‘s and open air as “O“‘s (letter). We went ahead and marked Santa’s planned positioning at the start of your leg of the journey with an “S”, and the location we want him to be leaving your leg of the journey as “E”. How he gets there is up to you. Each letter correlates to about 5 square miles, so the output of the program will be the distance Santa needs to go a direction, and the direction(first letter of the direction). The map is laid out such that the top of the map is North, the bottom is South, to the right is East, and to the left is West.

Example: Given the map:

OOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOXXXXXOOOOOOOOOOO
SOOOOOXXXXXXXXXXXOOOOOOOOOOO
OOOOOOOXXXXXXXXOOOOOOOOOOOOO
OOOOOOOOOOXXXXOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOXXXXOOOOOOOE
OOOOXXXOOOOOOOOOOOXXXXOOOOOO
OOXXXXXXOOOOOOOOOOOOOOOOOOOO

A Valid Solution would be:

2N28E5S

Which describes the path:

|---------------------------
|OOOOOOOOOOOXXXXXOOOOOOOOOO|
SOOOOOXXXXXXXXXXXOOOOOOOOOO|
OOOOOOOXXXXXXXXOOOOOOOOOOOO|
OOOOOOOOOOXXXXOOOOOOOOOOOOO|
OOOOOOOOOOOOOOOOXXXXOOOOOOOE
OOOOXXXOOOOOOOOOOOXXXXOOOOOO
OOXXXXXXOOOOOOOOOOOOOOOOOOOO

Challenge: Find the optimal path for Santa to go!