Skip to content

Commit

Permalink
solved part 1 with gaus
Browse files Browse the repository at this point in the history
  • Loading branch information
kotlinski committed Jan 17, 2024
1 parent 80581d7 commit 93ab2d7
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 222 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Coordinate } from '../../../common/matrix/interface';
import { findArea } from '../gauss-area-formula';

describe('findArea', () => {
let coordinates: Coordinate[];
describe('a four corner polygon starting at x:0, y:0', () => {
beforeEach(() => {
coordinates = [
{ x: 0, y: 0 },
{ x: 0, y: 3 },
{ x: 7, y: 3 },
{ x: 7, y: 0 },
];
});
it('should return 14', () => {
expect(findArea(coordinates)).toEqual(21);
});
});
describe('an offset four corner polygon', () => {
beforeEach(() => {
coordinates = [
{ x: 1, y: 0 },
{ x: 7, y: 0 },
{ x: 7, y: 3 },
{ x: 1, y: 3 },
];
});
it('should return 18', () => {
expect(findArea(coordinates)).toEqual(18);
});
});
describe('a five corner polygon', () => {
beforeEach(() => {
coordinates = [
{ x: 2, y: 7 },
{ x: 10, y: 1 },
{ x: 8, y: 6 },
{ x: 11, y: 7 },
{ x: 7, y: 10 },
];
});
it('should return 18', () => {
expect(findArea(coordinates)).toEqual(32);
});
});
});
99 changes: 54 additions & 45 deletions src/advent-of-code-solver/2023/day-18/__tests__/solver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,60 @@ import LavaductLagoonSolver, { DigInstruction } from '../solver';
describe('day 18', () => {
let solver: Solver<DigInstruction[]>;
describe('part one', () => {
type TestCase = { input: string; output: number };
const cases: TestCase[] = [
{
input:
'R 6 (#70c710)\n' +
'D 5 (#0dc571)\n' +
'L 2 (#5713f0)\n' +
'D 2 (#d2c081)\n' +
'R 2 (#59c680)\n' +
'D 2 (#411b91)\n' +
'L 5 (#8ceee2)\n' +
'U 2 (#caa173)\n' +
'L 1 (#1b58a2)\n' +
'U 2 (#caa171)\n' +
'R 2 (#7807d2)\n' +
'U 3 (#a77fa3)\n' +
'L 2 (#015232)\n' +
'U 2 (#7a21e3)\n',
output: 62,
},
{
input:
'U 1 (#70c710)\n' +
'R 6 (#70c710)\n' +
'D 6 (#0dc571)\n' +
'L 2 (#5713f0)\n' +
'D 2 (#d2c081)\n' +
'R 2 (#59c680)\n' +
'D 2 (#411b91)\n' +
'L 5 (#8ceee2)\n' +
'U 2 (#caa173)\n' +
'L 1 (#1b58a2)\n' +
'U 2 (#caa171)\n' +
'R 2 (#7807d2)\n' +
'U 3 (#a77fa3)\n' +
'L 2 (#015232)\n' +
'U 2 (#7a21e3)\n',
output: 69,
},
];
describe.each(cases)('with input $input', ({ input, output }: TestCase) => {
it(`should equal to ${output}`, () => {
solver = new LavaductLagoonSolver(input);
describe('box', () => {
it(`should equal`, () => {
solver = new LavaductLagoonSolver('R 2 (#70c710)\n' + 'D 1 (#0dc571)\n' + 'L 2 (#5713f0)\n' + 'U 1 (#d2c081)\n');
const result = solver.solvePartOne();
expect(result).toEqual(output);
expect(result).toEqual(6);
});
});
describe('sample-data', () => {
type TestCase = { input: string; output: number };
const cases: TestCase[] = [
{
input:
'R 6 (#70c710)\n' +
'D 5 (#0dc571)\n' +
'L 2 (#5713f0)\n' +
'D 2 (#d2c081)\n' +
'R 2 (#59c680)\n' +
'D 2 (#411b91)\n' +
'L 5 (#8ceee2)\n' +
'U 2 (#caa173)\n' +
'L 1 (#1b58a2)\n' +
'U 2 (#caa171)\n' +
'R 2 (#7807d2)\n' +
'U 3 (#a77fa3)\n' +
'L 2 (#015232)\n' +
'U 2 (#7a21e3)\n',
output: 62,
},
{
input:
'U 1 (#70c710)\n' +
'R 6 (#70c710)\n' +
'D 6 (#0dc571)\n' +
'L 2 (#5713f0)\n' +
'D 2 (#d2c081)\n' +
'R 2 (#59c680)\n' +
'D 2 (#411b91)\n' +
'L 5 (#8ceee2)\n' +
'U 2 (#caa173)\n' +
'L 1 (#1b58a2)\n' +
'U 2 (#caa171)\n' +
'R 2 (#7807d2)\n' +
'U 3 (#a77fa3)\n' +
'L 2 (#015232)\n' +
'U 2 (#7a21e3)\n',
output: 69,
},
];
describe.each(cases)('with input $input', ({ input, output }: TestCase) => {
it(`should equal to ${output}`, () => {
solver = new LavaductLagoonSolver(input);
const result = solver.solvePartOne();
expect(result).toEqual(output);
});
});
});
});
Expand All @@ -70,7 +79,7 @@ describe('day 18', () => {
'R 2 (#7807d2)\n' +
'U 3 (#a77fa3)\n' +
'L 2 (#015232)\n' +
'U 2 (#7a21e3)\n',
'U 2 (#7a21e3)',
output: 952408144115,
},
];
Expand Down
22 changes: 22 additions & 0 deletions src/advent-of-code-solver/2023/day-18/gauss-area-formula.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { summarize } from '../../common/array-operations/reduce';
import { Coordinate } from '../../common/matrix/interface';

/**
* Gauss area formula aka the Shoelace formula
* https://en.wikipedia.org/wiki/Shoelace_formula
*/
export function findArea(coordinates: Coordinate[]): number {
const shoelace_a = calculateSum(coordinates, (coordinate, i) => coordinate.x * nextCoordinate(coordinates, i).y);
const shoelace_b = calculateSum(coordinates, (coordinate, i) => coordinate.y * nextCoordinate(coordinates, i).x);
console.log(`shoelace_a: ${shoelace_a}`);
console.log(`shoelace_b: ${shoelace_b}`);
return Math.abs(shoelace_a - shoelace_b) / 2;
}
function nextCoordinate(coordinates: Coordinate[], i: number) {
return coordinates[(i + 1) % coordinates.length];
}

function calculateSum(coordinates: Coordinate[], mapFn: (_coordinate: Coordinate, index: number) => number) {
const sums = coordinates.map(mapFn);
return sums.reduce(summarize, 0);
}
Loading

0 comments on commit 93ab2d7

Please sign in to comment.