Skip to content

Commit

Permalink
day 13 (part 1) (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
adaamz authored Dec 13, 2020
1 parent 0d74823 commit e1ac3b4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 13/final_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1003055
37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,433,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,29,x,593,x,x,x,x,x,x,x,x,x,x,x,x,13
2 changes: 2 additions & 0 deletions 13/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Day 13: Shuttle Search
https://adventofcode.com/2020/day/13
17 changes: 17 additions & 0 deletions 13/src/13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function countPart1(input: string[]): number
{
const now = Number(input[0]);
const workingBuses = input[1].split(",").filter(line => line !== 'x').map(Number);
let minTimeArrival: [number, number] = [Infinity, 0];

for (const bus of workingBuses) {
const arrival = Math.ceil(now / bus) * bus;
if (minTimeArrival[0] > arrival) {
minTimeArrival = [arrival, bus];
}
}

return (minTimeArrival[0] - now) * minTimeArrival[1];
}

// part 2 not implemented
24 changes: 24 additions & 0 deletions 13/tests/13.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {assertStrictEquals} from "../../deps.ts";
import {countPart1} from "../src/13.ts";

Deno.test("ShuttleSearch: Example test from introduction (Part one)", () => {
const input = [
"939",
"7,13,x,x,59,x,31,19"
];
const expectedOutput = 295;

assertStrictEquals(countPart1(input), expectedOutput);
});

Deno.test("ShuttleSearch: Final test (Part one)", () => {
const input = (
Deno.readTextFileSync("13/final_input.txt").trimEnd()
).split("\n");

const expectedOutput = 410;

assertStrictEquals(countPart1(input), expectedOutput);
});

// part 2 not implemented

0 comments on commit e1ac3b4

Please sign in to comment.