Skip to content

Commit 2084481

Browse files
committed
Add day 7 part2
1 parent d6ee756 commit 2084481

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

src/2020/_dayx/test.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {part1} from './part1';
21
import {readInput} from '../../utils';
32

3+
import {part1} from './part1';
4+
45
describe('Advent of Code 2020 - Day x', () => {
56
let input: string;
67
beforeAll(async () => {

src/2020/day7/part1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ type Bags = Record<string, string[]>;
22

33
const parseColor = (rule: string) => rule.replace(/\d|bag(s?.)?/g, '').trim();
44

5-
const parseBags = (input: string): Bags => {
5+
export const parseBags = (input: string): Bags => {
66
return input.split('\n').reduce((bags, r) => {
77
const [, color, rules] = r.match(/([a-z]+ [a-z]+) bags contain (.*)/)!;
88
return {...bags, [color]: rules.split(', ')};

src/2020/day7/part2.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {parseBags} from './part1';
2+
3+
const parseAmount = (rule: string) => Number(rule.match(/\d/));
4+
const parseColor = (rule: string) => {
5+
const color = rule.replace(/\d|bag(s?.?)?/g, '').trim();
6+
return !color.includes('no other') ? color : '';
7+
};
8+
9+
export const part2 = (input: string) => {
10+
const bags = parseBags(input);
11+
const bagsToCount = ['shiny gold'];
12+
let sum = 0;
13+
14+
while (bagsToCount.length > 0) {
15+
const bag = bagsToCount.pop()!;
16+
const bagsCounter = bags[bag].reduce((a, rule) => {
17+
const amount = parseAmount(rule);
18+
19+
bagsToCount.push(
20+
...Array(amount)
21+
.fill(parseColor(rule))
22+
.filter(a => a) // remove '' (no other bags)
23+
);
24+
25+
return a + amount;
26+
}, 0);
27+
28+
sum += bagsCounter;
29+
}
30+
31+
return sum;
32+
};

src/2020/day7/test.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import {part1} from './part1';
21
import {readInput} from '../../utils';
32

3+
import {part1} from './part1';
4+
import {part2} from './part2';
5+
46
describe('Advent of Code 2020 - Day 7', () => {
57
let testInput: string;
8+
let testInput2: string;
69
let input: string;
10+
711
beforeAll(async () => {
812
testInput = await readInput(__dirname + '/test_input');
13+
testInput2 = await readInput(__dirname + '/test_input_2');
914
input = await readInput(__dirname + '/input');
1015
});
1116

@@ -18,4 +23,18 @@ describe('Advent of Code 2020 - Day 7', () => {
1823
expect(part1(input)).toBe(289);
1924
});
2025
});
26+
27+
describe('part 2', () => {
28+
it('should output 32 from test_input', () => {
29+
expect(part2(testInput)).toBe(32);
30+
});
31+
32+
it('should output 126 from test_input2', () => {
33+
expect(part2(testInput2)).toBe(126);
34+
});
35+
36+
it('should output 30055 valid bags from input', () => {
37+
expect(part2(input)).toBe(30055);
38+
});
39+
});
2140
});

src/2020/day7/test_input_2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
shiny gold bags contain 2 dark red bags.
2+
dark red bags contain 2 dark orange bags.
3+
dark orange bags contain 2 dark yellow bags.
4+
dark yellow bags contain 2 dark green bags.
5+
dark green bags contain 2 dark blue bags.
6+
dark blue bags contain 2 dark violet bags.
7+
dark violet bags contain no other bags.

0 commit comments

Comments
 (0)