Skip to content

Commit

Permalink
refactor: 2015 Day 8
Browse files Browse the repository at this point in the history
Refactor part 1 of day 8 of 2015
  • Loading branch information
marcobiedermann committed Apr 6, 2024
1 parent bae5500 commit e344614
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
49 changes: 21 additions & 28 deletions 2015/day/1/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,29 @@ import { getInput } from '../../../utils/file';

const input = await getInput(__dirname);

describe('Day 1', () => {
describe('Part 1', () => {
it('should return floor number', () => {
expect.assertions(10);

expect(part1('(())')).toStrictEqual(0);
expect(part1('()()')).toStrictEqual(0);

expect(part1('(((')).toStrictEqual(3);
expect(part1('(()(()(')).toStrictEqual(3);

expect(part1('))(((((')).toStrictEqual(3);

expect(part1('())')).toStrictEqual(-1);
expect(part1('))(')).toStrictEqual(-1);

expect(part1(')))')).toStrictEqual(-3);
expect(part1(')())())')).toStrictEqual(-3);

expect(part1(input)).toStrictEqual(280);
describe('2015', () => {
describe('Day 1', () => {
describe('Part 1', () => {
it('should return floor number', () => {
expect(part1('(())')).toBe(0);
expect(part1('()()')).toBe(0);
expect(part1('(((')).toBe(3);
expect(part1('(()(()(')).toBe(3);
expect(part1('))(((((')).toBe(3);
expect(part1('())')).toBe(-1);
expect(part1('))(')).toBe(-1);
expect(part1(')))')).toBe(-3);
expect(part1(')())())')).toBe(-3);
expect(part1(input)).toBe(280);
});
});
});

describe('Part 2', () => {
it('should return index of the first character to enter the basement', () => {
expect.assertions(3);

expect(part2(')')).toStrictEqual(1);
expect(part2('()())')).toStrictEqual(5);
expect(part2(input)).toStrictEqual(1797);
describe('Part 2', () => {
it('should return index of the first character to enter the basement', () => {
expect(part2(')')).toBe(1);
expect(part2('()())')).toBe(5);
expect(part2(input)).toBe(1797);
});
});
});
});
19 changes: 10 additions & 9 deletions 2015/day/8/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { readFile } from 'node:fs/promises';
import { describe, expect, it } from 'vitest';
import { part1 } from '.';
import { getInput } from '../../../utils/file';
import { NEWLINE } from '../../../utils/string';

const input = (await readFile(`${__dirname}/input`, 'utf-8')).split('\n');
const input = (await getInput(__dirname)).split(NEWLINE);

describe('Day 8', () => {
describe('Part 1', () => {
it('should return the number of characters', () => {
expect.assertions(2);

expect(part1(['""', '"abc"', '"aaa\\"aaa"', '"\\x27"'])).toStrictEqual(12);
expect(part1(input)).toStrictEqual(1350);
describe('2015', () => {
describe('Day 8', () => {
describe('Part 1', () => {
it('should return the number of characters', () => {
expect(part1(['""', '"abc"', '"aaa\\"aaa"', '"\\x27"'])).toBe(12);
expect(part1(input)).toBe(1350);
});
});
});
});
18 changes: 14 additions & 4 deletions 2015/day/8/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
/* eslint-disable import/prefer-default-export, no-eval */

import { sum } from '../../../utils/math';

function mapString(str: string): number {
const { length: totalCharacters } = str;
const { length: totalCharactersInMemory } = eval(str);

return totalCharacters - totalCharactersInMemory;
}

function mapStrings(strs: string[]): number[] {
return strs.map(mapString);
}

function part1(input: string[]): number {
return input.reduce(
(accumulator, currentValue) => accumulator + currentValue.length - eval(currentValue).length,
0,
);
return sum(mapStrings(input));
}

export { part1 };

0 comments on commit e344614

Please sign in to comment.