From ac3cf846188a3b30c7ac001c2d4ffe136e9848ff Mon Sep 17 00:00:00 2001 From: David Ashby Date: Tue, 3 Dec 2024 21:42:55 -0500 Subject: [PATCH] day 3 in the books; add a template starter --- 03/index.ts | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.tmpl.ts | 13 +++++ today.sh | 3 ++ 3 files changed, 144 insertions(+) create mode 100644 03/index.ts create mode 100644 index.tmpl.ts diff --git a/03/index.ts b/03/index.ts new file mode 100644 index 0000000..f7d7048 --- /dev/null +++ b/03/index.ts @@ -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 +); diff --git a/index.tmpl.ts b/index.tmpl.ts new file mode 100644 index 0000000..2a5b8f1 --- /dev/null +++ b/index.tmpl.ts @@ -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 +); diff --git a/today.sh b/today.sh index 79b8522..94ffe3d 100755 --- a/today.sh +++ b/today.sh @@ -15,4 +15,7 @@ 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}/index.ts" ]; then + cp index.tmpl.ts ${padded}/index.ts +fi open "https://adventofcode.com/${__year}/day/${__day##0}"