Skip to content

Commit 20d03c1

Browse files
committed
solve day 07 in deno
1 parent cb63751 commit 20d03c1

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

.github/workflows/day-00-example.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
test:
2121
runs-on: ubuntu-latest
2222
container:
23-
image: "jonasjso/adventofcode2020:2020-12-05-with-node15"
23+
image: "jonasjso/adventofcode2020:2020-12-07-with-deno"
2424
steps:
2525
- uses: actions/checkout@v2
2626
- name: make versions

days/day-07/solutions/day07.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { readFileSync } from "fs";
2-
import { resolve } from "path";
32

43
const STANDARD_IN = 0
54
const input = readFileSync(STANDARD_IN)

days/day-07/solutions/day07.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const input = (await Deno.readTextFile("/dev/stdin"));
2+
3+
type Bag = {
4+
color: string;
5+
bags_inside: BagRef[];
6+
};
7+
type BagRef = {
8+
count: number;
9+
color: string;
10+
};
11+
12+
const bags = [
13+
...input.matchAll(/([\w]+ [\w]+) bags contain (\d (.*)[.]|no other bags)/g),
14+
].map(([, color, rest]) => {
15+
const bags_inside = ([
16+
...rest.matchAll(/(\d) ([\w]+ [\w]+)/g),
17+
])
18+
.map(([, count, color]) => ({ count: parseInt(count), color })) as BagRef[];
19+
20+
return ({ color, bags_inside });
21+
}) as Bag[];
22+
23+
const COLOR_SHINY_GOLD = "shiny gold";
24+
25+
// --- Part 1 ----
26+
// Recursively find shiny golden bags
27+
function findShinyGoldenBag(bag_refs: BagRef[]) {
28+
if (bag_refs.length === 0) {
29+
return 0;
30+
}
31+
32+
const resolved_bags = bag_refs
33+
.map((bag) => bags.find(({ color }) => color === bag.color))
34+
.filter((bag) => bag !== undefined) as Bag[];
35+
36+
for (const { color, bags_inside } of resolved_bags) {
37+
if (color === COLOR_SHINY_GOLD) {
38+
return 1;
39+
} else {
40+
const found = findShinyGoldenBag(bags_inside);
41+
if (found) {
42+
return 1;
43+
} else {
44+
continue;
45+
}
46+
}
47+
}
48+
return 0;
49+
}
50+
51+
const bagContainingShinyGoldeBagCount = bags
52+
.reduce((count, bag) => count + findShinyGoldenBag(bag.bags_inside), 0);
53+
54+
// --- Part 2 ---
55+
// Recursively count bags inside
56+
function countBagsInside(bag_refs: BagRef[]): number {
57+
if (bag_refs.length === 0) {
58+
return 1;
59+
}
60+
61+
const resolved_bags = bag_refs
62+
.map(
63+
(bag) => [bag.count, bags.find(({ color }) => color === bag.color)]
64+
) as [number, Bag][];
65+
66+
const count = resolved_bags
67+
.reduce(
68+
(acc, [bag_count, bag]) =>
69+
acc + bag_count * countBagsInside(bag.bags_inside),
70+
1,
71+
);
72+
73+
return count;
74+
}
75+
76+
const shinyGoldenBag = bags.find(({ color }) =>
77+
color === COLOR_SHINY_GOLD
78+
) as Bag;
79+
80+
const shinyGoldBagInsideCount = countBagsInside(shinyGoldenBag.bags_inside) - 1;
81+
82+
console.log(`${bagContainingShinyGoldeBagCount}`);
83+
console.log(`${shinyGoldBagInsideCount}`);

days/day-07/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ $D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day07.sml
1010
$D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/tholok97.go
1111
$D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day07.stektpotet.py
1212
$D/../../languages/node.sh $D/input.txt $D/output.txt $D/solutions/day07.mjs
13+
$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day07.ts
1314
echo ""

0 commit comments

Comments
 (0)