first commit
This commit is contained in:
1
day02/input.txt
Normal file
1
day02/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
3335355312-3335478020,62597156-62638027,94888325-95016472,4653-6357,54-79,1-19,314-423,472-650,217886-298699,58843645-58909745,2799-3721,150748-178674,9084373-9176707,1744-2691,17039821-17193560,2140045-2264792,743-1030,6666577818-6666739950,22946-32222,58933-81008,714665437-714803123,9972438-10023331,120068-142180,101-120,726684-913526,7575737649-7575766026,8200-11903,81-96,540949-687222,35704-54213,991404-1009392,335082-425865,196-268,3278941-3383621,915593-991111,32-47,431725-452205
|
||||
45
day02/js/day02a.js
Normal file
45
day02/js/day02a.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import { promises as fsp } from "node:fs";
|
||||
|
||||
async function asyncReadFile(filename) {
|
||||
try {
|
||||
const contents = await fsp.readFile(filename, "utf-8");
|
||||
|
||||
return contents.trim();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
const dataToParse =
|
||||
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124";
|
||||
*/
|
||||
|
||||
const dataToParse = await asyncReadFile("../input.txt");
|
||||
|
||||
const isDuplicate = (val = "") => {
|
||||
const strVal = `${val}`,
|
||||
strLen = strVal.length;
|
||||
if (!strLen || strLen % 2 === 1) return false;
|
||||
|
||||
return strVal.substring(0, strLen / 2) === strVal.substring(strLen / 2);
|
||||
};
|
||||
|
||||
const ranges = dataToParse.split(",");
|
||||
let total = 0;
|
||||
|
||||
ranges.forEach((range) => {
|
||||
console.log(`> running range ${range}`);
|
||||
const [start, end] = range.split("-"),
|
||||
last = end * 1;
|
||||
let curr = start * 1;
|
||||
while (curr <= last) {
|
||||
if (isDuplicate(curr)) {
|
||||
total += curr;
|
||||
console.log(`>> found duplicate: ${curr} / ${total}`);
|
||||
}
|
||||
curr++;
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`> FINAL: ${total}`);
|
||||
59
day02/js/day02b.js
Normal file
59
day02/js/day02b.js
Normal file
@@ -0,0 +1,59 @@
|
||||
import { promises as fsp } from "node:fs";
|
||||
|
||||
async function asyncReadFile(filename) {
|
||||
try {
|
||||
const contents = await fsp.readFile(filename, "utf-8");
|
||||
|
||||
return contents.trim();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
const dataToParse =
|
||||
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124";
|
||||
*/
|
||||
|
||||
const dataToParse = await asyncReadFile("../input.txt");
|
||||
|
||||
const isDuplicateOld = (val = "") => {
|
||||
const strVal = `${val}`,
|
||||
strLen = strVal.length;
|
||||
if (!strLen || strLen % 2 === 1) return false;
|
||||
|
||||
return strVal.substring(0, strLen / 2) === strVal.substring(strLen / 2);
|
||||
};
|
||||
|
||||
const duplicateRE = /^(\d+)\1+$/;
|
||||
const isDuplicate = (val = "") => {
|
||||
// new process - runa regex that looks for repeats from start to end
|
||||
const strVal = `${val}`,
|
||||
strLen = strVal.length;
|
||||
if (!strLen) return false;
|
||||
|
||||
const matches = strVal.match(duplicateRE);
|
||||
|
||||
if (matches === null) return false;
|
||||
|
||||
return matches.length >= 1 && matches[0] === strVal;
|
||||
};
|
||||
|
||||
const ranges = dataToParse.split(",");
|
||||
let total = 0;
|
||||
|
||||
ranges.forEach((range) => {
|
||||
console.log(`> running range ${range}`);
|
||||
const [start, end] = range.split("-"),
|
||||
last = end * 1;
|
||||
let curr = start * 1;
|
||||
while (curr <= last) {
|
||||
if (isDuplicate(curr)) {
|
||||
total += curr;
|
||||
console.log(`>> found duplicate: ${curr} / ${total}`);
|
||||
}
|
||||
curr++;
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`> FINAL: ${total}`);
|
||||
13
day02/notes.md
Normal file
13
day02/notes.md
Normal file
@@ -0,0 +1,13 @@
|
||||
## Day 2: Gift Shop
|
||||
|
||||
This got a bit frustrating with the regex in the second part.
|
||||
|
||||
It only took about 30 minutes to solve first part, but my answer involved literally splitting strings, so it didn't work for arbitrary matches and had to be replaced by a regex solution. After struggling with the regex for another 30 minutes, I put it away until the next day, when a 5-minute web search lead me to the fix for my bad regex.
|
||||
|
||||
### JS Solutions
|
||||
|
||||
```
|
||||
cd js
|
||||
node day01a.js
|
||||
node day01b.js
|
||||
```
|
||||
Reference in New Issue
Block a user