2022 - Day 4
This commit is contained in:
42
2022/04/solution.ts
Normal file
42
2022/04/solution.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
const sample = await Deno.readTextFile("sample.txt");
|
||||
const input = await Deno.readTextFile("input.txt");
|
||||
|
||||
const solvePart1 = (data: string): number =>
|
||||
data
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) =>
|
||||
line
|
||||
.split(",")
|
||||
.map((section) =>
|
||||
section.split("-").map((value) => parseInt(value, 10))
|
||||
)
|
||||
)
|
||||
.filter(
|
||||
([section1, section2]) =>
|
||||
(section1[0] <= section2[0] && section1[1] >= section2[1]) ||
|
||||
(section2[0] <= section1[0] && section2[1] >= section1[1])
|
||||
).length;
|
||||
|
||||
console.log("Sample:", solvePart1(sample));
|
||||
console.log("Input", solvePart1(input));
|
||||
|
||||
const solvePart2 = (data: string): number =>
|
||||
data
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) =>
|
||||
line
|
||||
.split(",")
|
||||
.map((section) =>
|
||||
section.split("-").map((value) => parseInt(value, 10))
|
||||
)
|
||||
)
|
||||
.filter(
|
||||
([section1, section2]) =>
|
||||
(section1[0] <= section2[1] && section1[1] >= section2[0]) ||
|
||||
(section2[0] <= section1[1] && section2[1] >= section1[0])
|
||||
).length;
|
||||
|
||||
console.log("Sample:", solvePart2(sample));
|
||||
console.log("Input", solvePart2(input));
|
||||
Reference in New Issue
Block a user