summaryrefslogtreecommitdiff
path: root/aoc_2023/day-01/main_2.js
diff options
context:
space:
mode:
authorElizabeth (Lizzy) Hunt <elizabeth.hunt@simponic.xyz>2023-12-01 15:49:11 -0700
committerGitHub <noreply@github.com>2023-12-01 15:49:11 -0700
commitcdb12dbc28cc441745bad55e384494bc8942586a (patch)
treec0be1ce91bc908e29b6f1eb67b29f6053af71ff6 /aoc_2023/day-01/main_2.js
parentdd297be17d94f59a856add6dc7cd4c446225e099 (diff)
downloadaoc-cdb12dbc28cc441745bad55e384494bc8942586a.tar.gz
aoc-cdb12dbc28cc441745bad55e384494bc8942586a.zip
Template (#1)
* tempalte foo * buff aoc stuff
Diffstat (limited to 'aoc_2023/day-01/main_2.js')
-rw-r--r--aoc_2023/day-01/main_2.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/aoc_2023/day-01/main_2.js b/aoc_2023/day-01/main_2.js
new file mode 100644
index 0000000..3ba05be
--- /dev/null
+++ b/aoc_2023/day-01/main_2.js
@@ -0,0 +1,43 @@
+const fs = require("node:fs");
+
+const digits = {
+ one: 1,
+ two: 2,
+ three: 3,
+ four: 4,
+ five: 5,
+ six: 6,
+ seven: 7,
+ eight: 8,
+ nine: 9,
+};
+
+const data = fs.readFileSync("input.txt", "utf8");
+
+const res = data
+ .split("\n")
+ .filter((line) => line && line != "")
+ .map((line) => {
+ let newLine = "";
+ for (let i = 0; i < line.length; i++) {
+ for (let j = i + 1; j < line.length + 1; j++) {
+ const word = line.substring(i, j);
+ if (word.match(/^[0-9]$/)) {
+ newLine += word;
+ }
+ if (word in digits) {
+ newLine += digits[word].toString();
+ }
+ }
+ }
+ return newLine;
+ })
+ .reduce((acc, line) => {
+ const nums = line.split("");
+ const first = parseInt(nums.at(0));
+ const last = parseInt(nums.at(-1));
+
+ return acc + (first * 10 + last);
+ }, 0);
+
+console.log(res);