diff --git a/.gitignore b/.gitignore index c2658d7..5387f1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules/ +spec/*.js +day*.js \ No newline at end of file diff --git a/day1.js b/day1.js deleted file mode 100644 index 4cbbe73..0000000 --- a/day1.js +++ /dev/null @@ -1,51 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.driver = exports.ex2core = exports.ex1core = void 0; -var fs = __importStar(require("fs")); -function ex1core(input) { - for (var i = 0; i < input.length - 1; i++) - for (var j = i + 1; j < input.length; j++) - if (input[i] + input[j] === 2020) { - return input[i] * input[j]; - } - return 514579; -} -exports.ex1core = ex1core; -// i j k -// [1, 2, 3] -function ex2core(input) { - for (var i = 0; i < input.length - 2; i++) - for (var j = i + 1; j < input.length - 1; j++) - for (var k = j + 1; k < input.length; k++) - if (input[i] + input[j] + input[k] === 2020) { - return input[i] * input[j] * input[k]; - } - return 514579; -} -exports.ex2core = ex2core; -function driver(exercise, readFile, output) { - var content = readFile(); - var input = content.split("\n").map(function (x) { return +x.trim(); }); - output("" + exercise(input)); -} -exports.driver = driver; -driver(ex2core, function () { return "" + fs.readFileSync("ex1.txt"); }, function (s) { return console.log(s); }); diff --git a/day2.js b/day2.js deleted file mode 100644 index 7cf5c4b..0000000 --- a/day2.js +++ /dev/null @@ -1,70 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.driver = exports.ex2 = exports.ex1 = void 0; -var fs = __importStar(require("fs")); -function isValidExercise1(conf) { - var count = 0; - for (var i = 0; i < conf.password.length; i++) { - if (conf.password.charAt(i) === conf.policy.char) { - count++; - } - } - return conf.policy.min <= count && count <= conf.policy.max; -} -function xor(a, b) { - return (a && !b) || (!a && b); -} -function isValidExercise2(conf) { - return xor(conf.password.charAt(conf.policy.min - 1) === conf.policy.char, conf.password.charAt(conf.policy.max - 1) === conf.policy.char); -} -function core(validator, input) { - var valid = 0; - for (var i = 0; i < input.length; i++) - if (validator(input[i])) { - valid++; - } - return valid; -} -function ex1(input) { - return core(isValidExercise1, input); -} -exports.ex1 = ex1; -function ex2(input) { - return core(isValidExercise2, input); -} -exports.ex2 = ex2; -function parse(line) { - var parts = line.split(": "); - var policyParts = parts[0].split(" "); - var rangeParts = policyParts[0].split("-"); - return { - policy: { min: +rangeParts[0], max: +rangeParts[1], char: policyParts[1] }, - password: parts[1], - }; -} -function driver(validator, readFile, output) { - var content = readFile(); - var input = content.split("\n").map(parse); - output("" + core(validator, input)); -} -exports.driver = driver; -driver(isValidExercise2, function () { return "" + fs.readFileSync("ex2.txt"); }, function (s) { return console.log(s); }); diff --git a/day3.js b/day3.js deleted file mode 100644 index 9b421a8..0000000 --- a/day3.js +++ /dev/null @@ -1,53 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.ex2 = exports.ex1 = void 0; -var fs = __importStar(require("fs")); -function core(sx, sy, map) { - var x = 0; - var y = 0; - var count = 0; - while (y < map.length) { - if (map[y][x] === "#") - count++; - x += sx; - while (x >= map[y].length) - x -= map[y].length; - y += sy; - } - return count; -} -function ex1(map) { - return core(3, 1, map); -} -exports.ex1 = ex1; -function ex2(map) { - return (core(1, 1, map) * - core(3, 1, map) * - core(5, 1, map) * - core(7, 1, map) * - core(1, 2, map)); -} -exports.ex2 = ex2; -var map = ("" + fs.readFileSync("ex3.txt")) - .split("\n") - .map(function (x) { return x.trim().split(""); }); -console.log(ex2(map)); diff --git a/spec/day1spec.js b/spec/day1spec.js deleted file mode 100644 index 35a507c..0000000 --- a/spec/day1spec.js +++ /dev/null @@ -1,45 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var day1_1 = require("../day1"); -describe("Exercise 1 core", function () { - it("works for given example", function () { - var input = [1721, 979, 366, 299, 675, 1456]; - var result = day1_1.ex1core(input); - expect(result).toBe(514579); - }); - it("works for minimal example", function () { - var input = [2019, 1]; - var result = day1_1.ex1core(input); - expect(result).toBe(2019); - }); -}); -describe("Exercise 1", function () { - it("works for given example", function () { - var input = function () { return "1721\n979\n366\n299\n675\n1456"; }; - var out = ""; - var output = function (s) { return (out += s); }; - day1_1.driver(day1_1.ex1core, input, output); - expect(out).toBe("514579"); - }); -}); -describe("Exercise 2", function () { - it("works for given example", function () { - var input = function () { return "1721\n979\n366\n299\n675\n1456"; }; - var out = ""; - var output = function (s) { return (out += s); }; - day1_1.driver(day1_1.ex2core, input, output); - expect(out).toBe("241861950"); - }); -}); -describe("Exercise 2 core", function () { - it("works for given example", function () { - var input = [1721, 979, 366, 299, 675, 1456]; - var result = day1_1.ex2core(input); - expect(result).toBe(241861950); - }); - it("works for minimal example", function () { - var input = [2018, 1, 1]; - var result = day1_1.ex2core(input); - expect(result).toBe(2018); - }); -}); diff --git a/spec/day2spec.js b/spec/day2spec.js deleted file mode 100644 index f87c5dc..0000000 --- a/spec/day2spec.js +++ /dev/null @@ -1,31 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var day2_1 = require("../day2"); -describe("Exercise 1 core", function () { - it("works on given example", function () { - var input = [ - { policy: { min: 1, max: 3, char: "a" }, password: "abcde" }, - { policy: { min: 1, max: 3, char: "b" }, password: "cdefg" }, - { policy: { min: 2, max: 9, char: "c" }, password: "ccccccccc" }, - ]; - expect(day2_1.ex1(input)).toBe(2); - }); - it("works on another example", function () { - var input = [{ policy: { min: 3, max: 3, char: "h" }, password: "hhh" }]; - expect(day2_1.ex1(input)).toBe(1); - }); -}); -describe("Exercise 1 core", function () { - it("works on given example", function () { - var input = [ - { policy: { min: 1, max: 3, char: "a" }, password: "abcde" }, - { policy: { min: 1, max: 3, char: "b" }, password: "cdefg" }, - { policy: { min: 2, max: 9, char: "c" }, password: "ccccccccc" }, - ]; - expect(day2_1.ex2(input)).toBe(1); - }); - it("works on another example", function () { - var input = [{ policy: { min: 3, max: 3, char: "h" }, password: "hhh" }]; - expect(day2_1.ex2(input)).toBe(0); - }); -}); diff --git a/spec/day3spec.js b/spec/day3spec.js deleted file mode 100644 index 7eaa24a..0000000 --- a/spec/day3spec.js +++ /dev/null @@ -1,19 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var day3_1 = require("../day3"); -describe("Exercise 1", function () { - it("works with given example", function () { - var map = "..##.......\n#...#...#..\n.#....#..#.\n..#.#...#.#\n.#...##..#.\n..#.##.....\n.#.#.#....#\n.#........#\n#.##...#...\n#...##....#\n.#..#...#.#" - .split("\n") - .map(function (x) { return x.trim().split(""); }); - expect(day3_1.ex1(map)).toBe(7); - }); -}); -describe("Exercise 2", function () { - it("works with given example", function () { - var map = "..##.......\n#...#...#..\n.#....#..#.\n..#.#...#.#\n.#...##..#.\n..#.##.....\n.#.#.#....#\n.#........#\n#.##...#...\n#...##....#\n.#..#...#.#" - .split("\n") - .map(function (x) { return x.trim().split(""); }); - expect(day3_1.ex2(map)).toBe(336); - }); -});