50 lines
No EOL
1.2 KiB
TypeScript
50 lines
No EOL
1.2 KiB
TypeScript
import { solvables } from "../main.ts";
|
|
|
|
import type { Day } from "../day.ts";
|
|
import { readInput } from "../utils.ts";
|
|
|
|
const checkWindow = (input: number): boolean => {
|
|
const width = String(input).length / 2
|
|
const str = String(input);
|
|
|
|
const leftPatIdx = [0, width]
|
|
const rightPatIdx = [width, width + width]
|
|
|
|
const left = str.slice(...leftPatIdx)
|
|
const right = str.slice(...rightPatIdx);
|
|
|
|
return left === right;
|
|
}
|
|
|
|
class DayImpl implements Day {
|
|
day = 2
|
|
input = readInput('02')
|
|
|
|
public part1(): Day {
|
|
const vals: [number, number][] = this.input
|
|
.split(",")
|
|
.map(range => range.trim().split("-")
|
|
.map(Number) as [number, number]);
|
|
|
|
const invalid: number[] = [];
|
|
|
|
for (const [start, end] of vals) {
|
|
for (let i = start; i <= end; i++) {
|
|
if (String(i).length % 2 === 1) continue
|
|
if (checkWindow(i)) {
|
|
invalid.push(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(invalid.reduce((a, b) => a + b, 0))
|
|
|
|
return this
|
|
}
|
|
|
|
public part2(): Day {
|
|
return this
|
|
}
|
|
}
|
|
|
|
solvables.push(new DayImpl()) |