diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..0f831c6ce 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -1,14 +1,20 @@ -// Fix this implementation -// Start by running the tests for this function -// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory - -// Hint: Please consider scenarios when 'list' isn't an array, is empty, -// or contains values that aren't numbers (the function is expected to throw - see the tests). - function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + if (!list.every((item) => typeof item === "number")) { + throw new Error("calculateMedian requires an array of numbers"); + } + const sorted = [...list].sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); + if (sorted.length % 2 === 0) { + return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + } + + return sorted[middleIndex]; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..b54117bd9 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,11 @@ -function dedupe() {} +function dedupe(list) { + const result = []; + for (const item of list) { + if (!result.includes(item)) { + result.push(item); + } + } + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..b20e1f851 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,28 +1,13 @@ const dedupe = require("./dedupe.js"); -/* -Dedupe Array -📖 Dedupe means **deduplicate** +test("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); +}); -In this kata, you will need to deduplicate the elements of an array +test("returns a a copy with no duplicates", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); +}); -E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c'] -E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] -E.g. dedupe([1, 2, 1]) returns [1, 2] -*/ - -// Acceptance Criteria: - -// Given an empty array -// When passed to the dedupe function -// Then it should return an empty array -test.todo("given an empty array, it returns an empty array"); - -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array - -// Given an array of strings or numbers -// When passed to the dedupe function -// Then it should return a new array with duplicates removed while preserving the -// first occurrence of each element from the original array. +test("keeps the first occurrence order", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..e6a0a4033 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,13 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + try { + const median = calculateMedian(list); + return "The median is " + median; + } catch (error) { + return "Could not calculate a median: " + error.message; + } +} module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..711d96791 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -1,32 +1,18 @@ -/* Describe the median, or explain why there isn't one - -calculateMedian is given to you at the top of describe-median.js. It throws -when it can't calculate a median. Don't change it. - -Implement describeMedian, which calls calculateMedian and returns a sentence -instead of crashing. - -E.g. describeMedian([1, 2, 3]), target output: "The median is 2" -E.g. describeMedian([]), target output: "Could not calculate a median: calculateMedian requires a non-empty array" - -You'll need a try/catch block for this. The catch block must use the error's -message, so the caller can see what went wrong. -*/ - const describeMedian = require("./describe-median.js"); -// Acceptance criteria: -// Given an array of numbers -// When passed to describeMedian -// Then it should return "The median is " followed by the median -// Delete this test.todo and replace it with a test. -test.todo('given [1, 2, 3], returns "The median is 2"'); +test('given [1, 2, 3], returns "The median is 2"', () => { + expect(describeMedian([1, 2, 3])).toBe("The median is 2"); +}); -// Given an empty array -// When passed to describeMedian -// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +test("returns a sentence for an empty array", () => { + expect(describeMedian([])).toBe( + "Could not calculate a median: calculateMedian requires a non-empty array" + ); +}); -// Given something that isn't an array of numbers, e.g. "banana" -// When passed to describeMedian -// Then it should return "Could not calculate a median: calculateMedian requires an array of numbers" +test("return a sentence for a non-array", () => { + expect(describeMedian("banana")).toBe( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..2d1147c06 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,23 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + if (elements.length === 0) { + return -Infinity; + } + + if (!elements.every((item) => typeof item === "number")) { + throw new Error("findMax requires an array of numbers"); + } + + let max = elements[0]; + for (const item of elements) { + if (item > max) { + max = item; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..61090ad8c 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -1,43 +1,33 @@ -/* Find the maximum element of an array of numbers - -In this kata, you will need to implement a function that find the largest numerical element of an array. - -E.g. max([30, 50, 10, 40]), target output: 50 -E.g. max(['hey', 10, 'hi', 60, 10]) throws Error("findMax requires an array of numbers") (max can't compare non-numerical elements, so it shouldn't guess) - -You should implement this function in max.js, and add tests for it in this file. - -We have set things up already so that this file can see your function from the other file. -*/ - const findMax = require("./max.js"); -// Given an empty array -// When passed to the max function -// Then it should return -Infinity -// Delete this test.todo and replace it with a test. -test.todo("given an empty array, returns -Infinity"); - -// Given an array with one number -// When passed to the max function -// Then it should return that number - -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall - -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero - -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number - -// Given an array containing a value that isn't a number -// When passed to the max function -// Then it should throw Error("findMax requires an array of numbers") - -// Given something that isn't an array at all, such as "hey", 42 or no argument -// When passed to the max function -// Then it should throw Error("findMax requires an array of numbers") +test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toBe(-Infinity); +}); + +test("given an array with one number, returns that number", () => { + expect(findMax([7])).toBe(7); +}); + +test("returns the largest of positive and negative numbers", () => { + expect(findMax([-6, 5, -3, 8])).toBe(8); +}); + +test("returns the closest to zero for all negatives", () => { + expect(findMax([-9, -2, -6])).toBe(-2); +}); + +test("returns the largest decimal number", () => { + expect(findMax([1.2, 3.8, 2.6])).toBe(3.8); +}); + +test("throws for an array containing a non-number", () => { + expect(() => findMax(["hey", 10])).toThrow( + new Error("findMax requires an array of numbers") + ); +}); + +test("throws when not given an array", () => { + expect(() => findMax("hey")).toThrow( + new Error("findMax requires an array of numbers") + ); +}); diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..340508a03 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,18 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + if (!list.every((item) => typeof item === "number")) { + throw new Error("calculateMean requires an array of numbers"); + } + let total = 0; + for (const item of list) { + total += item; + } + return total / list.length; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..d4a592e2d 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -1,50 +1,31 @@ -/* Calculate the mean of an array of numbers - -In this kata, you will need to implement a function that calculates the mean of an array of numbers. - -E.g. calculateMean([1, 2, 6]), target output: 3 -E.g. calculateMean([]) throws Error("calculateMean requires a non-empty array") -E.g. calculateMean("banana") throws Error("calculateMean requires an array of numbers") - -There is no mean of an empty array, and a string isn't an array of numbers, so -calculateMean should throw rather than return a value. This is the same problem -calculateMedian solves in the prep and in the fix directory. - -You should implement this function in mean.js, and add tests for it in this file. - -We have set things up already so that this file can see your function from the other file. -*/ - const calculateMean = require("./mean.js"); -// Acceptance criteria: - -// Given an array of numbers -// When passed to calculateMean -// Then it should return their mean -// Delete this test.todo and replace it with a test. -test.todo("given [1, 2, 6], returns 3"); - -// Given an array with a single number -// When passed to calculateMean -// Then it should return that number - -// Given an array containing negative or decimal numbers -// When passed to calculateMean -// Then it should return the correct mean - -// Given an empty array -// When passed to calculateMean -// Then it should throw Error("calculateMean requires a non-empty array") - -// Given a value that isn't an array, e.g. "banana", 42, null or {} -// When passed to calculateMean -// Then it should throw Error("calculateMean requires an array of numbers") - -// Given no argument at all -// When passed to calculateMean -// Then it should throw Error("calculateMean requires an array of numbers") - -// Given an array containing a non-number value, e.g. [1, "2", 3] -// When passed to calculateMean -// Then it should throw Error("calculateMean requires an array of numbers") +test("given [1, 2, 6], returns 3", () => { + expect(calculateMean([1, 2, 6])).toBe(3); +}); + +test("returns the number for a single item array", () => { + expect(calculateMean([8])).toBe(8); +}); + +test("handles negative and decimal numbers", () => { + expect(calculateMean([-2, 4, 3.5])).toBeCloseTo(1.8333); +}); + +test("throws for an empty array", () => { + expect(() => calculateMean([])).toThrow( + new Error("calculateMean requires a non-empty array") + ); +}); + +test("throws when not given an array", () => { + expect(() => calculateMean("banana")).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); + +test("throws for an array containing a non-number", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..6b578d356 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,15 @@ -function sum(elements) { +function sum(list) { + if (!Array.isArray(list)) { + throw new Error("sum requires an array of numbers"); + } + if (!list.every((item) => typeof item === "number")) { + throw new Error("sum requires an array of numbers"); + } + let total = 0; + for (const item of list) { + total += item; + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..55a2135ec 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -1,36 +1,27 @@ -/* Sum the numbers in an array - -In this kata, you will need to implement a function that sums the numerical elements of an array - -E.g. sum([10, 20, 30]), target output: 60 -E.g. sum(['hey', 10, 'hi', 60, 10]) throws Error("sum requires an array of numbers") (sum can't add non-numerical elements, so it shouldn't guess) -*/ - const sum = require("./sum.js"); -// Acceptance Criteria: - -// Given an empty array -// When passed to the sum function -// Then it should return 0 -test.todo("given an empty array, returns 0") +test("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); +}); -// Given an array with just one number -// When passed to the sum function -// Then it should return that number +test("given an array with one number, return that number", () => { + expect(sum([8])).toBe(8); +}); -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum +test("adds negative number correctly", () => { + expect(sum([-5, 3, -10])).toBe(-12); +}); -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return the correct total sum +test("adds decimal numbers correctly", () => { + expect(sum([1.5, 2.25, 3])).toBe(6.75); +}); -// Given an array containing a value that isn't a number -// When passed to the sum function -// Then it should throw Error("sum requires an array of numbers") +test("throws for an array containing a non-number", () => { + expect(() => sum(["hey", 10])).toThrow("sum requires an array of numbers"); +}); -// Given something that isn't an array at all, such as "hey", 42 or no argument -// When passed to the sum function -// Then it should throw Error("sum requires an array of numbers") +test("throws when not given an array", () => { + expect(() => sum("hey")).toThrow( + new Error("sum requires an array of numbers") + ); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,7 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (let index = 0; index < list.length; index++) { - const element = list[index]; + for (const element of list) { if (element === target) { return true; } diff --git a/Sprint-1/stretch/aoc-2018-day1/input.txt b/Sprint-1/stretch/aoc-2018-day1/input.txt index 1ffa65fc8..67857469c 100644 --- a/Sprint-1/stretch/aoc-2018-day1/input.txt +++ b/Sprint-1/stretch/aoc-2018-day1/input.txt @@ -1,951 +1,1037 @@ --10 --12 -+1 -+14 -+11 --19 --4 -+10 -+10 --12 --13 --10 --8 ++13 ++19 +11 -+3 --10 -+8 -+5 --14 ++15 ++1 +7 -+12 -+12 ++17 +14 --1 +-16 ++15 ++4 +-2 +-10 ++2 +17 ++18 -5 -+9 --15 -+8 +-18 +-18 -16 -+9 -+6 -+17 -11 -+19 -+11 +-9 -9 -1 --8 --16 --5 -+6 -+2 +6 +12 -+15 -+16 --6 --8 --5 ++14 ++3 ++7 +11 ++13 ++10 -9 -+19 -+19 --5 --12 --17 --20 -+9 --6 --2 -+20 -+15 --8 ++16 +15 -+19 --5 ++18 +-19 -17 --9 +1 --9 --11 --19 --1 --15 -+18 --4 +-7 ++11 +19 -+3 -+21 -+5 --1 -+8 -+9 -+9 --16 -+17 --15 -+18 -+14 -+8 --13 -2 --15 -+13 +-3 +19 --13 -+5 ++12 ++9 ++7 +16 ++1 ++5 +-11 +-7 +16 -+7 -+8 --19 +3 --12 -+18 --16 --19 ++12 +8 --16 -+20 -+15 --5 -17 -+15 -+14 --2 -+18 --6 -+5 -+16 -+13 -8 +-11 ++5 -9 --1 -+13 -+18 --2 -+16 --4 -+19 -+6 -+14 --6 --16 --10 --5 --15 -+11 +-18 +10 -+18 -+8 --2 -+15 --9 -+7 +-7 ++2 ++4 ++4 +10 -+9 --6 ++13 ++6 +1 --17 --12 -+19 +-19 ++16 +14 -9 --18 -+20 -+12 -+10 -+14 -+6 -+9 --18 -+4 +15 ++19 +-18 +15 +-10 +-7 ++16 +3 -+9 --4 --19 ++19 +-17 +8 -+16 +19 --12 --1 -+16 -+11 ++14 +-17 +-15 +-18 +-16 +13 --8 +-16 ++13 ++17 ++17 ++6 ++7 ++8 +-11 +-17 +4 -+18 +-12 +-17 ++20 ++19 ++8 +10 --11 ++9 ++21 ++17 ++12 +18 --8 -+19 --4 --5 --12 --3 --10 --11 --18 -7 ++19 +-8 +-19 +3 -+14 -+18 -+2 -7 --12 -+14 -+12 -+13 --1 -+13 --1 -+15 -+13 --1 --17 --4 -+17 +-2 -3 ++16 +13 -+5 +-16 +-16 -19 -+17 +10 -+6 --15 -+11 -+16 -+11 -+20 +-19 ++14 +-9 +-17 ++7 ++1 ++2 +19 -+5 --11 --2 --12 --6 --11 ++7 +-14 ++1 +-8 ++3 -14 -+5 -+10 --23 -+14 -11 -+22 --1 ++2 ++19 ++19 +-20 +-8 -14 --35 --23 -+11 -+4 +10 --6 ++2 ++16 ++15 ++8 +19 --5 -+4 +-14 ++16 +-10 ++1 ++14 ++18 -13 --7 -+11 --24 --17 -+9 --17 -+11 -+2 -4 --10 --24 ++12 ++8 +-1 +-18 ++5 ++1 +14 -+15 ++7 ++6 +19 +-16 ++19 ++13 ++5 ++10 +-12 +7 --2 -+11 --13 --8 --8 --14 ++7 ++20 ++12 -17 -+6 -+8 ++13 ++18 +-13 +-9 ++12 ++5 +-10 ++3 ++5 ++16 +-7 +-1 ++11 ++12 -16 ++7 ++13 ++9 -18 -+1 --21 --14 -+8 --2 -+20 --19 +-9 ++4 +-20 +9 --15 --6 --7 ++8 ++6 +4 --3 ++11 ++7 -8 -+16 -+15 --26 -+18 --22 --16 +-13 +-2 ++17 +-7 -3 --17 --18 -+4 ++11 ++12 ++3 +-7 ++5 +5 +3 --1 ++5 +3 -+2 +-10 ++11 +-8 +-5 +3 -+7 ++19 -5 -+20 --11 -+15 --6 --19 --24 ++4 ++10 -6 --20 -+13 --4 ++12 +-11 ++3 ++9 ++10 -18 +-19 ++7 +-11 +-8 +-12 +-9 +-7 +-17 +15 -+9 -+16 -+12 --5 +-16 +-13 +-10 +-8 +16 -+9 --4 ++18 +-10 ++1 -14 -9 +-14 ++11 -19 +-2 +-18 -5 --16 --16 -+5 --4 +-19 ++17 ++8 +12 ++12 ++6 -15 --15 --14 -+4 --18 --12 --4 +-1 ++2 ++20 +-8 +6 +-13 ++1 +9 --12 -+14 -+9 --6 -+3 -+8 -+4 -+7 --14 --15 --18 -+12 ++18 ++16 -19 --14 --15 --16 -+15 +-17 +-24 +-1 -8 -+14 --4 --21 -+7 -12 --2 --10 +-17 -6 -+14 -+14 --9 --14 -16 -+2 -+7 ++13 +-10 ++17 ++13 ++8 +-17 +5 +-11 ++9 ++19 ++18 ++17 -15 ++14 -15 -+19 --16 --5 --16 --19 -+9 --27 --8 +-17 +-17 ++19 +6 -+1 --14 --9 --50 +-13 -9 --21 -+5 ++1 -20 --19 -+6 --11 -+6 +-1 +-7 +-3 +1 ++14 -17 +-16 +-16 +-18 +-7 +-19 -14 --2 +-18 +8 --4 -+16 ++14 +1 --3 +-12 -9 -+20 ++13 ++9 +-10 ++6 +15 --22 ++19 ++11 +-9 -3 --13 --12 --25 -+5 --13 --2 --8 --11 -+17 ++8 +3 --7 -+13 -+1 -+18 --13 ++6 +1 -+7 --15 --43 --6 --10 --9 --20 ++6 +-27 ++17 ++24 -7 ++20 ++23 ++16 ++3 -8 --15 --9 -+13 ++20 ++10 +-5 +-4 -9 -+24 -+13 -+5 -8 +-11 +-31 +-10 ++13 -15 --4 --4 --4 --14 --21 -+16 -+15 --14 --20 --20 --15 --19 --41 ++29 ++1 +16 -+3 -+8 --22 --30 -+8 ++10 +40 -+15 -+24 --7 -+22 ++9 +10 -+15 ++3 ++13 ++4 ++10 ++29 ++6 +-11 +-5 ++32 +-14 ++5 ++13 +-17 +-8 +9 -+28 -+14 ++29 ++17 ++3 +2 ++13 +-19 +14 ++10 ++2 ++5 +-15 +-19 +-20 ++12 ++19 ++24 +6 -+17 +-23 ++14 +-18 +1 -+30 -+5 ++10 +-20 ++34 ++9 -2 -+46 -+24 -+46 --3 ++18 ++15 -7 -+35 ++19 +-13 ++7 ++16 ++12 +-11 +-15 +-6 ++19 +16 ++9 ++5 +13 --21 -+28 ++17 +13 --25 -+180 -+25 +-1 +-1 +-16 +-18 ++16 +5 --9 -+23 --5 --2 -+13 --12 ++21 +2 --25 --5 -+18 --24 -+45 -+17 --7 ++2 ++9 +-14 +22 --152 --11 -+4 -+50 -+129 --7 ++12 +-2 +-6 +22 -+109 --56 -+80 -+29 --75060 -2 --14 -+7 -17 --5 -+6 --3 -+18 -+19 ++2 +-14 +-9 ++16 ++25 +-7 ++34 ++5 -14 +-34 ++11 +-28 -12 --6 -+9 +-16 +-18 ++17 +-9 ++6 -5 +-19 +-17 +-16 ++2 +-8 ++11 -13 --6 --3 --15 -+13 -+3 --18 --11 -+16 --2 +-4 +10 -14 +-19 +-14 -7 -+19 --20 -+15 -+10 -+12 ++2 ++2 ++4 ++5 ++30 +-19 +10 -+16 --11 +-17 ++2 +-25 ++28 +-2 +-2 ++7 +-19 ++29 ++4 ++2 +14 --5 +-6 ++22 +14 -+11 --5 -+1 +-11 ++16 -13 -+1 -+18 +-15 ++4 +-9 +-20 +-36 +-17 +-7 -10 -+25 ++183 +-9 +13 +-108 ++34 +-138 +-65 +-42 +-4 ++10 ++410 ++75921 +-9 ++18 -17 -+15 --18 -+1 -6 --12 -+5 --7 -+18 ++15 ++6 ++12 +-13 +-4 +-8 +-16 ++15 -6 -18 --17 --5 --15 +-6 +-11 ++4 ++3 -1 --15 -+1 --12 -+9 +-9 +16 -+8 +5 +-6 +-4 +1 -+9 -+18 --13 --30 --27 --19 -+2 --16 --19 --10 --18 -+21 -+18 --5 +14 -+9 --6 -+2 ++20 -16 ++3 ++6 ++15 ++18 -11 ++15 +-14 +-14 +-8 ++9 +1 +-15 +-17 +18 --4 +-9 +-14 +19 -+7 -+6 +-1 ++10 ++14 -8 -+25 --50 -+6 --21 ++20 ++17 ++17 ++12 +-10 ++11 ++13 +-9 +6 --2 +14 --11 -+10 --9 -+16 -+25 --61 --17 --14 -+13 -+3 --14 +-16 +-10 -4 --1 -+9 --1 +-2 -4 --5 -+7 --15 -+7 -+16 -+6 ++8 ++13 ++13 +-17 ++9 -2 +-6 ++9 ++15 ++17 ++18 +-6 +-16 ++6 ++8 ++1 ++9 ++20 +-19 ++3 +-7 +-1 +-19 ++4 +-6 -11 --11 --13 -+11 -12 --13 --8 ++10 ++9 +3 --13 -+16 ++10 ++10 -1 ++18 +-8 -14 -+19 -+4 -+10 +-22 +12 -+26 -+6 -+4 -+24 --31 -+9 -10 --23 +-10 ++2 ++12 -13 -+3 --18 --9 --6 -+1 --2 +-7 +15 --10 --17 --17 +-3 ++7 +15 -+9 -+4 -+19 ++20 ++14 ++6 +-2 +-20 ++13 -4 -+12 ++18 +-2 +-8 +11 --25 +16 -+4 --2 -+39 -+49 -+9 -+96 -+4 ++8 +-9 +19 -+3 -+11 -+25 -+14 -+11 --5 --26 --8 -16 -+11 -+2 -+2 --1 ++9 ++19 ++7 +-13 ++7 -10 --45 --160 --31 --3 --43 +-14 -18 -+19 -+3 -+4 +-12 ++18 +11 +-15 +-3 ++16 +-8 ++18 +-9 ++20 -16 --19 --22 -+6 -+2 -+10 --11 --17 -+7 -+1 -+15 -+17 --19 -+6 --26 --7 --9 -+19 -+2 -+6 --22 -+2 +-6 +-16 +-2 +-10 +-8 -8 -+5 -+7 +9 -+8 --16 -+2 --19 +-15 ++13 +-20 +-7 +-5 +-13 -19 --3 -+16 -+5 -+4 --18 +-2 ++7 +-6 +-10 +-14 +-17 -11 --19 -+16 ++6 +-14 -15 ++24 ++11 +-10 +-16 ++10 +-1 ++14 +7 +-18 ++17 ++11 ++11 -1 --9 -19 +-10 +11 --17 --5 -+7 --11 -+16 +-15 ++11 +-4 +-16 ++10 +-15 ++24 ++6 ++6 +-26 -19 -+5 ++7 +21 --2 -+12 -+14 --7 -+18 ++22 ++26 +-18 ++8 ++34 +-12 -8 -+10 --11 --17 ++28 +-1 ++34 +-10 +3 --13 -+19 +-6 ++25 ++11 -3 -+10 ++20 ++9 ++8 +-14 ++16 +17 +-11 ++10 ++4 +-10 +-6 ++8 ++11 ++1 ++2 +-5 +-10 ++2 ++15 +-13 ++10 +17 --18 -+7 --2 --2 +-1 ++3 ++19 +-14 +12 --6 ++7 ++5 +-1 ++11 +19 --20 --15 --11 --27 -+8 +-12 ++16 +-7 +-18 ++12 +18 ++10 +-2 +-16 +-16 +-16 +-13 +9 --1 +-8 ++1 ++17 -2 -+19 -+7 ++5 ++9 +12 -+32 -+14 +-17 +13 --9 --14 -+20 ++1 ++14 +18 --4 --49 ++1 ++3 -6 +-5 ++1 +-17 +-13 +-9 +-15 +-23 -10 --7 ++1 ++13 ++4 +-5 +-2 ++9 +-1 +-10 +-15 -4 -9 ++17 ++1 -6 --37 ++15 ++8 ++1 -21 --2 +-4 ++3 +-18 ++20 +5 -+12 ++26 +4 --33 --4 --13 --4 -+13 --19 --7 --16 --1 --1 ++19 +8 --14 --15 -+11 ++19 ++20 +-9 -10 --14 --22 --11 -+10 --3 -+7 -+9 --15 +-12 +-12 +16 --7 --1 -+2 +-8 +-5 +-16 +-17 +20 -+11 --1 -+6 +-16 ++8 +-10 ++14 -2 ++15 ++22 ++4 +3 ++20 ++2 +3 ++16 ++14 +-3 ++5 ++12 +-7 +-18 +2 --20 --10 -+11 --10 -+13 --1 -+20 -+7 -+7 ++14 ++4 +-11 ++1 +5 ++6 +-4 ++16 ++11 +-14 ++18 +-7 ++14 +1 -+4 --13 -+15 +-19 -3 -+2 ++18 ++6 ++16 ++7 +-9 ++16 ++7 ++10 -4 --3 --1 -+13 +-4 +-12 -15 --14 --2 --6 -+3 +-16 ++7 ++19 ++11 ++18 ++7 +-12 +-12 ++20 +13 -19 --11 +-6 +-10 ++13 +-7 +-14 +-15 +-17 +-5 ++14 ++6 +8 --21 --2 --23 +-7 +-15 +-11 -18 -+20 -+7 -+6 --28 -+7 --33 -+13 +-16 -16 -18 --10 -18 ++7 ++22 +13 +-1 ++10 ++18 +-9 +-16 ++3 +-12 +-10 ++6 +8 --14 --13 ++14 +-19 -2 -+4 --14 ++13 ++5 ++8 ++7 ++5 ++5 ++7 ++17 +-4 -7 ++5 +-16 ++15 -18 --20 --4 --5 ++4 ++6 ++5 ++14 ++27 ++5 +-11 +-7 +-7 ++15 ++7 +-17 +8 +-14 +-3 ++2 ++25 ++12 +-6 +-17 ++7 +-8 ++23 -4 -+16 -+21 +20 ++18 ++4 +-9 ++3 +-20 -8 ++40 +-35 +-39 +-42 ++13 +-48 -14 --2 --14 --22 -+14 -+36 -+32 --28 -+1 ++7 ++19 -21 -+8 ++10 +-19 ++1 +-11 ++12 +-25 +-9 +-19 ++23 ++1 ++22 +-3 ++22 +-7 +14 -+75784 \ No newline at end of file +-25 ++66 +-8 ++104 ++41 ++35 ++1 ++11 +-48 +-38 ++44 +-64 ++259 +-17 ++19 +-16 ++12 ++16 +-77039 \ No newline at end of file diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..fe4019862 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,16 @@ +const fs = require("fs"); +const changes = fs.readFileSync("input.txt", "utf8").trim().split("\n").map(Number); + +let frequency = 0; + +const seen = new Set([0]); + +for (i = 0; ;i++) { + frequency += changes[i % changes.length]; + + if (seen.has(frequency)) { + console.log(frequency); + break; +} + seen.add(frequency) +}