summaryrefslogtreecommitdiff
path: root/aoc_2023/day-03/part_1.ts
blob: 33a8107c1c74c42488ec6f0fe54a066420e65d02 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { JSONSet, Vec2 } from "@/utils";

const isNumeric = (symbol: string) => symbol <= "9" && symbol >= "0";

export const main = async (lines: string[]): Promise<number | string> => {
  const adjacentPoints: Vec2[] = [];

  const dimy = lines.length;
  for (let y = 0; y < lines.length; y++) {
    const dimx = lines[y].length;
    for (let x = 0; x < dimx; x++) {
      const symbol = lines[y][x];
      if (symbol != "." && !isNumeric(symbol)) {
        for (let dy = -1; dy <= 1; dy++) {
          for (let dx = -1; dx <= 1; dx++) {
            const newX = dx + x;
            const newY = dy + y;
            if (
              !(dy === dx && dy === 0) &&
              newX >= 0 &&
              newX < dimx &&
              newY < dimy &&
              newY >= 0
            ) {
              adjacentPoints.push({ x: x + dx, y: y + dy });
            }
          }
        }
      }
    }
  }

  let sum = 0;
  const visitedPoints: JSONSet<Vec2> = new JSONSet();
  for (const { x, y } of adjacentPoints) {
    const symbol = lines[y][x];
    if (!isNumeric(symbol)) continue;

    let num = "";
    if (!visitedPoints.has({ x, y })) {
      let x_i = x;
      while (x_i >= 0 && isNumeric(lines[y][x_i])) {
        x_i--;
      }
      x_i = Math.max(0, x_i + 1);

      while (x_i < lines[y].length && isNumeric(lines[y][x_i])) {
        num += lines[y][x_i];
        visitedPoints.add({ x: x_i, y });

        x_i++;
      }
    }

    if (num.trim() !== "") {
      sum += parseInt(num.trim());
    }
  }

  return sum;
};

//

const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
if (isrun) {
  const file = Bun.file("./problem.txt");
  const text = await file.text();
  const lines = text.split("\n").filter((x) => x && x.length);

  console.log("=== COMPUTATION ===\n");

  const answer = await main(lines);

  console.log("\n=== /COMPUTATION ===\n");

  console.log("=== ANSWER TO P1 ===");
  console.log(answer);
}