impl day 2 + simplification, woops implemented the wrong pattern checker
This commit is contained in:
parent
f33dda8916
commit
4fba52d596
4 changed files with 50 additions and 10 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,4 +1,5 @@
|
|||
.DS_Store
|
||||
.session
|
||||
.sessioncookie
|
||||
.tool_versions
|
||||
input
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
day=$(date +%-e)
|
||||
day_zero=$(date +%0e)
|
||||
year=$(date +%Y)
|
||||
session=$(cat .sessioncookie)
|
||||
mkdir -p input
|
||||
curl -b "session=${session}" -o "./input/${day_zero}.txt" "https://adventofcode.com/2024/day/${day}/input"
|
||||
curl -b "session=${session}" -o "./input/${day_zero}.txt" "https://adventofcode.com/${year}/day/${day}/input"
|
||||
4
main.ts
4
main.ts
|
|
@ -13,7 +13,9 @@ const solvables: Solvable[] = [];
|
|||
|
||||
// only solve the latest day
|
||||
console.log("Solving latest day only:")
|
||||
const day = solvables.toReversed()[0];
|
||||
const day = solvables[0];
|
||||
console.log(day)
|
||||
|
||||
day.part1()
|
||||
day.part2()
|
||||
})()
|
||||
|
|
|
|||
|
|
@ -3,20 +3,56 @@ import { solvables } from "../main.ts";
|
|||
import type { Solvable } from "../solvable.ts";
|
||||
import { readInput } from "../utils.ts";
|
||||
|
||||
class DayOne implements Solvable {
|
||||
input = readInput('01')
|
||||
|
||||
public part1(): DayOne {
|
||||
const vals: [string, number][] = this.input
|
||||
.split("\n")
|
||||
.map(line => [line[0], Number.parseInt(line.slice(1))]);
|
||||
// check if pattern with given width repeats in input
|
||||
const checkWindow = (width: number, input: number): boolean => {
|
||||
const str = String(input);
|
||||
for (let patternStart = 0; patternStart + 2 * width < str.length; patternStart += width) {
|
||||
const leftPatIdx = [patternStart, patternStart + width]
|
||||
const rightPatIdx = [patternStart + width, patternStart + width + width]
|
||||
const patternRepeated = str.slice(...leftPatIdx) === str.slice(...rightPatIdx);
|
||||
if (patternRepeated) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
class DayTwo implements Solvable {
|
||||
input = readInput('02')
|
||||
|
||||
public part1(): DayTwo {
|
||||
const vals: [number, number][] = this.input
|
||||
.split(",")
|
||||
.map(range => range.split("-")
|
||||
.map(Number) as [number, number]);
|
||||
|
||||
const invalid: number[] = [];
|
||||
|
||||
for (const [start, end] of vals) {
|
||||
for (let i = start; i <= end; i++) {
|
||||
// if the number is of odd length, it can't have a repeating pattern
|
||||
if (String(i).length % 2 !== 0) {
|
||||
continue;
|
||||
}
|
||||
// check for patterns with increasing width but never exceeding half the length of the number
|
||||
for (let width = 2; width <= String(i).length / 2; width++) {
|
||||
if (checkWindow(width, i)) {
|
||||
invalid.push(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(invalid.reduce((a, b) => a + b, 0))
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
public part2(): DayOne {
|
||||
public part2(): DayTwo {
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
solvables.push(new DayOne())
|
||||
solvables.push(new DayTwo())
|
||||
Loading…
Add table
Add a link
Reference in a new issue