Skip to content

Commit b0c763d

Browse files
authored
Merge pull request #6 from lundgren2/day7
Add day 7 2020
2 parents 3767b50 + 2084481 commit b0c763d

File tree

7 files changed

+713
-1
lines changed

7 files changed

+713
-1
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/input

Lines changed: 594 additions & 0 deletions
Large diffs are not rendered by default.

src/2020/day7/part1.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
type Bags = Record<string, string[]>;
2+
3+
const parseColor = (rule: string) => rule.replace(/\d|bag(s?.)?/g, '').trim();
4+
5+
export const parseBags = (input: string): Bags => {
6+
return input.split('\n').reduce((bags, r) => {
7+
const [, color, rules] = r.match(/([a-z]+ [a-z]+) bags contain (.*)/)!;
8+
return {...bags, [color]: rules.split(', ')};
9+
}, {});
10+
};
11+
12+
export const part1 = (input: string) => {
13+
const bags = parseBags(input);
14+
15+
const checkBag = (color: keyof Bags): boolean => {
16+
const rules = bags[color];
17+
18+
if (rules.join().includes('no other bags')) return false;
19+
if (rules.join().includes('shiny gold')) return true;
20+
21+
// Check if the other bags conain the target
22+
return !!rules.filter(rule => checkBag(parseColor(rule))).length;
23+
};
24+
25+
// Check all bags if valid
26+
const validBags = new Set();
27+
for (const bag in bags) checkBag(bag) && validBags.add(bag);
28+
return validBags.size;
29+
};

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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {readInput} from '../../utils';
2+
3+
import {part1} from './part1';
4+
import {part2} from './part2';
5+
6+
describe('Advent of Code 2020 - Day 7', () => {
7+
let testInput: string;
8+
let testInput2: string;
9+
let input: string;
10+
11+
beforeAll(async () => {
12+
testInput = await readInput(__dirname + '/test_input');
13+
testInput2 = await readInput(__dirname + '/test_input_2');
14+
input = await readInput(__dirname + '/input');
15+
});
16+
17+
describe('part 1', () => {
18+
it('should output 4', () => {
19+
expect(part1(testInput)).toBe(4);
20+
});
21+
22+
it('should output 289 valid bags from input', () => {
23+
expect(part1(input)).toBe(289);
24+
});
25+
});
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+
});
40+
});

src/2020/day7/test_input

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
light red bags contain 1 bright white bag, 2 muted yellow bags.
2+
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
3+
bright white bags contain 1 shiny gold bag.
4+
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
5+
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
6+
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
7+
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
8+
faded blue bags contain no other bags.
9+
dotted black bags contain no other bags.

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)