Skip to content

Commit

Permalink
day 15 with Map data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
adaamz committed Dec 15, 2020
1 parent 401264c commit 23c7ac5
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions 15/src/15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ export function countPart1(input: number[]): number

export function countPart2(input: number[]): number
{
// 6 minutes in Node :O
// 144s in Deno :O
// Object
// 6 minutes in Node (using Object) :O
// 144s in Deno (using Object) :O

// Map
// 6 seconds in Node (using Map)
// 5 seconds in Deno (using Map)
return calculateNth(input, 30000000);
}

function calculateNth(input: number[], nth: number) {
const start = new Date();
let previousNumber = input[input.length - 1];
const indexes: {[index: number]: number} = {};
const indexes = new Map<number, number>();

for (let i = 0; i < input.length - 1; i++) {
indexes[input[i]] = i + 1;
indexes.set(input[i], i + 1);
}

for (let i = input.length; i < nth; i++) {
const previousPosition = indexes[previousNumber];
indexes[previousNumber] = i;
const previousPosition = indexes.get(previousNumber);
indexes.set(previousNumber, i);

if (previousPosition === undefined) {
previousNumber = 0;
Expand Down

0 comments on commit 23c7ac5

Please sign in to comment.