Skip to content

Commit

Permalink
Add solution for 2016 day 13 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Sep 7, 2019
1 parent 252ecf0 commit 8a642bc
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
37 changes: 37 additions & 0 deletions 2016/13/Maze.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Grid } from '../../utils';

const Type = {
OPEN_SPACE: true,
WALL: false,
};

class Maze extends Grid {
constructor(seed) {
super();
this.seed = seed;
}

getPoint(x, y) {
let point = super.getPoint(x, y);
if (!point) {
point = this.set(x, y);
}
return point;
}

set(x, y) {
const point = super.set(x, y);

const base = x * x + 3 * x + 2 * x * y + y + y * y;
const sum = base + this.seed;
const ones = sum.toString(2).split('').filter(char => char === '1');
const even = ones.length % 2 === 0;
const value = even ? Type.OPEN_SPACE : Type.WALL;

point.value = value;
return point;
}
}

export default Maze;
export { Type };
26 changes: 26 additions & 0 deletions 2016/13/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node --experimental-modules --no-warnings

import { day } from '..';
import { astar } from '../../utils';

import Maze, { Type } from './Maze';
import examples from './input/examples';
import puzzleInput from './input';

const traverse = (start, goal) => (
astar(start, goal, {
neighborsFor: point => point.adjacentNeighbors.filter(neighbor => (
neighbor.value === Type.OPEN_SPACE
)),
})
);

day(13).part(1).test(examples).feed(puzzleInput).solution((input, isExample) => {
const maze = new Maze(input);
const start = maze.getPoint(1, 1);
const goal = isExample ? maze.getPoint(7, 4) : maze.getPoint(31, 39);
return traverse(start, goal).score;
});

// day(13).part(2).test(examples).feed(null).solution((input) => {
// });
5 changes: 5 additions & 0 deletions 2016/13/input/examples.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { example } from '../../../utils';

export default [
example(10, 11),
];
1 change: 1 addition & 0 deletions 2016/13/input/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1350;
2 changes: 1 addition & 1 deletion utils/pathfinding.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// See: https://en.wikipedia.org/wiki/A*_search_algorithm#Pseudocode
// And: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Pseudocode
export const astar = (start, goal, { neighborsFor, cost }) => {
export const astar = (start, goal, { neighborsFor, cost = () => 1 }) => {
// For each node, which node it can most efficiently be reached from.
// If a node can be reached from many nodes, cameFrom will eventually contain the
// most efficient previous step.
Expand Down
2 changes: 1 addition & 1 deletion utils/solution.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Challenge {
const { answer, duration } = this.execute(test.input, true);
const expected = test.expected[offset];
const passed = answer === expected;
const excerpt = test.input.replace(/\t/g, ' ').replace(/\n/g, ' ').slice(0, 25);
const excerpt = String(test.input).replace(/\n|\t/g, ' ').slice(0, 25);
const text = passed ? answer : `${answer} (expected: ${expected})`;
line(`Example ${colors.yellow(excerpt)}`, text, passed, duration);
}
Expand Down

0 comments on commit 8a642bc

Please sign in to comment.