day 3 in the books; add a template starter
This commit is contained in:
parent
0199cc2d68
commit
ac3cf84618
128
03/index.ts
Normal file
128
03/index.ts
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
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
|
||||||
|
.reduce((total, line) => {
|
||||||
|
let state = {
|
||||||
|
char: "",
|
||||||
|
multiplicand: "",
|
||||||
|
multiplier: "",
|
||||||
|
};
|
||||||
|
for (let index = 0; index < line.length; index++) {
|
||||||
|
switch (true) {
|
||||||
|
case "0123456789".includes(line[index]) && state.char === "(":
|
||||||
|
state.multiplicand += line[index];
|
||||||
|
break;
|
||||||
|
case "0123456789".includes(line[index]) && state.char === ",":
|
||||||
|
state.multiplier += line[index];
|
||||||
|
break;
|
||||||
|
case state.char === "(" &&
|
||||||
|
line[index] === "," &&
|
||||||
|
state.multiplicand !== "":
|
||||||
|
state.char = line[index];
|
||||||
|
break;
|
||||||
|
case line[index] === ")" &&
|
||||||
|
state.char === "," &&
|
||||||
|
state.multiplier !== "":
|
||||||
|
total +=
|
||||||
|
parseInt(state.multiplicand, 10) * parseInt(state.multiplier, 10);
|
||||||
|
state = { char: "", multiplicand: "", multiplier: "" };
|
||||||
|
break;
|
||||||
|
case line[index] === "m" && state.char === "":
|
||||||
|
case line[index] === "u" && state.char === "m":
|
||||||
|
case line[index] === "l" && state.char === "u":
|
||||||
|
case line[index] === "(" && state.char === "l":
|
||||||
|
state.char = line[index];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
state = { char: "", multiplicand: "", multiplier: "" };
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}, 0)
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"part two:",
|
||||||
|
data
|
||||||
|
.split("\n")
|
||||||
|
.slice(0, -1) // remove empty trailing newline
|
||||||
|
.reduce(
|
||||||
|
(total, line) => {
|
||||||
|
let state = {
|
||||||
|
char: "",
|
||||||
|
multiplicand: "",
|
||||||
|
multiplier: "",
|
||||||
|
on: total.on,
|
||||||
|
};
|
||||||
|
for (let index = 0; index < line.length; index++) {
|
||||||
|
switch (true) {
|
||||||
|
case "0123456789".includes(line[index]) && state.char === "mul(":
|
||||||
|
state.multiplicand += line[index];
|
||||||
|
break;
|
||||||
|
case "0123456789".includes(line[index]) && state.char === ",":
|
||||||
|
state.multiplier += line[index];
|
||||||
|
break;
|
||||||
|
case state.char === "mul(" &&
|
||||||
|
line[index] === "," &&
|
||||||
|
state.multiplicand !== "":
|
||||||
|
state.char = line[index];
|
||||||
|
break;
|
||||||
|
case line[index] === ")" &&
|
||||||
|
state.char === "," &&
|
||||||
|
state.multiplier !== "":
|
||||||
|
if (state.on) {
|
||||||
|
total = {
|
||||||
|
...total,
|
||||||
|
sum: (total.sum +=
|
||||||
|
parseInt(state.multiplicand, 10) *
|
||||||
|
parseInt(state.multiplier, 10)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
state = {
|
||||||
|
...state,
|
||||||
|
char: "",
|
||||||
|
multiplicand: "",
|
||||||
|
multiplier: "",
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case line[index] === ")" && state.char === "don't(":
|
||||||
|
state = { char: "", multiplicand: "", multiplier: "", on: false };
|
||||||
|
break;
|
||||||
|
case line[index] === ")" && state.char === "do(":
|
||||||
|
state = { char: "", multiplicand: "", multiplier: "", on: true };
|
||||||
|
break;
|
||||||
|
case line[index] === "m" && state.char === "":
|
||||||
|
case line[index] === "u" && state.char === "m":
|
||||||
|
case line[index] === "l" && state.char === "mu":
|
||||||
|
case line[index] === "(" && state.char === "mul":
|
||||||
|
case line[index] === "d" && state.char === "":
|
||||||
|
case line[index] === "o" && state.char === "d":
|
||||||
|
case line[index] === "n" && state.char === "do":
|
||||||
|
case line[index] === "(" && state.char === "do":
|
||||||
|
case line[index] === "'" && state.char === "don":
|
||||||
|
case line[index] === "t" && state.char === "don'":
|
||||||
|
case line[index] === "(" && state.char === "don't":
|
||||||
|
state.char += line[index];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
state = {
|
||||||
|
...state,
|
||||||
|
char: "",
|
||||||
|
multiplicand: "",
|
||||||
|
multiplier: "",
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { ...total, on: state.on };
|
||||||
|
},
|
||||||
|
{ sum: 0, on: true }
|
||||||
|
).sum
|
||||||
|
);
|
13
index.tmpl.ts
Normal file
13
index.tmpl.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
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
|
||||||
|
);
|
3
today.sh
3
today.sh
@ -15,4 +15,7 @@ if [ ! -f "${padded}/inputs/input" ]; then
|
|||||||
-A "https://git.yetaga.in/alazyreader/AdventOfCode${__year}/" \
|
-A "https://git.yetaga.in/alazyreader/AdventOfCode${__year}/" \
|
||||||
-H "Cookie: session=`cat .cookie`" https://adventofcode.com/${__year}/day/${__day##0}/input > "${padded}/inputs/input"
|
-H "Cookie: session=`cat .cookie`" https://adventofcode.com/${__year}/day/${__day##0}/input > "${padded}/inputs/input"
|
||||||
fi
|
fi
|
||||||
|
if [ ! -f "${padded}/index.ts" ]; then
|
||||||
|
cp index.tmpl.ts ${padded}/index.ts
|
||||||
|
fi
|
||||||
open "https://adventofcode.com/${__year}/day/${__day##0}"
|
open "https://adventofcode.com/${__year}/day/${__day##0}"
|
||||||
|
Loading…
Reference in New Issue
Block a user