-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b7076d
commit f035736
Showing
6 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Launch Program", | ||
"skipFiles": [ | ||
"<node_internals>/**", | ||
"node_modules/**" | ||
], | ||
"program": "${workspaceFolder}\\index.js" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const part1 = data => | ||
new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve( | ||
Array.from(data) | ||
.reverse() | ||
.join("") | ||
); | ||
}, 1000); | ||
}); | ||
|
||
module.exports = { | ||
part1 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const runner = require("./runner"); | ||
|
||
// Instructions: | ||
// For each day, create a new JS file and a puzzle input file consisting | ||
// of the day number and then the extension .py or .txt. | ||
// For example, on day 20 you would create "20.js" and "20.txt" | ||
// Don't forget to put a 0 before days 1-9 (so day 5 becomes "05.js") but not on | ||
// any other days. This ensures the days are sorted properly in the files view. | ||
|
||
// Each day in the JS file, all you have to do is write and export a | ||
// part1(prompt) method and paste your puzzle input in the text file. | ||
// You can also return a value and it will be printed out. | ||
// The input will be read and passed to your code automatically. | ||
// Add a part2() method when you're ready. | ||
// Look at 01.py for an example | ||
|
||
// Update the day in runner.run() each day to change which day is run. | ||
// Your code will be automatically timed. | ||
|
||
// For a menu where users can select a day, use this | ||
// the argument for maxDay is optional, remove it for no limit | ||
// runner.getDay(5).then((day) => runner.run(day)) | ||
|
||
runner.run(1); | ||
|
||
(""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "advent_code_2020", | ||
"version": "1.0.0", | ||
"description": "Just a repo for holding my work for Advent of Code 2020", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/miquelfire/aoc2020.git" | ||
}, | ||
"author": "Miquel Burns <[email protected]> (https://miquelfire.red)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/miquelfire/aoc2020/issues" | ||
}, | ||
"homepage": "https://github.com/miquelfire/aoc2020#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// AOC Runner | ||
// Author: Scoder12 | ||
|
||
const { performance } = require("perf_hooks"); | ||
const fs = require("fs").promises; | ||
const readline = require("readline"); | ||
|
||
process.on("unhandledRejection", error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
|
||
const formatFilename = day => { | ||
// You can customize this function to your liking | ||
|
||
return day.toString().padStart(2, "0"); | ||
}; | ||
|
||
const formatRuntime = ms => { | ||
// You can customize this function to your liking | ||
|
||
// microseconds | ||
if (ms <= 1) { | ||
return Math.round(ms * 1000) + "µs"; | ||
} | ||
// miliseconds | ||
if (ms < 1000) { | ||
const wholeMs = Math.floor(ms); | ||
return wholeMs + "ms " + formatRuntime(ms - wholeMs); | ||
} | ||
const sec = ms / 1000; | ||
if (sec < 60) { | ||
const wholeSec = Math.floor(sec); | ||
const remMs = ms - wholeSec * 1000; | ||
return wholeSec + "s " + formatRuntime(remMs); | ||
} | ||
// Minutes (hopefully it doesn't get to this point lol) | ||
return `${Math.floor(sec / 60)}m ` + formatRuntime((sec % 60) * 1000); | ||
}; | ||
|
||
const runPart = async (part, mod, data) => { | ||
const funcname = "part" + part; | ||
const func = mod ? mod[funcname] : undefined; | ||
|
||
if (typeof func === "function") { | ||
console.log("Running Part", part); | ||
|
||
const start = performance.now(); | ||
let output = func(data); | ||
// You might want to comment this out to get a slight performance benefit | ||
if (output instanceof Promise) { | ||
output = await output; | ||
} | ||
const end = performance.now(); | ||
|
||
console.log("Output:", output); | ||
const rtime = end - start; | ||
console.log("Took:", formatRuntime(rtime)); | ||
return rtime; | ||
} else { | ||
console.log(`No ${funcname} function`); | ||
return 0; | ||
} | ||
}; | ||
|
||
const getData = async day => { | ||
const fname = formatFilename(day) + ".txt"; | ||
|
||
let data; | ||
try { | ||
const buf = await fs.readFile(fname); | ||
data = buf.toString("utf-8"); | ||
} catch (e) { | ||
if (e && e.message) { | ||
e.message = `Error while reading ${fname}: ` + e.message; | ||
} | ||
throw e; | ||
} | ||
return data; | ||
}; | ||
|
||
const run = async (day, year = 2020) => { | ||
console.log(`AOC ${year} Day ${day}`); | ||
|
||
const mod = require("./" + formatFilename(day)); | ||
const data = await getData(day); | ||
|
||
const part1Time = await runPart(1, mod, data); | ||
const part2Time = await runPart(2, mod, data); | ||
if (part1Time != 0 && part2Time != 0) { | ||
console.log(`Total time: ${formatRuntime(part1Time + part2Time)}`); | ||
} | ||
}; | ||
|
||
const ask = question => | ||
new Promise(resolve => { | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
|
||
rl.question(question, answer => { | ||
rl.close(); | ||
resolve(answer); | ||
}); | ||
}); | ||
|
||
const getDay = async maxDay => { | ||
while (true) { | ||
let question = "Enter day" + (maxDay ? ` (max ${maxDay})` : "") + ": "; | ||
const inp = await ask(question); | ||
|
||
const day = Number(inp); | ||
if (isNaN(day)) { | ||
console.log("Invalid day"); | ||
} else if (maxDay && day > maxDay) { | ||
console.log(`Must be at most ${maxDay}`); | ||
} else { | ||
return day; | ||
} | ||
} | ||
}; | ||
|
||
module.exports = { | ||
formatFilename, | ||
formatRuntime, | ||
run, | ||
getDay | ||
}; |