Race Condition

Advent of Code 2024 [Day 20]

20-12-2024

The Historians are quite pixelated again. This time, a massive, black building looms over you - you’re right outside the CPU!

While The Historians get to work, a nearby program sees that you’re idle and challenges you to a race. Apparently, you’ve arrived just in time for the frequently-held race condition festival!

The race takes place on a particularly long and twisting code path; programs compete to see who can finish in the fewest picoseconds. The winner even gets their very own mutex!

They hand you a map of the racetrack (your puzzle input). For example:

###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

The map consists of track (.) - including the start (S) and end (E) positions (both of which also count as track) - and walls (#).

When a program runs through the racetrack, it starts at the start position. Then, it is allowed to move up, down, left, or right; each such move takes 1 picosecond. The goal is to reach the end position as quickly as possible. In this example racetrack, the fastest time is 84 picoseconds.

Because there is only a single path from the start to the end and the programs all go the same speed, the races used to be pretty boring. To make things more interesting, they introduced a new rule to the races: programs are allowed to cheat.

The rules for cheating are very strict. Exactly once during a race, a program may disable collision for up to 2 picoseconds. This allows the program to pass through walls as if they were regular track. At the end of the cheat, the program must be back on normal track again; otherwise, it will receive a segmentation fault and get disqualified.

So, a program could complete the course in 72 picoseconds (saving 12 picoseconds) by cheating for the two moves marked 1 and 2:

###############
#...#...12....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

Or, a program could complete the course in 64 picoseconds (saving 20 picoseconds) by cheating for the two moves marked 1 and 2:

###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...12..#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

This cheat saves 38 picoseconds:

###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...#...#
###.####1##.###
#...###.2.#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

This cheat saves 64 picoseconds and takes the program directly to the end:

###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..21...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

Each cheat has a distinct start position (the position where the cheat is activated, just before the first move that is allowed to go through walls) and end position; cheats are uniquely identified by their start position and end position.

In this example, the total number of cheats (grouped by the amount of time they save) are as follows:

You aren’t sure what the conditions of the racetrack will be like, so to give yourself as many options as possible, you’ll need a list of the best cheats. How many cheats would save you at least 100 picoseconds?

type Position = [number, number];

function findStartEnd(grid: string[][]): [Position, Position] {
  let start: Position = [0, 0];
  let end: Position = [0, 0];

  for (let y = 0; y < grid.length; y++) {
    for (let x = 0; x < grid[y].length; x++) {
      if (grid[y][x] === 'S') start = [y, x];
      if (grid[y][x] === 'E') end = [y, x];
    }
  }

  return [start, end];
}

function getBasePathLength(grid: string[][], start: Position, end: Position): number {
  const queue: [Position, number][] = [[start, 0]];
  const visited = new Set<string>();

  while (queue.length > 0) {
    const [[y, x], steps] = queue.shift()!;

    if (y === end[0] && x === end[1]) return steps;

    const key = `${y},${x}`;
    if (visited.has(key)) continue;
    visited.add(key);

    const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
    for (const [dy, dx] of dirs) {
      const ny = y + dy;
      const nx = x + dx;
      if (
        nx < 0 || nx >= grid[0].length ||
        ny < 0 || ny >= grid.length ||
        grid[ny][nx] === '#'
      ) continue;
      queue.push([[ny, nx], steps + 1]);
    }
  }

  return Infinity;
}

function findPossibleCheats(grid: string[][], maxDistance: number, picoseconds: number): number {
  const [start, end] = findStartEnd(grid);
  const baseTime = getBasePathLength(grid, start, end);
  let cheats = 0;

  for (let y = 0; y < grid.length; y++) {
    for (let x = 0; x < grid[0].length; x++) {
      if (grid[y][x] === '#') continue;
      const timeToCheatStart = getBasePathLength(grid, start, [y, x]);
      if (timeToCheatStart === Infinity) continue;

      for (let ny = 0; ny < grid.length; ny++) {
        for (let nx = 0; nx < grid[0].length; nx++) {
          const portalSteps = Math.abs(ny - y) + Math.abs(nx - x);
          if (grid[ny][nx] === '#' || portalSteps > maxDistance) continue;

          const timeFromCheatEnd = getBasePathLength(grid, [ny, nx], end);
          if (timeFromCheatEnd === Infinity) continue;

          const totalTime = timeToCheatStart + portalSteps + timeFromCheatEnd;
          const timeSaved = baseTime - totalTime;
          if (timeSaved >= picoseconds) {
            cheats++;
            console.log(`${timeSaved}: ${y},${x} -> ${ny},${nx}`);
          }
        }
      }
    }
  }

  return cheats;
}

async function main() {
  const input =  await Deno.readTextFile("input.txt");
  const grid = input.split('\n').map(line => line.split(''));
  console.log(findPossibleCheats(grid, 2, 100));
}

main().catch(console.error);

The programs seem perplexed by your list of cheats. Apparently, the two-picosecond cheating rule was deprecated several milliseconds ago! The latest version of the cheating rule permits a single cheat that instead lasts at most 20 picoseconds.

Now, in addition to all the cheats that were possible in just two picoseconds, many more cheats are possible. This six-picosecond cheat saves 76 picoseconds:

###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#1#####.#.#.###
#2#####.#.#...#
#3#####.#.###.#
#456.E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

Because this cheat has the same start and end positions as the one above, it’s the same cheat, even though the path taken during the cheat is different:

###############
#...#...#.....#
#.#.#.#.#.###.#
#S12..#.#.#...#
###3###.#.#.###
###4###.#.#...#
###5###.#.###.#
###6.E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

Cheats don’t need to use all 20 picoseconds; cheats can last any amount of time up to and including 20 picoseconds (but can still only end when the program is on normal track). Any cheat time not used is lost; it can’t be saved for another cheat later.

You’ll still need a list of the best cheats, but now there are even more to choose between. Here are the quantities of cheats in this example that save 50 picoseconds or more:

Find the best cheats using the updated cheating rules. How many cheats would save you at least 100 picoseconds?

type Position = [number, number];

function findStartEnd(grid: string[][]): [Position, Position] {
  let start: Position = [0, 0];
  let end: Position = [0, 0]; 

  for (let y = 0; y < grid.length; y++) {
    for (let x = 0; x < grid[y].length; x++) {
      if (grid[y][x] === 'S') start = [y, x];
      if (grid[y][x] === 'E') end = [y, x];
    }
  }

  return [start, end];
}

function getBasePathLength(grid: string[][], start: Position, end: Position): number {
  const queue: [Position, number][] = [[start, 0]];
  const visited = new Set<string>();

  while (queue.length > 0) {
    const [[y, x], steps] = queue.shift()!;

    if (y === end[0] && x === end[1]) return steps;

    const key = `${y},${x}`;
    if (visited.has(key)) continue;
    visited.add(key);

    const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
    for (const [dy, dx] of dirs) {
      const ny = y + dy;
      const nx = x + dx;
      if (
        nx < 0 || nx >= grid[0].length ||
        ny < 0 || ny >= grid.length ||
        grid[ny][nx] === '#'
      ) continue;
      queue.push([[ny, nx], steps + 1]);
    }
  }

  return Infinity;
}

function findPossibleCheats(grid: string[][], maxDistance: number, picoseconds: number): number {
  const [start, end] = findStartEnd(grid);
  const baseTime = getBasePathLength(grid, start, end);
  const timeToEnd: number[][] = Array(grid.length).fill(null).map(() => Array(grid[0].length).fill(Infinity));
  let cheats = 0;

  for (let y = 0; y < grid.length; y++) {
    for (let x = 0; x < grid[0].length; x++) {
      if (grid[y][x] === '#') continue;
      const timeToCheatStart = getBasePathLength(grid, start, [y, x]);
      if (timeToCheatStart === Infinity) continue;

      for (let ny = 0; ny < grid.length; ny++) {
        for (let nx = 0; nx < grid[0].length; nx++) {
          const portalSteps = Math.abs(ny - y) + Math.abs(nx - x);
          if (grid[ny][nx] === '#' || portalSteps > maxDistance) continue;

          if (timeToEnd[ny][nx] === Infinity) {
            timeToEnd[ny][nx] = getBasePathLength(grid, [ny, nx], end);
          }
          const timeFromCheatEnd = timeToEnd[ny][nx];
          if (timeFromCheatEnd === Infinity) continue;

          const totalTime = timeToCheatStart + portalSteps + timeFromCheatEnd;
          const timeSaved = baseTime - totalTime;
          if (timeSaved >= picoseconds) {
            cheats++;
            console.log(`${timeSaved}: ${y},${x} -> ${ny},${nx}`);
          }
        }
      }
    }
  }

  return cheats;
}

async function main() {
  const input =  await Deno.readTextFile("input.txt");
  const grid = input.split('\n').map(line => line.split(''));
  console.log(findPossibleCheats(grid, 20, 100));
}

main().catch(console.error);
Click to show the input
#############################################################################################################################################
###...#...#.........#...#...###...#...###...#.....#...#####.............#.....###...#.......#...#...#...###.......#...#...#...###...###.....#
###.#.#.#.#.#######.#.#.#.#.###.#.#.#.###.#.#.###.#.#.#####.###########.#.###.###.#.#.#####.#.#.#.#.#.#.###.#####.#.#.#.#.#.#.###.#.###.###.#
#...#...#.#.....#...#.#.#.#...#.#.#.#.#...#.#...#.#.#...#...#...........#...#...#.#.#...#...#.#.#.#.#.#.#...#...#...#...#.#.#...#.#.#...#...#
#.#######.#####.#.###.#.#.###.#.#.#.#.#.###.###.#.#.###.#.###.#############.###.#.#.###.#.###.#.#.#.#.#.#.###.#.#########.#.###.#.#.#.###.###
#.......#.......#.....#.#...#.#.#.#.#.#.#...#...#.#.#...#...#.....#...#...#...#.#.#.#...#...#.#.#.#.#.#.#.#...#...#.......#.#...#.#.#...#.###
#######.###############.###.#.#.#.#.#.#.#.###.###.#.#.#####.#####.#.#.#.#.###.#.#.#.#.#####.#.#.#.#.#.#.#.#.#####.#.#######.#.###.#.###.#.###
###...#.............#...#...#...#.#.#.#.#...#...#...#.#.....#...#.#.#.#.#.#...#.#.#.#.....#...#...#.#.#.#.#.....#...#.......#...#.#.#...#...#
###.#.#############.#.###.#######.#.#.#.###.###.#####.#.#####.#.#.#.#.#.#.#.###.#.#.#####.#########.#.#.#.#####.#####.#########.#.#.#.#####.#
#...#...###...#.....#...#.......#...#.#.#...###...#...#.#.....#...#.#...#.#.#...#.#...###...#.......#.#.#.#.....#...#.......#...#.#.#...#...#
#.#####.###.#.#.#######.#######.#####.#.#.#######.#.###.#.#########.#####.#.#.###.###.#####.#.#######.#.#.#.#####.#.#######.#.###.#.###.#.###
#.....#.#...#.#.......#.###...#.....#...#.#...#...#...#.#...#.....#.....#.#.#...#.#...#...#.#.....#...#...#.#...#.#...###...#.#...#.#...#...#
#####.#.#.###.#######.#.###.#.#####.#####.#.#.#.#####.#.###.#.###.#####.#.#.###.#.#.###.#.#.#####.#.#######.#.#.#.###.###.###.#.###.#.#####.#
###...#.#...#.........#.#...#.....#.#.....#.#...#.....#...#...#...###...#.#.#...#.#...#.#.#.#.....#.#.....#.#.#.#...#...#...#.#.#...#.#.....#
###.###.###.###########.#.#######.#.#.#####.#####.#######.#####.#####.###.#.#.###.###.#.#.#.#.#####.#.###.#.#.#.###.###.###.#.#.#.###.#.#####
#...#...###...........#.#.#...#...#.#...#...#.....#...#...#.....#...#...#.#.#.#...#...#.#.#.#.#...#...#...#.#.#...#.#...#...#...#...#.#.#...#
#.###.###############.#.#.#.#.#.###.###.#.###.#####.#.#.###.#####.#.###.#.#.#.#.###.###.#.#.#.#.#.#####.###.#.###.#.#.###.#########.#.#.#.#.#
#.#...#######...#.....#.#...#.#.###...#.#...#.......#.#...#.#...#.#.....#...#.#.#...#...#...#.#.#...#...#...#.#...#.#.#...#.........#.#...#.#
#.#.#########.#.#.#####.#####.#.#####.#.###.#########.###.#.#.#.#.###########.#.#.###.#######.#.###.#.###.###.#.###.#.#.###.#########.#####.#
#.#.#...###...#.#.....#.#...#.#...#...#.....#.....#...#...#...#.#...........#...#.....#.......#.#...#...#.#...#.#...#.#...#.....#.....#.....#
#.#.#.#.###.###.#####.#.#.#.#.###.#.#########.###.#.###.#######.###########.###########.#######.#.#####.#.#.###.#.###.###.#####.#.#####.#####
#.#.#.#...#...#...###.#.#.#.#.#...#.........#...#...###...#.....#...###...#...#.........#.......#...#...#.#.#...#...#.....#.....#.#...#.#...#
#.#.#.###.###.###.###.#.#.#.#.#.###########.###.#########.#.#####.#.###.#.###.#.#########.#########.#.###.#.#.#####.#######.#####.#.#.#.#.#.#
#.#.#...#.....#...#...#.#.#.#.#.#.....#.....#...###.....#.#.###...#.#...#.###.#.......###.....#.....#.#...#.#.....#.....#...#...#...#.#.#.#.#
#.#.###.#######.###.###.#.#.#.#.#.###.#.#####.#####.###.#.#.###.###.#.###.###.#######.#######.#.#####.#.###.#####.#####.#.###.#.#####.#.#.#.#
#.#.#...#.....#.###...#.#.#...#.#...#.#...#...#...#...#.#.#...#...#.#...#.#...#.......#.....#.#.....#.#.#...#.....#...#.#.#...#...#...#...#.#
#.#.#.###.###.#.#####.#.#.#####.###.#.###.#.###.#.###.#.#.###.###.#.###.#.#.###.#######.###.#.#####.#.#.#.###.#####.#.#.#.#.#####.#.#######.#
#.#.#.....###.#.#...#.#.#.....#.....#.#...#.###.#.....#.#...#.#...#.#...#.#...#.#...#...#...#.#.....#.#.#.#...#...#.#...#...#.....#.#.......#
#.#.#########.#.#.#.#.#.#####.#######.#.###.###.#######.###.#.#.###.#.###.###.#.#.#.#.###.###.#.#####.#.#.#.###.#.#.#########.#####.#.#######
#.#.###...#...#...#...#...###.#.......#...#.#...#...#...###.#.#...#.#...#...#.#...#.#...#.#...#...#...#...#...#.#.#.....#.....#...#.#.......#
#.#.###.#.#.#############.###.#.#########.#.#.###.#.#.#####.#.###.#.###.###.#.#####.###.#.#.#####.#.#########.#.#.#####.#.#####.#.#.#######.#
#.#.....#...#...........#.....#.....#...#.#...#...#...#...#.#...#.#...#.#...#.#.....#...#...#.....#.#.........#.#.#...#.#.......#.#...#.....#
#.###########.#########.###########.#.#.#.#####.#######.#.#.###.#.###.#.#.###.#.#####.#######.#####.#.#########.#.#.#.#.#########.###.#.#####
#.#...........#...#.....#.......#...#.#.#...#...#...###.#.#...#.#...#.#.#.###.#.#...#.......#.#...#.#.#...#...#.#.#.#.#.#.........#...#.....#
#.#.###########.#.#.#####.#####.#.###.#.###.#.###.#.###.#.###.#.###.#.#.#.###.#.#.#.#######.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#########.#######.#
#.#.#...........#.#...#...#.....#.#...#.#...#.#...#...#.#.#...#...#.#.#.#...#.#.#.#.#...#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...........#...#...#
#.#.#.###########.###.#.###.#####.#.###.#.###.#.#####.#.#.#.#####.#.#.#.###.#.#.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#############.#.#.###
#...#.#.........#.....#.#...#...#...#...#.###...#...#...#.#...#...#.#.#...#...#.#.#...#.#.###.#.#.#.#...#...#.#.#...#...#.............#...###
#####.#.#######.#######.#.###.#.#####.###.#######.#.#####.###.#.###.#.###.#####.#.#####.#.###.#.#.#.#########.#.#########.###################
#.....#.#.......#...#...#...#.#.....#...#.........#.###...#...#...#.#.#...#.....#.###...#.#...#.#...#.........#.#.....#...#...........#.....#
#.#####.#.#######.#.#.#####.#.#####.###.###########.###.###.#####.#.#.#.###.#####.###.###.#.###.#####.#########.#.###.#.###.#########.#.###.#
#.......#.........#...#.....#.#.....###.............#...#...#...#.#.#.#.###.....#...#.#...#...#...###...........#...#...#...#...#...#...#...#
#######################.#####.#.#####################.###.###.#.#.#.#.#.#######.###.#.#.#####.###.#################.#####.###.#.#.#.#####.###
#...#...................#...#.#...................#...#...#...#...#.#.#.#.......#...#.#.....#...#.#.....#.........#.......#...#...#.....#...#
#.#.#.###################.#.#.###################.#.###.###.#######.#.#.#.#######.###.#####.###.#.#.###.#.#######.#########.###########.###.#
#.#.#...#.................#...#...#...#.....#...#.#.#...###.......#.#...#.......#...#.#.....###...#...#...###...#.#.....#...###.......#.#...#
#.#.###.#.#####################.#.#.#.#.###.#.#.#.#.#.###########.#.###########.###.#.#.#############.#######.#.#.#.###.#.#####.#####.#.#.###
#.#...#...#...#...#.............#.#.#...#...#.#.#.#.#.........###...#...........#...#...#...#...#.....#.......#.#...###...#.....#...#.#.#...#
#.###.#####.#.#.#.#.#############.#.#####.###.#.#.#.#########.#######.###########.#######.#.#.#.#.#####.#######.###########.#####.#.#.#.###.#
#...#.......#...#...#...#.......#...#...#.....#...#...#...#...#...###.........#...#...#...#...#...#.....#...#...#...###.....#...#.#...#.#...#
###.#################.#.#.#####.#####.#.#############.#.#.#.###.#.###########.#.###.#.#.###########.#####.#.#.###.#.###.#####.#.#.#####.#.###
###.#...#...#.........#...#...#.......#.....#.....###...#...#...#...#.........#.###.#.#.............#.....#...###.#...#.#.....#...#...#...###
###.#.#.#.#.#.#############.#.#############.#.###.###########.#####.#.#########.###.#.###############.###########.###.#.#.#########.#.#######
#...#.#.#.#...###...........#...............#...#.#...#...###...#...#.#...#...#.#...#.....#...###...#.#...###...#.#...#.#.......#...#.#...###
#.###.#.#.#######.#############################.#.#.#.#.#.#####.#.###.#.#.#.#.#.#.#######.#.#.###.#.#.#.#.###.#.#.#.###.#######.#.###.#.#.###
#.....#...###...#.........#...#.................#...#...#.......#...#...#...#.#.#...#...#...#...#.#.#...#.....#...#...#.#.......#.#...#.#...#
#############.#.#########.#.#.#.###################################.#########.#.###.#.#.#######.#.#.#################.#.#.#######.#.###.###.#
#.............#...........#.#...#...............#...#...#...#.......#...#.....#...#.#.#.......#.#.#...#.........#...#...#.......#.#.###.#...#
#.#########################.#####.#############.#.#.#.#.#.#.#.#######.#.#.#######.#.#.#######.#.#.###.#.#######.#.#.###########.#.#.###.#.###
#...................#.....#.....#.............#.#.#...#...#...#...###.#.#.#...#...#...#.......#.#...#.#.......#.#.#...#...#...#...#...#.#...#
###################.#.###.#####.#############.#.#.#############.#.###.#.#.#.#.#.#######.#######.###.#.#######.#.#.###.#.#.#.#.#######.#.###.#
#...................#.#...#...#.###...#.......#.#.#.........#...#.....#...#.#.#.#...###.......#.....#.........#.#.#...#.#...#...#.....#.#...#
#.###################.#.###.#.#.###.#.#.#######.#.#.#######.#.#############.#.#.#.#.#########.#################.#.#.###.#######.#.#####.#.###
#...#...#.......#...#.#.#...#...#...#...#...###...#...#.....#.#.....#.....#.#.#.#.#.....#...#...#.....#.......#...#...#...#...#...#.....#...#
###.#.#.#.#####.#.#.#.#.#.#######.#######.#.#########.#.#####.#.###.#.###.#.#.#.#.#####.#.#.###.#.###.#.#####.#######.###.#.#.#####.#######.#
###...#...#...#...#...#...#.....#.........#...#...#...#.....#.#.#...#...#...#...#.....#...#.....#...#...#.....###...#.....#.#.......#...#...#
###########.#.#############.###.#############.#.#.#.#######.#.#.#.#####.#############.#############.#####.#######.#.#######.#########.#.#.###
#####...#...#.......#...###...#.......#...#...#.#.#.#.......#...#...#...#...........#.....#...#...#.#.....#.......#.........#.........#...###
#####.#.#.#########.#.#.#####.#######.#.#.#.###.#.#.#.#############.#.###.#########.#####.#.#.#.#.#.#.#####.#################.###############
###...#...#.....#...#.#.#...#.#.....#.#.#.#.....#...#.......#.......#...#.....#.....#.....#.#.#.#.#.#.#.....#.............#...###.......#...#
###.#######.###.#.###.#.#.#.#.#.###.#.#.#.#################.#.#########.#####.#.#####.#####.#.#.#.#.#.#.#####.###########.#.#####.#####.#.#.#
#...#.....#.#...#.#...#...#.#...#...#...#...#.......#.......#.......#...#.....#...#...#...#.#...#...#...#...#...#...#...#...#...#.#.....#.#.#
#.###.###.#.#.###.#.#######.#####.#########.#.#####.#.#############.#.###.#######.#.###.#.#.#############.#.###.#.#.#.#.#####.#.#.#.#####.#.#
#...#...#...#...#.#.......#...###.........#.#.....#.#...........#...#.#...#.....#...#...#.#.#...###.......#.....#.#.#.#.#...#.#.#.#.......#.#
###.###.#######.#.#######.###.###########.#.#####.#.###########.#.###.#.###.###.#####.###.#.#.#.###.#############.#.#.#.#.#.#.#.#.#########.#
#...#...###.....#.#.....#...#...###...#...#.#.....#...#.........#.....#.....###.......###...#.#.....#########.....#...#...#...#...#...#.....#
#.###.#####.#####.#.###.###.###.###.#.#.###.#.#######.#.#####################################.###############.#####################.#.#.#####
#.....#...#.....#...#...###...#.#...#.#...#.#.#.....#.#.......#.....#...#.....#...............###############.#.......#.....#...#...#.#.....#
#######.#.#####.#####.#######.#.#.###.###.#.#.#.###.#.#######.#.###.#.#.#.###.#.#############################.#.#####.#.###.#.#.#.###.#####.#
#...#...#.#...#.....#.#...###.#.#...#.....#...#.#...#.........#.#...#.#...#...#..E###########################.#.#...#...###...#.#...#.....#.#
#.#.#.###.#.#.#####.#.#.#.###.#.###.###########.#.#############.#.###.#####.#################################.#.#.#.###########.###.#####.#.#
#.#.#...#...#.......#...#.....#...#.........#...#.........#...#.#...#.#.....#...........#######...#.....###S..#...#.....#...#...#...#...#...#
#.#.###.#########################.#########.#.###########.#.#.#.###.#.#.#####.#########.#######.#.#.###.###############.#.#.#.###.###.#.#####
#.#.###.................#...#...#...........#...#.........#.#.#...#.#.#...###.........#.#.......#.#.#...#...............#.#.#...#...#.#.#...#
#.#.###################.#.#.#.#.###############.#.#########.#.###.#.#.###.###########.#.#.#######.#.#.###.###############.#.###.###.#.#.#.#.#
#.#.#.............#...#.#.#.#.#...........###...#...###.....#...#.#...#...#.....#.....#.#...#...#...#.....#...###.........#...#...#.#.#.#.#.#
#.#.#.###########.#.#.#.#.#.#.###########.###.#####.###.#######.#.#####.###.###.#.#####.###.#.#.###########.#.###.###########.###.#.#.#.#.#.#
#.#.#...........#.#.#.#...#...#...#.....#...#...#...#...#.......#.....#...#.#...#.....#...#...#...#.........#...#.#.........#...#.#.#.#...#.#
#.#.###########.#.#.#.#########.#.#.###.###.###.#.###.###.###########.###.#.#.#######.###.#######.#.###########.#.#.#######.###.#.#.#.#####.#
#.#.......#...#.#...#.###...#...#...###.#...#...#.....#...#...#.....#.#...#.#.....###...#.#...###.#.#.........#...#.......#.#...#...#...#...#
#.#######.#.#.#.#####.###.#.#.#########.#.###.#########.###.#.#.###.#.#.###.#####.#####.#.#.#.###.#.#.#######.###########.#.#.#########.#.###
#.#.....#...#...#...#.....#...#.......#.#.###.....#.....###.#.#.#...#.#.....#...#.#.....#...#...#...#.....###.....#.......#...#...#####.#...#
#.#.###.#########.#.###########.#####.#.#.#######.#.#######.#.#.#.###.#######.#.#.#.###########.#########.#######.#.###########.#.#####.###.#
#.#.###...........#.#.......#...###...#...#...#...#.....#...#.#.#.#...#.......#...#...........#.###.....#.#...###.#.............#...#...#...#
#.#.###############.#.#####.#.#####.#######.#.#.#######.#.###.#.#.#.###.#####################.#.###.###.#.#.#.###.#################.#.###.###
#...#...............#.....#.#...#...#.....#.#.#.....#...#...#...#...#...#...#...###.......#...#...#...#.#...#...#.#...#...#...#...#...#...###
#####.###################.#.###.#.###.###.#.#.#####.#.#####.#########.###.#.#.#.###.#####.#.#####.###.#.#######.#.#.#.#.#.#.#.#.#.#####.#####
#...#.................#...#.#...#...#...#.#.#...#...#...###.....#...#.....#.#.#.#...#.....#...#...#...#.....#...#...#...#...#...#...#...#####
#.#.#################.#.###.#.#####.###.#.#.###.#.#####.#######.#.#.#######.#.#.#.###.#######.#.###.#######.#.#####################.#.#######
#.#.#...#.......###...#.#...#.#.....#...#.#.#...#.....#...#.....#.#.........#.#.#...#.###.....#...#.......#...#...###...#...#...###...#...###
#.#.#.#.#.#####.###.###.#.###.#.#####.###.#.#.#######.###.#.#####.###########.#.###.#.###.#######.#######.#####.#.###.#.#.#.#.#.#######.#.###
#.#.#.#.#.....#.....#...#.....#.......#...#.#.#...#...#...#.....#.#...#.....#.#.#...#...#.....#...###.....#...#.#...#.#...#...#.........#...#
#.#.#.#.#####.#######.#################.###.#.#.#.#.###.#######.#.#.#.#.###.#.#.#.#####.#####.#.#####.#####.#.#.###.#.#####################.#
#.#.#.#.#.....#...#...#...###...#.......###.#.#.#.#...#.#...###.#.#.#.#.#...#.#.#...#...#...#.#.#...#.....#.#.#.#...#...#...................#
#.#.#.#.#.#####.#.#.###.#.###.#.#.#########.#.#.#.###.#.#.#.###.#.#.#.#.#.###.#.###.#.###.#.#.#.#.#.#####.#.#.#.#.#####.#.###################
#.#...#.#.......#...#...#.....#.#.....#.....#.#.#.#...#.#.#.#...#.#.#.#.#.#...#...#.#.#...#.#.#.#.#.#...#...#...#...#...#...................#
#.#####.#############.#########.#####.#.#####.#.#.#.###.#.#.#.###.#.#.#.#.#.#####.#.#.#.###.#.#.#.#.#.#.###########.#.#####################.#
#.....#...............#.......#.....#.#.....#...#.#...#.#.#.#...#...#.#.#.#...#...#.#.#...#...#.#.#...#.#.........#...#.....................#
#####.#################.#####.#####.#.#####.#####.###.#.#.#.###.#####.#.#.###.#.###.#.###.#####.#.#####.#.#######.#####.#####################
#.....#...#.....#.....#...#...#...#...#...#.#.....#...#...#...#.#...#.#.#.#...#...#.#.....#.....#.....#.#...#...#.....#.....................#
#.#####.#.#.###.#.###.###.#.###.#.#####.#.#.#.#####.#########.#.#.#.#.#.#.#.#####.#.#######.#########.#.###.#.#.#####.#####################.#
#...#...#.#.#...#...#.#...#...#.#.#...#.#...#.....#...#.......#.#.#.#...#.#...#...#.......#...#...#...#...#...#...#...#.......#.............#
###.#.###.#.#.#####.#.#.#####.#.#.#.#.#.#########.###.#.#######.#.#.#####.###.#.#########.###.#.#.#.#####.#######.#.###.#####.#.#############
###.#...#.#.#.#.....#.#.#.....#.#...#.#.###...#...#...#...#...#...#...###.#...#.#...#...#.###...#.#.#...#.#.....#.#...#.....#.#.....#.......#
###.###.#.#.#.#.#####.#.#.#####.#####.#.###.#.#.###.#####.#.#.#######.###.#.###.#.#.#.#.#.#######.#.#.#.#.#.###.#.###.#####.#.#####.#.#####.#
#...#...#.#.#.#.#.....#.#.#.....#.....#.#...#.#...#.#.....#.#...#...#.#...#.#...#.#...#.#.....###...#.#...#...#.#.#...#.....#.#...#...#.....#
#.###.###.#.#.#.#.#####.#.#.#####.#####.#.###.###.#.#.#####.###.#.#.#.#.###.#.###.#####.#####.#######.#######.#.#.#.###.#####.#.#.#####.#####
#.#...###...#...#...#...#.#...#...#...#.#...#.....#.#.....#...#.#.#...#...#.#...#.....#.#.....#.......#...#...#.#.#...#.....#...#.#...#.....#
#.#.###############.#.###.###.#.###.#.#.###.#######.#####.###.#.#.#######.#.###.#####.#.#.#####.#######.#.#.###.#.###.#####.#####.#.#.#####.#
#.#.#...###.........#.#...#...#...#.#...###.#...#...#.....#...#.#.....#...#.###.....#.#.#.....#.#.....#.#...#...#...#.#...#...###...#.....#.#
#.#.#.#.###.#########.#.###.#####.#.#######.#.#.#.###.#####.###.#####.#.###.#######.#.#.#####.#.#.###.#.#####.#####.#.#.#.###.###########.#.#
#...#.#...#.....#.....#.....#.....#...#.....#.#...###...#...###...#...#...#...#.....#.#.#...#.#.#.#...#...###.#...#.#.#.#...#.#.........#.#.#
#####.###.#####.#.###########.#######.#.#####.#########.#.#######.#.#####.###.#.#####.#.#.#.#.#.#.#.#####.###.#.#.#.#.#.###.#.#.#######.#.#.#
#...#...#.#...#.#...#.........#.......#.....#...#...#...#.......#...#.....#...#.#...#.#.#.#.#.#...#.#...#...#.#.#.#.#...###.#...#...#...#.#.#
#.#.###.#.#.#.#.###.#.#########.###########.###.#.#.#.#########.#####.#####.###.#.#.#.#.#.#.#.#####.#.#.###.#.#.#.#.#######.#####.#.#.###.#.#
#.#.....#...#...#...#.#.....#...###...#.....#...#.#...#.........#...#...#...###.#.#...#.#.#.#...###...#...#.#...#.#.......#...#...#.#...#.#.#
#.###############.###.#.###.#.#####.#.#.#####.###.#####.#########.#.###.#.#####.#.#####.#.#.###.#########.#.#####.#######.###.#.###.###.#.#.#
#...........#...#...#.#.#...#.###...#...#...#.###.....#.#.....#...#.....#...#...#...###.#.#...#...#.......#.....#.#...#...###...#...#...#.#.#
###########.#.#.###.#.#.#.###.###.#######.#.#.#######.#.#.###.#.###########.#.#####.###.#.###.###.#.###########.#.#.#.#.#########.###.###.#.#
#...........#.#.#...#...#...#...#.........#.#.#...#...#...#...#...#...#...#.#.#.....#...#.###.#...#...#...#...#.#.#.#.#.......#...#...#...#.#
#.###########.#.#.#########.###.###########.#.#.#.#.#######.#####.#.#.#.#.#.#.#.#####.###.###.#.#####.#.#.#.#.#.#.#.#.#######.#.###.###.###.#
#.#.......#...#.#.#.........#...###.......#...#.#.#...#...#.###...#.#.#.#.#.#.#.....#...#...#.#.....#.#.#.#.#.#.#.#.#.#...#...#.#...###...#.#
#.#.#####.#.###.#.#.#########.#####.#####.#####.#.###.#.#.#.###.###.#.#.#.#.#.#####.###.###.#.#####.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#######.#.#
#...###...#...#.#.#.#...#...#.#.....#...#...###.#.....#.#.#...#...#.#.#.#.#.#...#...#...#...#.......#.#.#.#.#.#.#.#.#.#.#.#...#...###.....#.#
#######.#####.#.#.#.#.#.#.#.#.#.#####.#.###.###.#######.#.###.###.#.#.#.#.#.###.#.###.###.###########.#.#.#.#.#.#.#.#.#.#.###.#######.#####.#
#.......#.....#...#.#.#...#...#.....#.#...#.....#.....#.#.#...#...#.#.#.#.#.#...#.###...#...........#.#.#.#.#.#.#.#.#.#.#...#...#...#.#...#.#
#.#######.#########.#.#############.#.###.#######.###.#.#.#.###.###.#.#.#.#.#.###.#####.###########.#.#.#.#.#.#.#.#.#.#.###.###.#.#.#.#.#.#.#
#...#...#.........#.#.#.............#.#...#.....#.#...#.#.#.#...#...#.#.#.#.#.#...#.....#...#.....#.#.#.#.#.#.#.#.#.#.#...#...#...#.#.#.#.#.#
###.#.#.#########.#.#.#.#############.#.###.###.#.#.###.#.#.#.###.###.#.#.#.#.#.###.#####.#.#.###.#.#.#.#.#.#.#.#.#.#.###.###.#####.#.#.#.#.#
###...#...........#...#...............#.....###...#.....#...#.....###...#...#...###.......#...###...#...#...#...#...#.....###.......#...#...#
#############################################################################################################################################