-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
222 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/advent-of-code-solver/2023/day-18/__tests__/gauss-area-formula.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/advent-of-code-solver/2023/day-18/gauss-area-formula.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.