Skip to content

Commit

Permalink
add 2018/11 and 2018/18
Browse files Browse the repository at this point in the history
  • Loading branch information
CForrest97 committed Nov 26, 2023
1 parent 4654fc0 commit 64e4d52
Show file tree
Hide file tree
Showing 11 changed files with 2,635 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<h1 align="center">Advent of Code [2022] 👋</h1>
<h1 align="center">Advent of Code 👋</h1>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000" />
<a href="#" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
</a>
</p>

> My hopefully clean-ish complete 2022 solutions to Advent of Code using Typescript with a datastructures library and ramda
> My hopefully clean-ish complete solutions to Advent of Code using Typescript with ramda
## What is Advent of Code?

Expand Down
48 changes: 33 additions & 15 deletions src/.template/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { solvePart1, solvePart2 } from "./index";
import { InputType, readInput } from "../../helpers/readInput";

describe.skip("Day __", () => {
describe.each<[InputType, number, number]>([
["simpleInput", 0, 0],
// ["puzzleInput", 0, 0]
])("%s", (filename, expectedPart1, expectedPart2) => {
it("should solve part 1", async () => {
const input = await readInput(__dirname, filename);

expect(solvePart1(input)).toBe(expectedPart1);
import { day } from "./index";

describe("2023/__", () => {
describe("part 1", () => {
it("should solve the simple input", async () => {
const input = await day.getSimpleInput();

const output = day.solvePart1(input);

expect(output).toBe(-1);
});

it("should solve the puzzle input", async () => {
const input = await day.getPuzzleInput();

const output = day.solvePart1(input);

expect(output).toBe(-1);
});
});

describe("part 2", () => {
it("should solve the simple input", async () => {
const input = await day.getSimpleInput();

const output = day.solvePart2(input);

expect(output).toBe(-1);
});

it("should solve the puzzle input", async () => {
const input = await day.getPuzzleInput();

it("should solve part 2", async () => {
const input = await readInput(__dirname, filename);
const output = day.solvePart2(input);

expect(solvePart2(input)).toBe(expectedPart2);
expect(output).toBe(-1);
});
});
});
18 changes: 15 additions & 3 deletions src/.template/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { readInput } from "../helpers/readInput";
import { Day } from "../helpers/types";

const parseInput = (input: string) => input;

const solve = (input: string) => input;
const solve = (input: string) => input.length;

const solvePart1 = (input: string) => solve(parseInput(input));
const solvePart2 = (input: string) => solve(parseInput(input));

export const solvePart1 = (input: string) => solve(parseInput(input));
export const solvePart2 = (input: string) => solve(parseInput(input));
export const day: Day = {
day: 0,
year: 2023,
getSimpleInput: () => readInput(__dirname, "simpleInput"),
getPuzzleInput: () => readInput(__dirname, "puzzleInput"),
solvePart1,
solvePart2,
};
39 changes: 39 additions & 0 deletions src/2018/11/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { chronalCharge } from "./index";

describe("2023/11", () => {
describe("part 1", () => {
it("should solve the simple input", async () => {
const input = await chronalCharge.getSimpleInput();

const output = chronalCharge.solvePart1(input);

expect(output).toBe("21,61");
});

it("should solve the puzzle input", async () => {
const input = await chronalCharge.getPuzzleInput();

const output = chronalCharge.solvePart1(input);

expect(output).toBe("20,43");
});
});

describe("part 2", () => {
it("should solve the simple input", async () => {
const input = await chronalCharge.getSimpleInput();

const output = chronalCharge.solvePart2(input);

expect(output).toBe("232,251,12");
});

it("should solve the puzzle input", async () => {
const input = await chronalCharge.getPuzzleInput();

const output = chronalCharge.solvePart2(input);

expect(output).toBe("233,271,13");
});
});
});
108 changes: 108 additions & 0 deletions src/2018/11/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Position } from "../../helpers/Position";
import { Day } from "../../helpers/types";

const calculatePowerLevel = (
{ x, y }: Position,
gridSerialNumber: number,
): number => {
const rackId = x + 1 + 10;
const power = (rackId * (y + 1) + gridSerialNumber) * rackId;
const hundredsValue = ((power % 1000) - (power % 100)) / 100;

return hundredsValue - 5;
};

const calculateMaxFixedGrid = (gridSerialNumber: number) => {
const fuelCells = Array.from({ length: 300 }, (_, y) =>
Array.from({ length: 300 }, (__, x) =>
calculatePowerLevel({ x, y }, gridSerialNumber),
),
);

const size = 3;
let maxPowerLevel = -Infinity;
let position: Position = { x: -1, y: -1 };

for (let y = 0; y <= fuelCells.length - size; y += 1) {
for (let x = 0; x <= fuelCells[y].length - size; x += 1) {
let powerLevel = 0;

for (let yDiff = 0; yDiff < size; yDiff += 1)
for (let xDiff = 0; xDiff < size; xDiff += 1)
powerLevel += fuelCells[y + yDiff][x + xDiff];

if (powerLevel > maxPowerLevel) {
position = { x, y };
maxPowerLevel = powerLevel;
}

maxPowerLevel = Math.max(maxPowerLevel, powerLevel);
}
}

return `${position.x + 1},${position.y + 1}`;
};

const calculateMaxVariableGrid = (gridSerialNumber: number) => {
const gridSize = 300;
const maxSquareSize = 20;
const sumsToRightAndBelow = Array.from({ length: gridSize }, (_, y) =>
Array.from({ length: gridSize }, (__, x) =>
calculatePowerLevel({ x, y }, gridSerialNumber),
),
);

for (let diff = gridSize - 2; diff >= 0; diff -= 1) {
sumsToRightAndBelow[diff][gridSize - 1] +=
sumsToRightAndBelow[diff + 1][gridSize - 1];
sumsToRightAndBelow[gridSize - 1][diff] +=
sumsToRightAndBelow[gridSize - 1][diff + 1];
}

for (let y = gridSize - 2; y >= 0; y -= 1)
for (let x = gridSize - 2; x >= 0; x -= 1)
sumsToRightAndBelow[y][x] +=
sumsToRightAndBelow[y + 1][x] +
sumsToRightAndBelow[y][x + 1] -
sumsToRightAndBelow[y + 1][x + 1];

let maxPowerLevel = -Infinity;
let bestPosition: Position = { x: -1, y: -1 };
let bestSize = -1;

for (let y = 0; y < gridSize; y += 1)
for (let x = 0; x < gridSize; x += 1)
for (
let size = 1;
size < Math.min(maxSquareSize, gridSize - x, gridSize - y);
size += 1
) {
const powerLevel =
sumsToRightAndBelow[y][x] -
sumsToRightAndBelow[y + size][x] -
sumsToRightAndBelow[y][x + size] +
sumsToRightAndBelow[y + size][x + size];

if (powerLevel > maxPowerLevel) {
bestPosition = { x, y };
maxPowerLevel = powerLevel;
bestSize = size;
}
}

return `${bestPosition.x + 1},${bestPosition.y + 1},${bestSize}`;
};

const solvePart1 = (gridSerialNumber: number) =>
calculateMaxFixedGrid(gridSerialNumber);
const solvePart2 = (gridSerialNumber: number) =>
calculateMaxVariableGrid(gridSerialNumber);

export const chronalCharge: Day<number, string> = {
day: 13,
year: 2018,
getSimpleInput: async () => 42,
getPuzzleInput: async () => 1309,
solvePart1,
solvePart2,
};
38 changes: 38 additions & 0 deletions src/2018/18/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { reservoirResearch } from "./index";

describe("2018/18", () => {
describe("part 1", () => {
it("should solve the simple input", async () => {
const input = await reservoirResearch.getSimpleInput();

const output = reservoirResearch.solvePart1(input);

expect(output).toBe(57);
});

it("should solve the puzzle input", async () => {
const input = await reservoirResearch.getPuzzleInput();

const output = reservoirResearch.solvePart1(input);

expect(output).toBe(39_367);
});
});

describe("part 2", () => {
it("should solve the simple input", async () => {
const input = await reservoirResearch.getSimpleInput();

const output = reservoirResearch.solvePart2(input);

expect(output).toBe(29);
});
it("should solve the puzzle input", async () => {
const input = await reservoirResearch.getPuzzleInput();

const output = reservoirResearch.solvePart2(input);

expect(output).toBe(33_061);
});
});
});
Loading

0 comments on commit 64e4d52

Please sign in to comment.