Skip to content

Commit

Permalink
refactor: 2015 Day 3
Browse files Browse the repository at this point in the history
Refactor day 3 of 2015
  • Loading branch information
marcobiedermann committed Apr 5, 2024
1 parent 49177b7 commit dbb86ac
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 72 deletions.
34 changes: 16 additions & 18 deletions 2015/day/3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@ import { part1, part2 } from '.';

const input = await readFile(`${__dirname}/input`, 'utf-8');

describe('2015 Day 3', () => {
describe('Part 1', () => {
it('should return number of unique locations visited', () => {
expect.assertions(4);

expect(part1('>')).toStrictEqual(2);
expect(part1('^>v<')).toStrictEqual(4);
expect(part1('^v^v^v^v^v')).toStrictEqual(2);
expect(part1(input)).toStrictEqual(2081);
describe('2015', () => {
describe('Day 3', () => {
describe('Part 1', () => {
it('should return number of unique locations visited', () => {
expect(part1('>')).toBe(2);
expect(part1('^>v<')).toBe(4);
expect(part1('^v^v^v^v^v')).toBe(2);
expect(part1(input)).toBe(2081);
});
});
});

describe('Part 2', () => {
it('should return number of unique locations visited by santa and robo-santa', () => {
expect.assertions(4);

expect(part2('^v')).toStrictEqual(3);
expect(part2('^>v<')).toStrictEqual(3);
expect(part2('^v^v^v^v^v')).toStrictEqual(11);
expect(part2(input)).toStrictEqual(2341);
describe('Part 2', () => {
it('should return number of unique locations visited by santa and robo-santa', () => {
expect(part2('^v')).toBe(3);
expect(part2('^>v<')).toBe(3);
expect(part2('^v^v^v^v^v')).toBe(11);
expect(part2(input)).toBe(2341);
});
});
});
});
92 changes: 38 additions & 54 deletions 2015/day/3/index.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,66 @@
const directions = new Map<string, [number, number]>([
type Direction = '^' | '<' | '>' | 'v';
type Move = [number, number];

const moves = new Map<Direction, Move>([
['^', [0, 1]],
['v', [0, -1]],
['<', [-1, 0]],
['>', [1, 0]],
['v', [0, -1]],
]);
const fallbackMove = [0, 0] as const;
const startLocation: Move = [0, 0];

function parseMoves(moves: string): string[] {
return moves.split('');
}
function part1(presents: string): number {
let santaLocation: Move = startLocation;

function part1(moves: string): number {
const parsedMoves = parseMoves(moves);
const start = [0, 0];
const set = new Set();
const houses = new Set([JSON.stringify(startLocation)]);

set.add(start.join(','));
for (let i = 0; i < presents.length; i += 1) {
const present = presents[i] as Direction;
const move = moves.get(present) || fallbackMove;
const newSantaLocation: Move = [santaLocation[0] + move[0], santaLocation[1] + move[1]];

parsedMoves.reduce((position, move) => {
const direction = directions.get(move) || [0, 0];
const newPosition = [position[0] + direction[0], position[1] + direction[1]];
houses.add(JSON.stringify(newSantaLocation));

set.add(newPosition.join(','));
santaLocation = newSantaLocation;
}

return newPosition;
}, start);

return set.size;
return houses.size;
}

function isEven(n: number): boolean {
return n % 2 === 0;
}

function part2(moves: string): number {
const parsedMoves = parseMoves(moves);
const set = new Set();

const start = [0, 0];

set.add(start.join(','));
function part2(presents: string): number {
let santaLocation: Move = startLocation;
let roboSantaLocation: Move = startLocation;

parsedMoves.reduce(
(positions, move, index) => {
const direction = directions.get(move) || [0, 0];
const houses = new Set([JSON.stringify(startLocation)]);

if (isEven(index)) {
const newSantaPosition = [
positions.santa[0] + direction[0],
positions.santa[1] + direction[1],
];
for (let i = 0; i < presents.length; i += 1) {
const present = presents[i] as Direction;
const move = moves.get(present) || fallbackMove;

set.add(newSantaPosition.join(','));
if (isEven(i)) {
const newSantaLocation: Move = [santaLocation[0] + move[0], santaLocation[1] + move[1]];

return {
...positions,
santa: newSantaPosition,
};
}
houses.add(JSON.stringify(newSantaLocation));

const newRobotPosition = [
positions.robot[0] + direction[0],
positions.robot[1] + direction[1],
santaLocation = newSantaLocation;
} else {
const newRoboSantaLocation: Move = [
roboSantaLocation[0] + move[0],
roboSantaLocation[1] + move[1],
];

set.add(newRobotPosition.join(','));
houses.add(JSON.stringify(newRoboSantaLocation));

return {
...positions,
robot: newRobotPosition,
};
},
{
santa: start,
robot: start,
},
);
roboSantaLocation = newRoboSantaLocation;
}
}

return set.size;
return houses.size;
}

export { part1, part2 };

0 comments on commit dbb86ac

Please sign in to comment.