update with day04 and first puzzle of day05
This commit is contained in:
1187
day05/input.txt
Normal file
1187
day05/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
80
day05/js/day05a.js
Normal file
80
day05/js/day05a.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import { promises as fsPromises } from "node:fs";
|
||||
|
||||
async function asyncReadFile(filename) {
|
||||
try {
|
||||
const contents = await fsPromises.readFile(filename, "utf-8");
|
||||
|
||||
const arr = contents.split(/\r?\n/);
|
||||
|
||||
return arr;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
const parseData = (rows) => {
|
||||
let isInRanges = true;
|
||||
let freshRanges = [];
|
||||
let freshCount = 0;
|
||||
|
||||
rows.forEach((row) => {
|
||||
if (isInRanges) {
|
||||
if (row === "") {
|
||||
isInRanges = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (row.includes("-")) {
|
||||
freshRanges.push(row);
|
||||
/*
|
||||
const [start, end] = row.split("-").map((val) => val * 1);
|
||||
for (let i = start; i <= end; i++) {
|
||||
if (!freshIngredients.includes(i)) freshIngredients.push(i);
|
||||
}
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
if (/^\d+$/.matches(row) && !freshIngredients.includes(row * 1)) {
|
||||
// freshIngredients.push(row * 1);
|
||||
freshRanges.push(row);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let iNum = row * 1;
|
||||
let isFresh = freshRanges.some((range) => {
|
||||
if (!range.includes("-")) return range === `${iNum}`;
|
||||
const [start, end] = range.split(/-/).map((val) => val * 1);
|
||||
// console.log({ range, iNum, start, end });
|
||||
return iNum >= start && iNum <= end;
|
||||
});
|
||||
|
||||
if (isFresh) freshCount++;
|
||||
|
||||
console.log(`Ingredient ${row} is ${isFresh ? "fresh" : "spoiled"}.`);
|
||||
});
|
||||
|
||||
return freshCount;
|
||||
};
|
||||
|
||||
const testData = `
|
||||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
`.trim();
|
||||
|
||||
// const rows = testData.split("\n");
|
||||
|
||||
const rows = await asyncReadFile("../input.txt");
|
||||
|
||||
console.log(`Fresh ingredient count: ${parseData(rows)}`);
|
||||
12
day05/notes.md
Normal file
12
day05/notes.md
Normal file
@@ -0,0 +1,12 @@
|
||||
## Day 5: Cafeteria
|
||||
|
||||
Puzzle 1 took me 30-ish minutes to figure out. I initially tried building an array of numbers from the ranges, but it turned out to be _much_ easier to parse each range against each value and let the machine do the math.
|
||||
|
||||
Puzzle 2 seemed pretty straightforward, and I thought I had a simple solution, but it didn't work, and an hour later (1:30a), I have to admit defeat for tonight.
|
||||
|
||||
### JS Solutions
|
||||
|
||||
```
|
||||
cd js
|
||||
node day05a.js
|
||||
```
|
||||
Reference in New Issue
Block a user