18

Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] Rin@lemm.ee 1 points 2 weeks ago

This one was a little bit of a pain. I loved it.

TypeScript

Solution

import { AdventOfCodeSolutionFunction } from "./solutions";

enum Direction {
    UP,
    UP_RIGHT,
    RIGHT,
    BOTTOM_RIGHT,
    BOTTOM,
    BOTTOM_LEFT,
    LEFT,
    UP_LEFT,
};

const ALL_DIRECTIONS = [
    Direction.RIGHT,
    Direction.BOTTOM_RIGHT,
    Direction.BOTTOM,
    Direction.BOTTOM_LEFT,
    Direction.LEFT,
    Direction.UP_LEFT,
    Direction.UP,
    Direction.UP_RIGHT,
];

const check_coords = (grid: Array<Array<string>>, x: number, y: number) => {
    return y >= grid.length ||
        y < 0 ||
        x >= grid[y].length ||
        x < 0
}

const search_direction = (grid: Array<Array<string>>, x: number, y: number, direction: Direction, find: Array<string>) => {
    // exit conditions
    // no more to find
    if (find.length == 0)
        return 1; // found the end

    // invalid coords
    if (check_coords(grid, x, y))
        return 0;

    // make new mutable list
    const newFind = [...find];
    const searchChar = newFind.shift();

    // wrong character
    if (grid[y][x] !== searchChar)
        return 0;

    switch (direction) {
        case Direction.UP:
            return search_direction(grid, x, y + 1, direction, newFind);

        case Direction.UP_RIGHT:
            return search_direction(grid, x + 1, y + 1, direction, newFind);

        case Direction.RIGHT:
            return search_direction(grid, x + 1, y, direction, newFind);

        case Direction.BOTTOM_RIGHT:
            return search_direction(grid, x + 1, y - 1, direction, newFind);

        case Direction.BOTTOM:
            return search_direction(grid, x, y - 1, direction, newFind);

        case Direction.BOTTOM_LEFT:
            return search_direction(grid, x - 1, y - 1, direction, newFind);

        case Direction.LEFT:
            return search_direction(grid, x - 1, y, direction, newFind);

        case Direction.UP_LEFT:
            return search_direction(grid, x - 1, y + 1, direction, newFind);

        default:
            return 0;
    }
}

const part_1_search = (grid: Array<Array<string>>, x: number, y: number, find: Array<string>) => {
    return ALL_DIRECTIONS.reduce<number>(
        (instances, direction) =>
            instances + search_direction(grid, x, y, direction, find),
        0
    );
}

const part_2_search = (grid: Array<Array<string>>, x: number, y: number, find: Array<string>) => {
    return (
        search_direction(grid, x - 1, y + 1, Direction.BOTTOM_RIGHT, find) +
        search_direction(grid, x + 1, y + 1, Direction.BOTTOM_LEFT, find) +
        search_direction(grid, x - 1, y - 1, Direction.UP_RIGHT, find) +
        search_direction(grid, x + 1, y - 1, Direction.UP_LEFT, find)
    ) == 2 ? 1 : 0;
}

export const solution_4: AdventOfCodeSolutionFunction = (input) => {
    const grid = input.split("\n").map(st => st.trim()).map(v => v.split(""));

    let part_1 = 0;
    let part_2 = 0;

    const find_1 = "XMAS".split("");
    const find_2 = "MAS".split("");

    for (let y = 0; y < grid.length; y++) {
        for (let x = 0; x < grid[y].length; x++) {
            part_1 += part_1_search(grid, x, y, find_1);
            part_2 += part_2_search(grid, x, y, find_2);
        }
    }

    return {
        part_1,
        part_2,
    };
}

Felt like this code quality is better than what I usually output :)

this post was submitted on 04 Dec 2024
18 points (95.0% liked)

Advent Of Code

920 readers
58 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS