generated from CForrest97/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4654fc0
commit 64e4d52
Showing
11 changed files
with
2,635 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.