-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial solution for 2023 day 3
- Loading branch information
Showing
9 changed files
with
270 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"schemaVersion": 1, | ||
"label": "Advent of TypeScript 2023", | ||
"message": "2/25", | ||
"message": "3/25", | ||
"color": "orange" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
467..114.. | ||
...*...... | ||
..35..633. | ||
......#... | ||
617*...... | ||
.....+.58. | ||
..592..... | ||
......755. | ||
...$.*.... | ||
.664.598.. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,63 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
/* eslint-disable @typescript-eslint/prefer-for-of */ | ||
import { Direction, Vec2, task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
|
||
export const p1 = (input: string): number => { | ||
return input.lines().length; | ||
const m = input.toMatrix(); | ||
const numbers: number[] = []; | ||
let num: string | undefined = undefined; | ||
let lastVecs: Vec2[] = []; | ||
|
||
for (let y = 0; y < m.length; y++) { | ||
const row = m[y]!; | ||
|
||
for (let x = 0; x < row.length; x++) { | ||
const v = row[x]!; | ||
|
||
if (/\d/.test(v)) { | ||
num = num === undefined ? v : num + v; | ||
lastVecs.push(new Vec2({ x, y: y })); | ||
} else if ( | ||
num !== undefined && | ||
lastVecs.some((lv) => | ||
Direction.allDirections | ||
.map((d) => d.add(lv)) | ||
.some((v) => { | ||
// const cv = new Vec2(x, i); | ||
const n = m[v.y]?.[v.x]; | ||
if (n) { | ||
// console.log(num, n, v.toString()); | ||
} | ||
return ( | ||
n && | ||
(n === '#' || | ||
n === '$' || | ||
n === '%' || | ||
n === '&' || | ||
n === '*' || | ||
n === '+' || | ||
n === '/' || | ||
n === '=' || | ||
n === '-' || | ||
n === '@') | ||
); | ||
}), | ||
) | ||
) { | ||
//TODO check surr | ||
numbers.push(num.toInt()); | ||
// console.log('FOUND', num); | ||
num = undefined; | ||
lastVecs = []; | ||
} else { | ||
num = undefined; | ||
lastVecs = []; | ||
} | ||
} | ||
} | ||
|
||
return numbers.sum(); | ||
}; | ||
|
||
await task(p1, packageJson.aoc); // 2149 ~0.22ms | ||
await task(p1, packageJson.aoc); // 539637 ~1.39ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,60 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
/* eslint-disable @typescript-eslint/prefer-for-of */ | ||
import { Direction, Vec2, task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
|
||
export const p2 = (input: string): number => { | ||
return input.lines().length; | ||
const m = input.toMatrix(); | ||
const numbers: number[] = []; | ||
let num: string | undefined = undefined; | ||
let lastVecs: Vec2[] = []; | ||
|
||
const gears = new Map<string, number[]>(); | ||
for (let y = 0; y < m.length; y++) { | ||
const row = m[y]!; | ||
|
||
for (let x = 0; x < row.length; x++) { | ||
const v = row[x]!; | ||
|
||
const gearVec = lastVecs | ||
.map((lv) => | ||
Direction.allDirections | ||
.map((d) => d.add(lv)) | ||
.find((v) => { | ||
// const cv = new Vec2(x, i); | ||
const n = m[v.y]?.[v.x]; | ||
if (n) { | ||
// console.log(num, n, v.toString()); | ||
} | ||
return n && n === '*'; | ||
}), | ||
) | ||
.find((v) => !!v); | ||
|
||
if (/\d/.test(v)) { | ||
num = num === undefined ? v : num + v; | ||
lastVecs.push(new Vec2({ x, y: y })); | ||
} else if (num !== undefined && gearVec) { | ||
//TODO check surr | ||
numbers.push(num.toInt()); | ||
if (gears.has(gearVec.toString())) { | ||
gears.get(gearVec.toString())?.push(num.toInt()); | ||
} else { | ||
gears.set(gearVec.toString(), [num.toInt()]); | ||
} | ||
num = undefined; | ||
lastVecs = []; | ||
} else { | ||
num = undefined; | ||
lastVecs = []; | ||
} | ||
} | ||
} | ||
|
||
return [...gears.values()] | ||
.filter((gp) => gp.length === 2) | ||
.map((gp) => gp.product()) | ||
.sum(); | ||
}; | ||
|
||
await task(p2, packageJson.aoc); // 0 ~0.22ms | ||
await task(p2, packageJson.aoc); // 82818007 ~3.89ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters