Day 2 part 1 done

This commit is contained in:
Daniel Flanagan 2021-12-02 08:37:00 -06:00
parent 7ca5b3d8fe
commit fcef9dee90
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
2 changed files with 27 additions and 2 deletions

26
2021/2.ts Normal file
View file

@ -0,0 +1,26 @@
import { collectArray, inputLines, measureDuration } from "./common.ts";
const input = await collectArray(await inputLines("2"));
export function part1(input: string[]): number {
let x = 0;
let y = 0;
for (const line of input) {
console.log(line);
if (line.startsWith("forward ")) {
x += parseInt(line.substr(8));
} else if (line.startsWith("up ")) {
y -= parseInt(line.substr(3));
} else if (line.startsWith("down ")) {
y += parseInt(line.substr(5));
}
}
return x * y;
}
await measureDuration(() => console.log("Part 1", part1(input)));
export function part2(input: string[]): number {
return 0;
}
await measureDuration(() => console.log("Part 2", part2(input)));

View file

@ -5,7 +5,6 @@ export const XDG_CACHE_HOME = Deno.env.get("XDG_CACHE_HOME") ||
path.join(HOME, ".config"); path.join(HOME, ".config");
const INPUT_CACHE_DIR = path.join(XDG_CACHE_HOME, "aoc2021"); const INPUT_CACHE_DIR = path.join(XDG_CACHE_HOME, "aoc2021");
console.log(INPUT_CACHE_DIR);
async function fileExists(filePath: string) { async function fileExists(filePath: string) {
try { try {
@ -96,7 +95,7 @@ export async function preloadedAsyncIterator<T>(
return asyncIterator(arr); return asyncIterator(arr);
} }
export async function measureDuration(cb: () => Promise<void>) { export async function measureDuration(cb: () => Promise<void> | void) {
const p1t = window.performance.now(); const p1t = window.performance.now();
await cb(); await cb();
const p1t2 = window.performance.now(); const p1t2 = window.performance.now();