clean up the code a little more
This commit is contained in:
parent
172adce0ba
commit
9e99375349
38
04/index.ts
38
04/index.ts
@ -14,31 +14,30 @@ console.log(
|
|||||||
data
|
data
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.slice(0, -1)
|
.slice(0, -1)
|
||||||
.reduce((coll, curr, index, array) => {
|
.reduce(
|
||||||
|
(coll, curr, index, array) => {
|
||||||
// 2D string matching is a pain. Let's turn it into a 1D problem!
|
// 2D string matching is a pain. Let's turn it into a 1D problem!
|
||||||
// create the columns
|
// create the columns
|
||||||
curr.split("").forEach((s, i) => {
|
curr.split("").forEach((s, i) => {
|
||||||
coll[i] = coll[i] ? coll[i] + s : s;
|
coll[i] += s;
|
||||||
});
|
});
|
||||||
// create the down-left-diagonals
|
// create the down-left-diagonals
|
||||||
curr.split("").forEach((s, i) => {
|
curr.split("").forEach((s, i) => {
|
||||||
coll[i + array.length + index] = coll[i + array.length + index]
|
coll[i + array.length + index] += s;
|
||||||
? coll[i + array.length + index] + s
|
|
||||||
: s;
|
|
||||||
});
|
});
|
||||||
// create the down-right-diagonals
|
// create the down-right-diagonals
|
||||||
curr.split("").forEach((s, i) => {
|
curr.split("").forEach((s, i) => {
|
||||||
coll[array.length * 4 + index - i - 2] = coll[
|
coll[array.length * 4 + index - i - 2] += s;
|
||||||
array.length * 4 + index - i - 2
|
|
||||||
]
|
|
||||||
? coll[array.length * 4 + index - i - 2] + s
|
|
||||||
: s;
|
|
||||||
});
|
});
|
||||||
// include the original row
|
// include the original row
|
||||||
return [...coll, curr];
|
return [...coll, curr];
|
||||||
}, Array.from({ length: test ? 10 + (10 * 4 - 2) : 140 + 140 * 4 - 2 }) as string[])
|
},
|
||||||
|
// pre-populate the array so everything's aligned
|
||||||
|
Array.from({ length: test ? 10 * 5 - 2 : 140 * 5 - 2 }, () => "")
|
||||||
|
)
|
||||||
.reduce((total, curr) => {
|
.reduce((total, curr) => {
|
||||||
// and now just do some regexes over the set of 1D strings
|
// and now just do some regexes over the set of 1D strings
|
||||||
|
// can't do /XMAS|SMAX/ because overlaps like XMASAMX are allowed!
|
||||||
let matchesForward = curr.match(/XMAS/g);
|
let matchesForward = curr.match(/XMAS/g);
|
||||||
let matchesBackward = curr.match(/SAMX/g);
|
let matchesBackward = curr.match(/SAMX/g);
|
||||||
if (matchesForward) {
|
if (matchesForward) {
|
||||||
@ -64,25 +63,28 @@ console.log(
|
|||||||
return coll;
|
return coll;
|
||||||
}
|
}
|
||||||
// look for A's
|
// look for A's
|
||||||
curr.split("").forEach((s, i, a) => {
|
return [
|
||||||
|
...coll,
|
||||||
|
...curr.split("").reduce((c, s, i, a) => {
|
||||||
if (i === 0 || i === a.length - 1) {
|
if (i === 0 || i === a.length - 1) {
|
||||||
// same as above, edges are useless
|
// same as above, edges are useless
|
||||||
return;
|
return c;
|
||||||
}
|
}
|
||||||
if (s === "A") {
|
|
||||||
// 1 2
|
// 1 2
|
||||||
// A
|
// A
|
||||||
// 3 4
|
// 3 4
|
||||||
coll = [
|
if (s === "A") {
|
||||||
...coll,
|
return [
|
||||||
|
...c,
|
||||||
array[index - 1][i - 1] +
|
array[index - 1][i - 1] +
|
||||||
array[index - 1][i + 1] +
|
array[index - 1][i + 1] +
|
||||||
array[index + 1][i - 1] +
|
array[index + 1][i - 1] +
|
||||||
array[index + 1][i + 1],
|
array[index + 1][i + 1],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
});
|
return c;
|
||||||
return coll;
|
}, [] as string[]),
|
||||||
|
];
|
||||||
}, [] as string[])
|
}, [] as string[])
|
||||||
.reduce((coll, curr) => {
|
.reduce((coll, curr) => {
|
||||||
// we could have just done this check above,
|
// we could have just done this check above,
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import { printTime, now } from "../util";
|
import { printTime, now } from "../util";
|
||||||
|
|
||||||
const data = fs.readFileSync("./inputs/input", "utf8");
|
const test = false;
|
||||||
|
|
||||||
|
const data = fs.readFileSync(
|
||||||
|
test ? "./inputs/testinput" : "./inputs/input",
|
||||||
|
"utf8"
|
||||||
|
);
|
||||||
|
|
||||||
let timer = now.instant();
|
let timer = now.instant();
|
||||||
console.log(
|
console.log(
|
||||||
|
Loading…
Reference in New Issue
Block a user