Skip to content

Commit e08a2ae

Browse files
committedDec 1, 2022
Complete day 1
0 parents  commit e08a2ae

File tree

7 files changed

+2325
-0
lines changed

7 files changed

+2325
-0
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

‎.vscode/settings.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"deno.enable": true,
3+
"deno.unstable": true
4+
}

‎days/01.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { assertEquals } from "https://deno.land/std@0.165.0/testing/asserts.ts";
2+
import {part1, part2} from "./01.ts";
3+
4+
5+
const testDataFromExample =
6+
`1000
7+
2000
8+
3000
9+
10+
4000
11+
12+
5000
13+
6000
14+
15+
7000
16+
8000
17+
9000
18+
19+
10000`;
20+
21+
22+
Deno.test("Day 1 Part 1", async (t) => {
23+
await t.step("example", () => {
24+
assertEquals(part1(testDataFromExample), 24000);
25+
})
26+
27+
await t.step("answer", async () => {
28+
const input = await Deno.readTextFile(`./days/01.txt`);
29+
assertEquals(part1(input), 70374);
30+
});
31+
});
32+
33+
Deno.test("Day 1 Part 2", async (t) => {
34+
await t.step("example", () => {
35+
assertEquals(part2(testDataFromExample), 45000);
36+
})
37+
38+
await t.step("answer", async () => {
39+
const input = await Deno.readTextFile(`./days/01.txt`);
40+
assertEquals(part2(input), 204610);
41+
});
42+
});

‎days/01.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
// returns [1000,3000,2000], a list of total calories of each elf
3+
function parseInput(input:string):any {
4+
const elves = [0];
5+
input.split("\n").forEach((line:string) => {
6+
if (line !== "") {
7+
elves[elves.length-1] += parseInt(line);
8+
} else {
9+
elves.push(0);
10+
}
11+
})
12+
return elves;
13+
}
14+
15+
function sumArr(arr:number[]):number {
16+
return arr.reduce((acc, item) => acc + item, 0);
17+
}
18+
19+
export function part1(input:string):number {
20+
const elves = parseInput(input);
21+
return Math.max(...elves);
22+
}
23+
24+
function sortDesc (a:number,b:number) {return b-a};
25+
26+
export function part2(input:string):number {
27+
const elves = parseInput(input);
28+
const top3 = elves.slice(0).sort(sortDesc).slice(0,3);
29+
return sumArr(top3);
30+
}

‎days/01.txt

+2,234
Large diffs are not rendered by default.

‎deno.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"tasks": {
3+
"test": "deno test --watch --allow-read ."
4+
}
5+
}

‎deno.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.