day one
This commit is contained in:
parent
82fb017d03
commit
be0f9029a4
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.cookie
|
||||
node_modules
|
||||
|
||||
*/inputs
|
59
01/index.ts
Normal file
59
01/index.ts
Normal file
@ -0,0 +1,59 @@
|
||||
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
|
||||
.map(
|
||||
(v) => v.split(" ").filter((c) => c) // abusing js string nullity to remove empty members
|
||||
)
|
||||
.reduce(
|
||||
(result, current) => {
|
||||
// build two arrays, one of each column
|
||||
result[0].push(parseInt(current[0], 10));
|
||||
result[1].push(parseInt(current[1], 10));
|
||||
return result;
|
||||
},
|
||||
[[], []] as Array<Array<number>>
|
||||
)
|
||||
.map((list) => [...list].sort()) // hacky return-sorted-values step
|
||||
.reduce((result, current) => {
|
||||
// rotate arrays into pairs
|
||||
current.forEach((n, i) => {
|
||||
result[i] === undefined ? (result[i] = [n]) : result[i].push(n);
|
||||
});
|
||||
return result;
|
||||
}, [] as Array<Array<number>>)
|
||||
.reduce((result, current) => {
|
||||
// do the math
|
||||
return result + Math.abs(current[0] - current[1]);
|
||||
}, 0)
|
||||
);
|
||||
|
||||
console.log(
|
||||
"part two:",
|
||||
Object.entries(
|
||||
data
|
||||
.split("\n")
|
||||
.slice(0, -1) // remove empty trailing newline
|
||||
.map(
|
||||
(v) => v.split(" ").filter((c) => c) // abusing js string nullity to remove empty members
|
||||
)
|
||||
.reduce((result, current) => {
|
||||
// build one object that contains the full set of frequencies
|
||||
result[current[0]] = !result[current[0]]
|
||||
? [1, 0]
|
||||
: [result[current[0]][0] + 1, result[current[0]][1]];
|
||||
result[current[1]] = !result[current[1]]
|
||||
? [0, 1]
|
||||
: [result[current[1]][0], result[current[1]][1] + 1];
|
||||
return result;
|
||||
}, [] as Array<Array<number>>)
|
||||
).reduce((result, current) => {
|
||||
// and then just reduce down the object.
|
||||
return result + parseInt(current[0], 10) * current[1][0] * current[1][1];
|
||||
}, 0)
|
||||
);
|
29
package-lock.json
generated
Normal file
29
package-lock.json
generated
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "AdventOfCode2024",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "22.10.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.10.1.tgz",
|
||||
"integrity": "sha512-qKgsUwfHZV2WCWLAnVP1JqnpE6Im6h3Y0+fYgMTasNQ7V++CBX5OT1as0g0f+OyubbFqhf6XVNIsmN4IIhEgGQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"undici-types": "~6.20.0"
|
||||
}
|
||||
},
|
||||
"node_modules/undici-types": {
|
||||
"version": "6.20.0",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz",
|
||||
"integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
5
package.json
Normal file
5
package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.1"
|
||||
}
|
||||
}
|
17
run.sh
Executable file
17
run.sh
Executable file
@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
|
||||
__year=2024
|
||||
|
||||
__day="${1}"
|
||||
if [ -z "${__day}" ]; then
|
||||
__day=$(date '+%d')
|
||||
fi
|
||||
|
||||
padded=$(printf "%02g" ${__day})
|
||||
if [ -f "${padded}/index.ts" ]; then
|
||||
cd $padded
|
||||
npx tsx "index.ts" 2>/dev/null
|
||||
cd - >/dev/null
|
||||
else
|
||||
echo "day not found"
|
||||
fi
|
7
today.sh
Normal file → Executable file
7
today.sh
Normal file → Executable file
@ -3,7 +3,7 @@
|
||||
__year=2024
|
||||
|
||||
__day="${1}"
|
||||
if [ "${day}" == ""]; then
|
||||
if [ "${__day}" == "" ]; then
|
||||
__day=$(date '+%d')
|
||||
fi
|
||||
|
||||
@ -15,7 +15,4 @@ if [ ! -f "${padded}/inputs/input" ]; then
|
||||
-A "https://git.yetaga.in/alazyreader/AdventOfCode${__year}/" \
|
||||
-H "Cookie: session=`cat .cookie`" https://adventofcode.com/${__year}/day/${__day##0}/input > "${padded}/inputs/input"
|
||||
fi
|
||||
# if [ ! -f "${padded}/main.go" ]; then
|
||||
# cp -n main.go.tmpl ${padded}/main.go
|
||||
# fi
|
||||
open "https://adventofcode.com/${__year}/day/${__day}"
|
||||
open "https://adventofcode.com/${__year}/day/${__day##0}"
|
||||
|
Loading…
Reference in New Issue
Block a user