Skip to content

Commit

Permalink
feature(2018-day-18): solve part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kotlinski committed Jan 17, 2024
1 parent 93ab2d7 commit 9b1f7f1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 98 deletions.
2 changes: 1 addition & 1 deletion src/advent-of-code-solver/2023/day-18/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h2>--- Day 18: Lavaduct Lagoon ---</h2>

https://adventofcode.com/2023/day/18
https://adventofcode.com/2023/day/18
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('findArea', () => {
{ x: 7, y: 0 },
];
});
it('should return 14', () => {
it('should return 21', () => {
expect(findArea(coordinates)).toEqual(21);
});
});
Expand All @@ -39,7 +39,7 @@ describe('findArea', () => {
{ x: 7, y: 10 },
];
});
it('should return 18', () => {
it('should return 32', () => {
expect(findArea(coordinates)).toEqual(32);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ describe('day 18', () => {
describe('part one', () => {
describe('box', () => {
it(`should equal`, () => {
solver = new LavaductLagoonSolver('R 2 (#70c710)\n' + 'D 1 (#0dc571)\n' + 'L 2 (#5713f0)\n' + 'U 1 (#d2c081)\n');
solver = new LavaductLagoonSolver('R 1 (#70c710)\n' + 'D 1 (#0dc571)\n' + 'L 1 (#5713f0)\n' + 'U 1 (#d2c081)\n');
const result = solver.solvePartOne();
expect(result).toEqual(6);
expect(result).toEqual(4);
});
});
describe('sample-data', () => {
Expand Down
14 changes: 7 additions & 7 deletions src/advent-of-code-solver/2023/day-18/gauss-area-formula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import { Coordinate } from '../../common/matrix/interface';
* 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;
const shoelace = Math.abs(calculateSum(coordinates));
return shoelace / 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);
function calculateSum(coordinates: Coordinate[]) {
const sums = coordinates.map((coordinate, i) => {
const next_coordinate = nextCoordinate(coordinates, i);
return coordinate.x * next_coordinate.y - coordinate.y * next_coordinate.x;
});
return sums.reduce(summarize, 0);
}
29 changes: 29 additions & 0 deletions src/advent-of-code-solver/2023/day-18/input-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { DigInstruction } from './solver';
import { Coordinate } from '../../common/matrix/interface';

function instructionToCoordinate(last: Coordinate, instruction: DigInstruction /* , cycle_direction: boolean*/): Coordinate {
const { direction, steps } = instruction;
const coordinate_change = steps;
switch (direction) {
case 'up':
return { ...last, y: last.y - coordinate_change };
case 'down':
return { ...last, y: last.y + coordinate_change };
case 'left':
return { ...last, x: last.x - coordinate_change };
case 'right':
return { ...last, x: last.x + coordinate_change };
}
}

export function mapToCoordinates(instructions: DigInstruction[]) {
return instructions.reduce(
(coordinates: Coordinate[], instruction: DigInstruction, index: number) => {
const current = coordinates[index];
const next = instructionToCoordinate(current, instruction);
coordinates.push(next);
return coordinates;
},
[{ x: 0, y: 0 }],
);
}
57 changes: 0 additions & 57 deletions src/advent-of-code-solver/2023/day-18/map/input-parser.ts

This file was deleted.

46 changes: 17 additions & 29 deletions src/advent-of-code-solver/2023/day-18/solver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { findArea } from './gauss-area-formula';
import { mapToCoordinates } from './map/input-parser';
import { mapToCoordinates } from './input-parser';
import Solver from '../../../advent-of-code-solver/solver';
import { removeEmptyLinesPredicate } from '../../common/array-operations/filter';
import { summarize } from '../../common/array-operations/reduce';
Expand Down Expand Up @@ -31,10 +31,22 @@ export default class LavaductLagoonSolver extends Solver<DigInstruction[]> {
});
}
solvePartOne(): number {
const coordinates: Coordinate[] = mapToCoordinates(this.input);
console.log(`coordinates: ${JSON.stringify(coordinates, null, 2)}`);
return this.calculateArea(this.input);
}

/**
* The instructions -> coordinates won't be perfect for calculating area with Gauss formula,
* since 2 steps to the right and 1 step down from {x:0,y:0} will end up at {x:2,y:2},
* but the area of such instructions would actually be 2x3.
*
* To compensate for this,
* we calculate the perimeter and divide it by 2.
* +1 to reach the top of the first square meter.
*/
private calculateArea(instructions: DigInstruction[]) {
const coordinates: Coordinate[] = mapToCoordinates(instructions);
const area = findArea(coordinates);
const steps = this.input.map((instruction) => instruction.steps);
const steps = instructions.map((instruction) => instruction.steps);
const radius = steps.reduce(summarize, 0) / 2;
return area + radius + 1;
}
Expand All @@ -51,30 +63,6 @@ export default class LavaductLagoonSolver extends Solver<DigInstruction[]> {
steps,
};
});
const coordinates: Coordinate[] = mapToCoordinates(real_input);
// console.log(`coordinates: ${JSON.stringify(coordinates, null, 2)}`);
const area = findArea(coordinates);
const s = this.input.map((instruction) => instruction.steps);
const radius = s.reduce(summarize, 0) / 2;
return area + radius + 1;
/* ;
const pipe_input: MapTile[][] = formatToPipeMazeInput(real_input);
const pipe_maze = new PipeMaze(pipe_input);
this.printInput(pipe_input);
const loop_steps = pipe_maze.findNumberOfStepsInLoop();
const filled_area = pipe_maze.findInsideAreaOfLoop();
/!* console.log(`${loop_steps}`);
console.log(`${filled_area}`);*!/
return filled_area + loop_steps;*/
return this.calculateArea(real_input);
}
/* printInput(pipe_input: MapTile[][]) {
let output = '';
for (const row of pipe_input) {
for (const item of row) {
output += item;
}
output += '\n';
}
console.log(output);
}*/
}

0 comments on commit 9b1f7f1

Please sign in to comment.