Skip to content

Commit

Permalink
chore: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 3, 2023
1 parent dce1cae commit 2a52656
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 59 deletions.
50 changes: 15 additions & 35 deletions solutions/typescript/2023/03/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,38 @@
/* 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 => {
const m = input.toMatrix();
const numbers: number[] = [];
let num: string | undefined = undefined;
let lastVecs: Vec2[] = [];
let number: string | undefined = undefined;
let numberPositions: 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 }));
number = number === undefined ? v : number + v;
numberPositions.push(new Vec2({ x, y }));
} else if (
num !== undefined &&
lastVecs.some((lv) =>
number !== undefined &&
numberPositions.some((numberPosition) =>
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 === '@')
);
.map((direction) => direction.add(numberPosition))
.some((neighbour) => {
const neighbourValue = m[neighbour.y]?.[neighbour.x];
return neighbourValue && !/(\.|\d)/.test(neighbourValue);
}),
)
) {
//TODO check surr
numbers.push(num.toInt());
// console.log('FOUND', num);
num = undefined;
lastVecs = [];
numbers.push(number.toInt());
number = undefined;
numberPositions = [];
} else {
num = undefined;
lastVecs = [];
number = undefined;
numberPositions = [];
}
}
}
Expand Down
41 changes: 17 additions & 24 deletions solutions/typescript/2023/03/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* 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 => {
const m = input.toMatrix();
const numbers: number[] = [];
let num: string | undefined = undefined;
let lastVecs: Vec2[] = [];
let number: string | undefined = undefined;
let numberPositions: Vec2[] = [];

const gears = new Map<string, number[]>();
for (let y = 0; y < m.length; y++) {
Expand All @@ -16,44 +14,39 @@ export const p2 = (input: string): number => {
for (let x = 0; x < row.length; x++) {
const v = row[x]!;

const gearVec = lastVecs
const gearVector = numberPositions
.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());
number = number === undefined ? v : number + v;
numberPositions.push(new Vec2({ x, y }));
} else if (number !== undefined && gearVector) {
if (gears.has(gearVector.toString())) {
gears.get(gearVector.toString())?.push(number.toInt());
} else {
gears.set(gearVec.toString(), [num.toInt()]);
gears.set(gearVector.toString(), [number.toInt()]);
}
num = undefined;
lastVecs = [];
number = undefined;
numberPositions = [];
} else {
num = undefined;
lastVecs = [];
number = undefined;
numberPositions = [];
}
}
}

return [...gears.values()]
.filter((gp) => gp.length === 2)
.map((gp) => gp.product())
return gears
.valueArray()
.filter((gearValues) => gearValues.length === 2)
.map((gearValues) => gearValues.product())
.sum();
};

Expand Down

0 comments on commit 2a52656

Please sign in to comment.