Skip to content

Instantly share code, notes, and snippets.

@biancadanforth
Last active July 23, 2019 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biancadanforth/179b892e8737a0a762a8764e690f4659 to your computer and use it in GitHub Desktop.
Save biancadanforth/179b892e8737a0a762a8764e690f4659 to your computer and use it in GitHub Desktop.
Day 03 Advent of Code implemented in JS and Python with TDD (https://adventofcode.com/2015/day/3)
const fs = require("fs");
function main() {
const path = __dirname + "/input";
let input = fs.readFileSync(path, { encoding: "utf8" });
console.log("Day 3, part 1: ", part1(input)); // 2572
console.log("Day 3, part 2: ", part2(input)); // 2631
}
if (require.main === module) {
main();
}
function part1(input) {
const directions = parseInput(input); // an array of characters '^', 'v' '<', '>'
// Set of String (house coordinates)
const houses = new Set();
// Zeroth house gets a present
let [x, y] = [0, 0];
let houseCoords = [x, y].join(); // becomes `${x},${y}`
houses.add(houseCoords);
for (direction of directions) {
// Go to next house
switch (direction) {
case '^':
x--;
break;
case 'v':
x++;
break;
case '<':
y--;
break;
case '>':
y++;
break;
default:
throw new Error(`Unrecognized character ${direction}`);
}
houseCoords = [x, y].join();
houses.add(houseCoords);
}
return houses.size;
}
function part2(input) {
const directions = parseInput(input); // an array of characters '^', 'v' '<', '>'
// Set of String (house coordinates)
const houses = new Set();
// Zeroth house gets two presents
let [xSanta, ySanta, xRobo, yRobo] = [0, 0, 0, 0];
let houseCoords = [xSanta, ySanta].join(); // becomes `${xSanta},${ySanta}`
houses.add(houseCoords);
for (let i = 0; i < directions.length; i++) {
let [x, y] = (i % 2 === 0 ? [xRobo, yRobo] : [xSanta, ySanta]);
const direction = directions[i];
// Go to next house
switch (direction) {
case '^':
x--;
break;
case 'v':
x++;
break;
case '<':
y--;
break;
case '>':
y++;
break;
default:
throw new Error(`Unrecognized character ${direction}`);
}
if (i % 2 === 0) {
[xRobo, yRobo] = [x, y];
} else {
[xSanta, ySanta] = [x, y];
}
houseCoords = [x, y].join();
houses.add(houseCoords);
}
return houses.size;
}
/**
* @param {string} input - The puzzle input as a single line string
* @returns {Array<String>} An array of single character strings, one of '^', 'v'. '<' or '>'
*/
function parseInput(input) {
const directions = input.trim().split("\n");
// input file is only one line
return directions[0].split('');
}
module.exports = {
parseInput,
part1,
part2,
};
const { expect } = require("chai");
const { parseInput, part1, part2 } = require("./day03.js");
describe("day03", () => {
describe("parseInput", () => {
it("should work with example input", () => {
const input = "v>v<vv\n"; // random; first n chars in input
const expected = ['v', '>', 'v', '<', 'v', 'v'];
expect(parseInput(input)).to.deep.equal(expected);
})
});
describe("part1", () => {
it("should work for example 1", () => {
const input = ">";
const expected = 2;
expect(part1(input)).to.equal(expected);
});
it("should work for example 2", () => {
const input = "^>v<";
const expected = 4;
expect(part1(input)).to.equal(expected);
});
it("should work for example 3", () => {
const input = "^v^v^v^v^v";
const expected = 2;
expect(part1(input)).to.equal(expected);
});
});
describe("part2", () => {
it("should work for example 1", () => {
const input = "^v";
const expected = 3;
expect(part2(input)).to.equal(expected);
});
it("should work for example 2", () => {
const input = "^>v<";
const expected = 3;
expect(part2(input)).to.equal(expected);
});
it("should work for example 3", () => {
const input = "^v^v^v^v^v";
const expected = 11;
expect(part2(input)).to.equal(expected);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment