2022 - Day 3
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
const sample1 = await Deno.readTextFile("sample1.txt");
|
||||
const sample2 = await Deno.readTextFile("sample2.txt");
|
||||
const input = await Deno.readTextFile("input.txt");
|
||||
|
||||
const solvePart1 = (data: string): number =>
|
||||
data
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) => [
|
||||
new Set(line.substring(0, line.length / 2).split("")),
|
||||
new Set(line.substring((line.length - 1) / 2 + 1, line.length).split("")),
|
||||
])
|
||||
.map(([set1, set2]) => [...set1].filter((v) => set2.has(v)).pop())
|
||||
.map((c) => c?.charCodeAt(0)!)
|
||||
.map((c) => c - (c > 96 ? 96 : 64 - 26))
|
||||
.reduce((sum, value) => sum + value, 0);
|
||||
|
||||
console.log("Sample1:", solvePart1(sample1));
|
||||
console.log("Input", solvePart1(input));
|
||||
|
||||
const solvePart2 = (data: string): number =>
|
||||
data
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) => new Set(line.split("")))
|
||||
.reduce((result, set, index) => {
|
||||
const resultIndex = Math.floor(index / 3);
|
||||
result[resultIndex] = [...(result[resultIndex] || []), set];
|
||||
return result;
|
||||
}, [] as Set<string>[][])
|
||||
.map(([set1, set2, set3]) =>
|
||||
[...set1].filter((v) => set2.has(v) && set3.has(v)).pop()
|
||||
)
|
||||
.map((c) => c?.charCodeAt(0)!)
|
||||
.map((c) => c - (c > 96 ? 96 : 64 - 26))
|
||||
.reduce((sum, value) => sum + value, 0);
|
||||
|
||||
console.log("Sample:", solvePart2(sample2));
|
||||
console.log("Input", solvePart2(input));
|
||||
Reference in New Issue
Block a user