Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
merrywhether committed Dec 4, 2024
1 parent d404106 commit da60af2
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/00/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEquals } from "@std/assert";
import { main } from "./main.ts";

Deno.test("correct something for sample", async () => {
Deno.test("correct placeholder for sample", async () => {
const result = await main("sample");
assertEquals(result.text, "");
});
15 changes: 6 additions & 9 deletions src/01/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// { dist: 3246517, sim: 29379307 }
// { distance: 3246517, similiarity: 29379307 }

export async function main(target: string) {
const dirpath = new URL(".", import.meta.url).pathname;
Expand All @@ -13,22 +13,19 @@ export async function main(target: string) {
return agg;
}, { left: [] as number[], right: [] as number[] });

data.left.sort();
data.right.sort();

const stats = data.right.reduce((agg, val) => {
const frequencies = data.right.sort().reduce((agg, val) => {
agg[val] ??= 0;
agg[val] += 1;
return agg;
}, [] as number[]);

return data.left.reduce(
return data.left.sort().reduce(
(agg, val, idx) => {
agg.dist = agg.dist + Math.abs(data.right[idx] - val);
agg.sim = agg.sim + val * (stats[val] ?? 0);
agg.distance += Math.abs(data.right[idx] - val);
agg.similiarity += val * (frequencies[val] ?? 0);
return agg;
},
{ dist: 0, sim: 0 },
{ distance: 0, similiarity: 0 },
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/01/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { main } from "./main.ts";

Deno.test("correct distance for sample", async () => {
const result = await main("sample");
assertEquals(result.dist, 11);
assertEquals(result.distance, 11);
});

Deno.test("correct similarity for sample", async () => {
const result = await main("sample");
assertEquals(result.sim, 31);
assertEquals(result.similiarity, 31);
});
24 changes: 10 additions & 14 deletions src/02/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// { safe: 660, dampSafe: 689 }
// { safeLines: 660, dampenedSafeLines: 689 }

export async function main(target = "input") {
const dirpath = new URL(".", import.meta.url).pathname;
Expand All @@ -19,39 +19,35 @@ export async function main(target = "input") {
});

if (isSafe) {
agg.safe++;
agg.dampSafe++;
agg.safeLines++;
agg.dampenedSafeLines++;
return agg;
}

const isDampSafe = levels.some((_level, i) => {
const isSafeWithDampening = levels.some((_level, i) => {
const test = Array.from(levels);
test.splice(i, 1);
const dampDirection = test[1] - test[0] > 0 ? 1 : -1;
const direction = test[1] - test[0] > 0 ? 1 : -1;

return test.every((level, i) => {
if (i === test.length - 1) {
return true;
}

const difference = test[i + 1] - level;
return difference * dampDirection >= 1 &&
difference * dampDirection <= 3;
return difference * direction >= 1 &&
difference * direction <= 3;
});
});

if (isDampSafe) {
agg.dampSafe++;
if (isSafeWithDampening) {
agg.dampenedSafeLines++;
return agg;
}

console.log(line);
}

console.log("---");

return agg;
}, { safe: 0, dampSafe: 0 });
}, { safeLines: 0, dampenedSafeLines: 0 });
}

if (import.meta.main) {
Expand Down
6 changes: 3 additions & 3 deletions src/02/test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { assertEquals } from "@std/assert";
import { main } from "./main.ts";

Deno.test("correct strict safe count for sample", async () => {
Deno.test("correct safe count for sample", async () => {
const result = await main("sample");
assertEquals(result.safe, 2);
assertEquals(result.safeLines, 2);
});

Deno.test("correct dampended safe count for sample", async () => {
const result = await main("sample");
assertEquals(result.dampSafe, 4);
assertEquals(result.dampenedSafeLines, 4);
});
44 changes: 24 additions & 20 deletions src/03/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// { mulQuo: 163931492, doQuo: 76911921 }
// { doQuotientSum: 76911921, quotientSum: 163931492 }

// fun with eval()!
// fun with eval()! (this signature is found in the input so we use it directly instead of parsing and reconstructing)
// deno-lint-ignore no-unused-vars
function mul(a: number, b: number) {
return a * b;
}
Expand All @@ -9,25 +10,28 @@ export async function main(target = "input") {
const dirpath = new URL(".", import.meta.url).pathname;
const text = await Deno.readTextFile(`${dirpath}${target}.txt`);

const mulMatches = text.matchAll(/mul\(\d{1,3},\d{1,3}\)/g);
const { doQuotientSum, otherQuotientSum } = text.matchAll(
/(mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\))/g,
).reduce(
(agg, match) => {
if (match[0] === "do()") {
agg.do = true;
} else if (match[0] === `don't()`) {
agg.do = false;
} else if (agg.do) {
agg.doQuotientSum += eval(match[0]);
} else {
agg.otherQuotientSum += eval(match[0]);
}
return agg;
},
{ doQuotientSum: 0, do: true, otherQuotientSum: 0 },
);

const mulQuo = mulMatches.reduce((agg, match) => {
return agg + eval(match[0]);
}, 0);

const doMatches = text.matchAll(/(mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\))/g);
const { doQuo } = doMatches.reduce((agg, match) => {
if (match[0] === "do()") {
agg.do = true;
} else if (match[0] === `don't()`) {
agg.do = false;
} else if (agg.do) {
agg.doQuo += eval(match[0]);
}
return agg;
}, { doQuo: 0, do: true });

return { mulQuo, doQuo };
return {
doQuotientSum,
quotientSum: doQuotientSum + otherQuotientSum,
};
}

if (import.meta.main) {
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions src/03/test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { assertEquals } from "@std/assert";
import { main } from "./main.ts";

Deno.test("correct quotient for sample", async () => {
Deno.test("correct quotient sum for sample", async () => {
const result = await main("sample");
assertEquals(result.mulQuo, 161);
assertEquals(result.quotientSum, 161);
});

Deno.test("correct quotient for sample2", async () => {
const result = await main("sample2");
assertEquals(result.doQuo, 48);
Deno.test("correct quotient sum for sample with do", async () => {
const result = await main("sample-do");
assertEquals(result.doQuotientSum, 48);
});
22 changes: 11 additions & 11 deletions src/04/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export async function main(target = "input") {
line.split("").filter((l) => l.length > 0)
);

return data.reduce((acc, line, lineIdx) => {
const { wordCount, xCount } = line.reduce((lineAcc, char, charIdx) => {
return data.reduce((agg, line, lineIdx) => {
const { wordCount, xCount } = line.reduce((lineAgg, char, charIdx) => {
wordTargets.forEach((target) => {
if (char === target[0]) {
// across
Expand All @@ -32,7 +32,7 @@ export async function main(target = "input") {
data[lineIdx][charIdx + 2] === target[2] &&
data[lineIdx][charIdx + 3] === target[3]
) {
lineAcc.wordCount++;
lineAgg.wordCount++;
}

// down
Expand All @@ -41,7 +41,7 @@ export async function main(target = "input") {
data[lineIdx + 2][charIdx] === target[2] &&
data[lineIdx + 3][charIdx] === target[3]
) {
lineAcc.wordCount++;
lineAgg.wordCount++;
}

// diagonal down right
Expand All @@ -50,7 +50,7 @@ export async function main(target = "input") {
data[lineIdx + 2][charIdx + 2] === target[2] &&
data[lineIdx + 3][charIdx + 3] === target[3]
) {
lineAcc.wordCount++;
lineAgg.wordCount++;
}

// diagonal down left
Expand All @@ -59,7 +59,7 @@ export async function main(target = "input") {
data[lineIdx + 2][charIdx - 2] === target[2] &&
data[lineIdx + 3][charIdx - 3] === target[3]
) {
lineAcc.wordCount++;
lineAgg.wordCount++;
}
}
});
Expand All @@ -78,18 +78,18 @@ export async function main(target = "input") {
data[lineIdx][charIdx + 2] === target[2] &&
data[lineIdx + 2][charIdx] === target[0]
) {
lineAcc.xCount++;
lineAgg.xCount++;
}
}
}
});

return lineAcc;
return lineAgg;
}, { wordCount: 0, xCount: 0 });

acc.wordCount += wordCount;
acc.xCount += xCount;
return acc;
agg.wordCount += wordCount;
agg.xCount += xCount;
return agg;
}, { wordCount: 0, xCount: 0 });
}

Expand Down

0 comments on commit da60af2

Please sign in to comment.