Last active
December 1, 2020 13:32
-
-
Save stenehall/07918265b259c69bd5c5616465f58f66 to your computer and use it in GitHub Desktop.
Advent of Code
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
import { assertEquals } from 'https://deno.land/std@0.79.0/testing/asserts.ts' | |
const numbers = (await Deno.readTextFile('./sample_data/aoc-day1.txt')) | |
.split('\n') | |
.map(Number) | |
function* nextNumber(numbers: number[]) { | |
for (const value of numbers) { | |
yield value | |
} | |
} | |
function day1x1(numbers: number[], searchNumber: number) { | |
const numberSet = new Set(numbers) | |
let a = 0 | |
loop: for (a of nextNumber(numbers)) { | |
if (numberSet.has(searchNumber - a)) break loop | |
} | |
return { a, b: searchNumber - a } | |
} | |
function day1x2(numbers: number[], searchNumber: number) { | |
const numberSet = new Set(numbers) | |
let a = 0 | |
let b = 0 | |
loop: for (a of nextNumber(numbers)) { | |
for (b of nextNumber(numbers)) { | |
if (numberSet.has(searchNumber - a - b)) break loop | |
} | |
} | |
return { a, b, c: searchNumber - a - b } | |
} | |
/** | |
* Test cases for Day 1 | |
*/ | |
Deno.test('AOC - Day 1 example', async () => { | |
const numbers = '1721\n979\n366\n299\n675\n1456' | |
.split('\n') | |
.map((number) => parseInt(number)) | |
let { a, b } = day1x1(numbers, 2020) | |
console.log(`Found ${a} and ${b}`) | |
assertEquals(a * b, 514579) | |
}) | |
Deno.test('AOC - Day 1 task 1', async () => { | |
let { a, b } = day1x1(numbers, 2020) | |
console.log(`Found ${a} and ${b}`) | |
assertEquals(a * b, 842016) | |
}) | |
Deno.test('AOC - Day 1 task 2', async () => { | |
let { a, b, c } = day1x2(numbers, 2020) | |
console.log(`Found ${a} and ${b}`) | |
assertEquals(a * b * c, 9199664) | |
}) |
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
day01: | |
@deno test --allow-read src/aoc-day1.ts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment