Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/2020/_dayx/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {part1} from './part1';
import {readInput} from '../../utils';

import {part1} from './part1';

describe('Advent of Code 2020 - Day x', () => {
let input: string;
beforeAll(async () => {
Expand Down
594 changes: 594 additions & 0 deletions src/2020/day7/input

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/2020/day7/part1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
type Bags = Record<string, string[]>;

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

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

export const part1 = (input: string) => {
const bags = parseBags(input);

const checkBag = (color: keyof Bags): boolean => {
const rules = bags[color];

if (rules.join().includes('no other bags')) return false;
if (rules.join().includes('shiny gold')) return true;

// Check if the other bags conain the target
return !!rules.filter(rule => checkBag(parseColor(rule))).length;
};

// Check all bags if valid
const validBags = new Set();
for (const bag in bags) checkBag(bag) && validBags.add(bag);
return validBags.size;
};
32 changes: 32 additions & 0 deletions src/2020/day7/part2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {parseBags} from './part1';

const parseAmount = (rule: string) => Number(rule.match(/\d/));
const parseColor = (rule: string) => {
const color = rule.replace(/\d|bag(s?.?)?/g, '').trim();
return !color.includes('no other') ? color : '';
};

export const part2 = (input: string) => {
const bags = parseBags(input);
const bagsToCount = ['shiny gold'];
let sum = 0;

while (bagsToCount.length > 0) {
const bag = bagsToCount.pop()!;
const bagsCounter = bags[bag].reduce((a, rule) => {
const amount = parseAmount(rule);

bagsToCount.push(
...Array(amount)
.fill(parseColor(rule))
.filter(a => a) // remove '' (no other bags)
);

return a + amount;
}, 0);

sum += bagsCounter;
}

return sum;
};
40 changes: 40 additions & 0 deletions src/2020/day7/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {readInput} from '../../utils';

import {part1} from './part1';
import {part2} from './part2';

describe('Advent of Code 2020 - Day 7', () => {
let testInput: string;
let testInput2: string;
let input: string;

beforeAll(async () => {
testInput = await readInput(__dirname + '/test_input');
testInput2 = await readInput(__dirname + '/test_input_2');
input = await readInput(__dirname + '/input');
});

describe('part 1', () => {
it('should output 4', () => {
expect(part1(testInput)).toBe(4);
});

it('should output 289 valid bags from input', () => {
expect(part1(input)).toBe(289);
});
});

describe('part 2', () => {
it('should output 32 from test_input', () => {
expect(part2(testInput)).toBe(32);
});

it('should output 126 from test_input2', () => {
expect(part2(testInput2)).toBe(126);
});

it('should output 30055 valid bags from input', () => {
expect(part2(input)).toBe(30055);
});
});
});
9 changes: 9 additions & 0 deletions src/2020/day7/test_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.
7 changes: 7 additions & 0 deletions src/2020/day7/test_input_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
shiny gold bags contain 2 dark red bags.
dark red bags contain 2 dark orange bags.
dark orange bags contain 2 dark yellow bags.
dark yellow bags contain 2 dark green bags.
dark green bags contain 2 dark blue bags.
dark blue bags contain 2 dark violet bags.
dark violet bags contain no other bags.