Skip to content

Commit

Permalink
2024 day 1 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehw committed Dec 1, 2024
1 parent 86e9032 commit 4b403c9
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
deno-version: v2.x

- name: Run tests
run: deno test --allow-env --allow-read=. --allow-write $GITHUB_WORKSPACE/advent-of-code/test.ts
run: deno test --allow-env --allow-read=. --allow-write $GITHUB_WORKSPACE/advent-of-code/test.ts
2 changes: 1 addition & 1 deletion 2023/day-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "@std/assert";
const puzzle: Day = {
year: 2023,
day: 1,
part1: (input: string): string => {
part1: (input) => {
let sum = 0;
for (const line of input.split("\n")) {
const digits = line.replaceAll(/[^0-9]/g, "");
Expand Down
63 changes: 63 additions & 0 deletions 2024/day-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Day, Solve } from "@mikehw/advent-of-code";
import { assert, assertEquals } from "@std/assert";

const puzzle: Day = {
year: 2024,
day: 1,
part1: (input) => {
const left: number[] = [];
const right: number[] = [];
for (const line of input.split("\n")) {
const pieces = line.split(/\s+/g);
assert(pieces.length === 2, `Failed to split "${line}", got ${pieces}`);
left.push(parseInt(pieces[0]));
right.push(parseInt(pieces[1]));
}
left.sort();
right.sort();
let distanceSum = 0;
for (let i = 0; i < left.length; i++) {
distanceSum += Math.abs(left[i] - right[i]);
}
return distanceSum;
},
part2: (input) => {
const left: number[] = [];
const right: number[] = [];
for (const line of input.split("\n")) {
const pieces = line.split(/\s+/g);
assert(pieces.length === 2, `Failed to split "${line}", got ${pieces}`);
left.push(parseInt(pieces[0]));
right.push(parseInt(pieces[1]));
}
let sum = 0;
for (const v of left) {
sum += v * (right.filter((r) => (r === v)).length);
}
return sum;
},
};

if (import.meta.main) {
await Solve(puzzle);
}

Deno.test("2024/day-1/part-1", async () => {
const input = `3 4
4 3
2 5
1 3
3 9
3 3`;
assertEquals(await puzzle.part1(input), 11);
});

Deno.test("2024/day-1/part-2", async () => {
const input = `3 4
4 3
2 5
1 3
3 9
3 3`;
assertEquals(await puzzle.part2?.(input), 31);
});
4 changes: 2 additions & 2 deletions advent-of-code/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { getInputForDay } from "./utils.ts";
export interface Day {
/** Year of the challenge */
year: number;

/** Day of the challenge */
day: number;

/** Part 1 Solution */
// deno-lint-ignore no-explicit-any
part1: (input: string) => Promise<any> | any;
Expand Down
24 changes: 12 additions & 12 deletions advent-of-code/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Deno.test("getInputForDay", async (t) => {
Error,
"ADVENT_OF_CODE_SESSION_COOKIE enviornment variable is required",
);
cleanup(originalEnv)
cleanup(originalEnv);
});

await t.step("should fetch input from cache if exists", async () => {
Expand All @@ -31,7 +31,7 @@ Deno.test("getInputForDay", async (t) => {
statStub.restore();
readStub.restore();
}
cleanup(originalEnv)
cleanup(originalEnv);
});

await t.step("should fetch input from web and cache", async () => {
Expand All @@ -46,7 +46,7 @@ Deno.test("getInputForDay", async (t) => {
} as Response));

const statStub = stub(Deno, "stat", () => {
throw new Deno.errors.NotFound;
throw new Deno.errors.NotFound();
});

// Spy on Deno.mkdir and Deno.writeTextFile
Expand All @@ -70,22 +70,22 @@ Deno.test("getInputForDay", async (t) => {
);
assertEquals(writeFileSpy.calls[0].args[1], "web fetched input");

Deno.remove(join(Deno.cwd(), ".inputs", "2023_1.txt"))
Deno.remove(join(Deno.cwd(), ".inputs"))
Deno.remove(join(Deno.cwd(), ".inputs", "2023_1.txt"));
Deno.remove(join(Deno.cwd(), ".inputs"));
} finally {
fetchStub.restore();
statStub.restore();
mkdirSpy.restore();
writeFileSpy.restore();
}
cleanup(originalEnv)
cleanup(originalEnv);
});
});

function cleanup(originalEnv: string | undefined) {
if (originalEnv) {
Deno.env.set("ADVENT_OF_CODE_SESSION_COOKIE", originalEnv);
} else {
Deno.env.delete("ADVENT_OF_CODE_SESSION_COOKIE");
}
}
if (originalEnv) {
Deno.env.set("ADVENT_OF_CODE_SESSION_COOKIE", originalEnv);
} else {
Deno.env.delete("ADVENT_OF_CODE_SESSION_COOKIE");
}
}
4 changes: 3 additions & 1 deletion advent-of-code/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export async function getInputForDay(
}
const session = Deno.env.get("ADVENT_OF_CODE_SESSION_COOKIE");
if (!session) {
throw new Error("ADVENT_OF_CODE_SESSION_COOKIE enviornment variable is required")
throw new Error(
"ADVENT_OF_CODE_SESSION_COOKIE enviornment variable is required",
);
}
const headers = new Headers({
Cookie: `session=${session}`,
Expand Down

0 comments on commit 4b403c9

Please sign in to comment.