From fcef9dee904689874f38fe65c89785b93c2993a5 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Thu, 2 Dec 2021 08:37:00 -0600 Subject: [PATCH] Day 2 part 1 done --- 2021/2.ts | 26 ++++++++++++++++++++++++++ 2021/common.ts | 3 +-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 2021/2.ts diff --git a/2021/2.ts b/2021/2.ts new file mode 100644 index 0000000..0ac231f --- /dev/null +++ b/2021/2.ts @@ -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))); diff --git a/2021/common.ts b/2021/common.ts index 2c67753..b14f00f 100644 --- a/2021/common.ts +++ b/2021/common.ts @@ -5,7 +5,6 @@ export const XDG_CACHE_HOME = Deno.env.get("XDG_CACHE_HOME") || path.join(HOME, ".config"); const INPUT_CACHE_DIR = path.join(XDG_CACHE_HOME, "aoc2021"); -console.log(INPUT_CACHE_DIR); async function fileExists(filePath: string) { try { @@ -96,7 +95,7 @@ export async function preloadedAsyncIterator( return asyncIterator(arr); } -export async function measureDuration(cb: () => Promise) { +export async function measureDuration(cb: () => Promise | void) { const p1t = window.performance.now(); await cb(); const p1t2 = window.performance.now();