day 4 done

This commit is contained in:
David 2024-12-04 22:18:24 -05:00
parent ac3cf84618
commit 06f3e2411d
8 changed files with 182 additions and 16 deletions

View File

@ -1,7 +1,11 @@
import fs from "node:fs";
import { Temporal } from "@js-temporal/polyfill";
import { printTime } from "../util";
const data = fs.readFileSync("./inputs/input", "utf8");
let timer = Temporal.Now.instant();
console.log(
"part one:",
data
@ -45,9 +49,12 @@ console.log(
}
}
return total;
}, 0)
}, 0),
printTime(Temporal.Now.instant().since(timer))
);
timer = Temporal.Now.instant();
console.log(
"part two:",
data
@ -124,5 +131,6 @@ console.log(
return { ...total, on: state.on };
},
{ sum: 0, on: true }
).sum
).sum,
printTime(Temporal.Now.instant().since(timer))
);

103
04/index.ts Normal file
View File

@ -0,0 +1,103 @@
import fs from "node:fs";
import { printTime, now } from "../util";
const test = false;
const data = fs.readFileSync(
test ? "./inputs/testinput" : "./inputs/input",
"utf8"
);
let timer = now.instant();
console.log(
"part one:",
data
.split("\n")
.slice(0, -1)
.reduce((coll, curr, index, array) => {
// 2D string matching is a pain. Let's turn it into a 1D problem!
// create the down rows
curr.split("").forEach((s, i) => {
coll[i] = coll[i] ? coll[i] + s : s;
});
// create the down-left-diagonals
curr.split("").forEach((s, i) => {
coll[i + array.length + index] = coll[i + array.length + index]
? coll[i + array.length + index] + s
: s;
});
// create the down-right-diagonals
curr.split("").forEach((s, i) => {
coll[array.length * 4 + index - i - 2] = coll[
array.length * 4 + index - i - 2
]
? coll[array.length * 4 + index - i - 2] + s
: s;
});
// include the original crosses
return [...coll, curr];
}, Array.from({ length: test ? 10 + (10 * 4 - 2) : 140 + 140 * 4 - 2 }) as string[])
.reduce((total, curr) => {
// and now just do some regexes over the set of 1D strings
let matchesForward = curr.match(/XMAS/g);
let matchesBackward = curr.match(/SAMX/g);
if (matchesForward) {
total += matchesForward.length;
}
if (matchesBackward) {
total += matchesBackward.length;
}
return total;
}, 0),
printTime(now.instant().since(timer))
);
timer = now.instant();
console.log(
"part two:",
data
.split("\n")
.slice(0, -1)
.reduce((coll, curr, index, array) => {
if (index === 0 || index === array.length - 1) {
// we can entirely skip the first and last rows, since there's no way to get a cross started
return coll;
}
// look for A's
curr.split("").forEach((s, i, a) => {
if (i === 0 || i === a.length - 1) {
// same as above, edges are useless
return;
}
if (s === "A") {
// 1 2
// A
// 3 4
coll = [
...coll,
array[index - 1][i - 1] +
array[index - 1][i + 1] +
array[index + 1][i - 1] +
array[index + 1][i + 1],
];
}
});
return coll;
}, [] as string[])
.reduce((coll, curr) => {
// we could have just done this check above,
// but it's a little more clear, although slower,
// to iterate again.
switch (curr) {
case "MSMS":
case "SSMM":
case "SMSM":
case "MMSS":
coll += 1;
default:
break;
}
return coll;
}, 0),
printTime(now.instant().since(timer))
);

View File

@ -1,13 +0,0 @@
import fs from "node:fs";
const data = fs.readFileSync("./inputs/input", "utf8");
console.log(
"part one:",
data.split("\n").slice(0, -1) // remove empty trailing newline
);
console.log(
"part two:",
data.split("\n").slice(0, -1) // remove empty trailing newline
);

18
index.ts.tmpl Normal file
View File

@ -0,0 +1,18 @@
import fs from "node:fs";
import { printTime, now } from "../util";
const data = fs.readFileSync("./inputs/input", "utf8");
let timer = now.instant();
console.log(
"part one:",
data.split("\n").slice(0, -1),
printTime(now.instant().since(timer))
);
timer = now.instant();
console.log(
"part two:",
data.split("\n").slice(0, -1),
printTime(now.instant().since(timer))
);

28
package-lock.json generated
View File

@ -4,10 +4,26 @@
"requires": true,
"packages": {
"": {
"dependencies": {
"@js-temporal/polyfill": "^0.4.4"
},
"devDependencies": {
"@types/node": "^22.10.1"
}
},
"node_modules/@js-temporal/polyfill": {
"version": "0.4.4",
"resolved": "https://registry.npmjs.org/@js-temporal/polyfill/-/polyfill-0.4.4.tgz",
"integrity": "sha512-2X6bvghJ/JAoZO52lbgyAPFj8uCflhTo2g7nkFzEQdXd/D8rEeD4HtmTEpmtGCva260fcd66YNXBOYdnmHqSOg==",
"license": "ISC",
"dependencies": {
"jsbi": "^4.3.0",
"tslib": "^2.4.1"
},
"engines": {
"node": ">=12"
}
},
"node_modules/@types/node": {
"version": "22.10.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.10.1.tgz",
@ -18,6 +34,18 @@
"undici-types": "~6.20.0"
}
},
"node_modules/jsbi": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/jsbi/-/jsbi-4.3.0.tgz",
"integrity": "sha512-SnZNcinB4RIcnEyZqFPdGPVgrg2AcnykiBy0sHVJQKHYeaLUvi3Exj+iaPpLnFVkDPZIV4U0yvgC9/R4uEAZ9g==",
"license": "Apache-2.0"
},
"node_modules/tslib": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
"license": "0BSD"
},
"node_modules/undici-types": {
"version": "6.20.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz",

View File

@ -1,5 +1,8 @@
{
"devDependencies": {
"@types/node": "^22.10.1"
},
"dependencies": {
"@js-temporal/polyfill": "^0.4.4"
}
}

View File

@ -16,6 +16,6 @@ if [ ! -f "${padded}/inputs/input" ]; then
-H "Cookie: session=`cat .cookie`" https://adventofcode.com/${__year}/day/${__day##0}/input > "${padded}/inputs/input"
fi
if [ ! -f "${padded}/index.ts" ]; then
cp index.tmpl.ts ${padded}/index.ts
cp index.ts.tmpl ${padded}/index.ts
fi
open "https://adventofcode.com/${__year}/day/${__day##0}"

19
util.ts Normal file
View File

@ -0,0 +1,19 @@
import { Temporal } from "@js-temporal/polyfill";
export const now = Temporal.Now;
export const printTime = (duration: Temporal.Duration): string => {
const units = [
{ unit: "m", value: duration.minutes },
{ unit: "s", value: duration.seconds },
{ unit: "ms", value: duration.milliseconds },
{ unit: "µs", value: duration.microseconds },
{ unit: "ns", value: duration.nanoseconds },
];
const parts = units
.filter((u) => u.value !== 0)
.map((u) => `${u.value} ${u.unit}`);
return parts.join(", ");
};