diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..0b327cb54 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,30 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { + + 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"); + } + + for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMedian requires an array of numbers"); + } + } + + const sortedList = [...list].sort((a, b) => a - b); const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (list.length % 2 === 1) { + return sortedList[middleIndex]; + } + + const lowerMiddle = sortedList[middleIndex - 1]; + const upperMiddle = sortedList[middleIndex]; + return (lowerMiddle + upperMiddle) / 2; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..3bd620ed4 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,16 @@ -function dedupe() {} +function dedupe(originalArray) { + const uniqueItems = []; + + for (let i = 0; i < originalArray.length; i++) { + const currentItem = originalArray[i]; + const alreadyInArray = uniqueItems.includes(currentItem); + + if (alreadyInArray === false) { + uniqueItems.push(currentItem); + } + } + + return uniqueItems; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..9e8bcdc78 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,30 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // 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"); +test("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("given an array with no duplicates, it returns a copy of the original array", () => { + const input = [1, 2, 3, 4, 5]; + const result = dedupe(input); + + expect(result).toEqual([1, 2, 3, 4, 5]); + expect(result).not.toBe(input); +}); // 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("given an array of strings with duplicates, it removes them and keeps the first occurrence", () => { + expect(dedupe(["a", "a", "b", "b", "b", "c"])).toEqual(["a", "b", "c"]); +}); + +test("given an array of numbers with duplicates, it removes them and keeps the first occurrence", () => { + expect(dedupe([1, 2, 2, 3, 3, 3, 4, 4, 5, 5])).toEqual([1, 2, 3, 4, 5]); + expect(dedupe([5, 5, 2, 1, 1,])).toEqual([5, 2, 1]); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..492dba77c 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..7d4ee8f1c 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,20 @@ const describeMedian = require("./describe-median.js"); // 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('given an empty array, returns "Could not calculate a median: calculateMedian requires a non-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('given something that is not an array, returns "Could not calculate a median: calculateMedian requires an array of numbers"', () => { + 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..e991aea97 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,20 @@ function findMax(elements) { + if(!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + let max = -Infinity; + + for (const element of elements) { + if (typeof element !== "number" || Number.isNaN(element)) { + throw new Error("findMax requires an array of numbers"); + } + + if (element > max) { + max = element; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..2066115ea 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,50 @@ const findMax = require("./max.js"); // 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"); +test("given an empty array, returns -Infinity", ()=> { + expect(findMax([])).toBe(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("given an array with one number, returns that number", () => { + expect(findMax([7])).toBe(7); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("given positive and negative numbers, return the largest overall", () => { + expect(findMax([-5, 10, -20, 3])).toBe(10); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given only negative numbers, returns the one closest to zero", () => { + expect(findMax([-8, -2, -30])).toBe(-2); +}) // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given decimal numbers, returns the largest decimal", () => { + expect(findMax([1.5, 2.7, 2.65])).toBe(2.7); +}) // 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") +test("given an array with a non-number, throws an error", () => { + expect(() => findMax(["hey", 10, "hi", 60, 10])).toThrow("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 something that is not an array, throws an error", () => { + expect(() => findMax("hey")).toThrow("findMax requires an array of numbers"); + expect(() => findMax(42)).toThrow("findMax requires an array of numbers"); + expect(() => findMax()).toThrow("findMax requires an array of numbers"); +}); diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..86d340ecd 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,22 @@ -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"); + } + + let total = 0; + + for (let i = 0; i < list.length; i++) { + if (typeof list[i] !== "number") { + throw new Error("calculateMean requires an array of numbers"); + } + total += list[i]; + } + + 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..c3723cb90 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -22,29 +22,65 @@ const calculateMean = require("./mean.js"); // 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"); +test("given [1, 2, 6], returns 3", () => { + expect(calculateMean([1, 2, 6])).toBe(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +test("given [7], return 7", () => { + expect(calculateMean([7])).toBe(7); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +test("given an array of negative numbers, return the correct mean", () => { + expect(calculateMean([-1, -3, -5])).toBe(-3); +}); + +test("given an array of decimal numbers, return the correct mean", () => { + expect(calculateMean([1.5, 2.5])).toBe(2); + expect(calculateMean([0.1, 0.2])).toBeCloseTo(0.15); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +test("given an empty array, throws an error", () => { + expect(() => calculateMean([])).toThrow("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") +test("given a value that is not an array, throws an error", () => { + expect(() => calculateMean("banana")).toThrow("calculateMean requires an array of numbers"); +}); + +test("given a value that is not an array, throws an error", () => { + expect(() => calculateMean(42)).toThrow("calculateMean requires an array of numbers"); +}); + +test("given a value that is not an array, throws an error", () => { + expect(() => calculateMean(null)).toThrow("calculateMean requires an array of numbers"); +}); + +test("given a value that is not an array, throws an error", () => { + expect(() => calculateMean({})).toThrow("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") +test("given no argument at all, throws an error", () => { + expect(() => calculateMean()).toThrow("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 an array with a non-number, throws an error", () => { + expect(() => calculateMean([7, "2", 5])).toThrow("calculateMean requires an array of numbers"); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..b76d7e492 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,18 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + let total = 0; + + for (const element of elements) { + if (typeof element !== "number") { + throw new Error("sum requires an array of numbers"); + } + total += element; + } + + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..af3587116 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,45 @@ const sum = require("./sum.js"); // 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 just one number, returns that number", () => { + expect(sum([7])).toBe(7); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given an array with negative numbers, returns the correct total", () => { + expect(sum([10, -4, -6])).toBe(0); + expect(sum([-1, -2, -3])).toBe(-6); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given an array with decimal numbers, returns the correct total", () => { + expect(sum([1.5, 2.5, 3])).toBe(7); + expect(sum([0.1, 0.2])).toBeCloseTo(0.3); +}); // 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("given an array with a non-number, throws an error", () => { + expect(() => sum(["hey", 10, "hi", 60, 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("given something that is not an array, throws an error", () => { + expect(() => sum("hey")).toThrow("sum requires an array of numbers"); + expect(() => sum(42)).toThrow("sum requires an array of numbers"); + expect(() => sum()).toThrow("sum requires an array of numbers"); +}); \ No newline at end of file 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..80150a795 100644 --- a/Sprint-1/stretch/aoc-2018-day1/input.txt +++ b/Sprint-1/stretch/aoc-2018-day1/input.txt @@ -1,951 +1,993 @@ ++3 ++15 +-1 +-18 ++3 -10 --12 -+1 -+14 -+11 --19 --4 -+10 ++3 +10 --12 --13 --10 -8 +-20 ++13 +11 -+3 +-21 -10 -+8 -+5 --14 -+7 -+12 -+12 -+14 --1 -+17 --5 -+9 --15 -+8 -16 -+9 -+6 -+17 --11 +-2 +-6 +19 -+11 --9 --1 -8 --16 --5 -+6 -+2 -+6 +12 -+15 -+16 --6 --8 --5 -+11 +-13 ++4 -9 -+19 -+19 --5 --12 --17 -20 -+9 --6 --2 -+20 -+15 --8 ++12 +15 -+19 --5 +-17 +-18 ++11 -17 -9 -+1 +-12 -9 --11 --19 --1 --15 -+18 --4 -+19 +3 -+21 -+5 --1 -+8 -+9 +9 --16 -+17 --15 +-6 +18 -+14 -+8 --13 --2 --15 -+13 -+19 --13 -+5 -+16 +16 -+7 -+8 --19 -+3 --12 -+18 --16 --19 ++9 +-11 +-1 +8 +-20 ++17 +-3 -16 -+20 -+15 --5 --17 +-7 ++10 ++6 ++3 +15 -+14 ++3 -2 -+18 ++9 +-8 -6 ++11 +5 -+16 -+13 ++24 ++10 ++4 -8 --9 --1 ++2 ++13 +-16 +13 -+18 --2 -+16 --4 -+19 -+6 -+14 --6 -16 --10 --5 --15 +11 -+10 -+18 -+8 --2 -+15 --9 +7 -+10 -+9 --6 -+1 --17 --12 -+19 -+14 -9 --18 -+20 -+12 -+10 -+14 -+6 -+9 --18 -+4 -+15 -+15 -+3 -+9 --4 --19 -+8 -+16 -+19 +-13 -12 ++17 +-4 +-5 -1 +16 -+11 -+13 --8 ++22 ++8 ++12 ++9 ++2 +-3 +-5 ++12 +4 ++4 ++8 +18 -+10 ++9 +-14 -11 -+18 --8 -+19 --4 -5 --12 --3 --10 -11 ++15 -18 -7 -+3 -+14 -+18 -+2 --7 -12 -+14 -+12 -+13 --1 -+13 --1 -+15 -+13 --1 --17 --4 -+17 --3 -+13 ++1 +5 --19 +-18 ++14 +-13 +-10 +17 -+10 -+6 --15 -+11 -+16 ++8 +11 +-12 +20 -+19 +-16 ++22 +5 --11 --2 --12 --6 --11 --14 ++1 +-9 ++16 ++13 ++6 +5 -+10 --23 -+14 --11 -+22 --1 --14 --35 --23 -+11 ++7 +-4 +-4 +-7 ++16 +4 -+10 --6 +-1 +-15 +-11 +19 --5 -+4 --13 ++9 ++17 +-15 ++21 -7 ++4 +-16 +-3 +-9 ++4 +11 --24 --17 -+9 --17 ++12 +11 -+2 --4 --10 --24 ++7 ++12 +14 +-6 +-1 +-18 +-3 +-13 ++4 ++10 +-15 +-10 +-8 ++15 +15 +19 ++16 +-3 +-14 ++13 ++16 ++1 ++10 ++9 +7 +-11 +-19 ++2 +-3 ++10 -2 -+11 ++13 +-19 +-7 ++3 ++18 ++1 ++9 ++2 ++12 -13 +-9 +-7 +-15 +-18 +-6 +-1 -8 --8 +-12 ++19 +-1 -14 --17 -+6 -+8 +-6 -16 --18 +-4 +1 --21 --14 -+8 --2 -+20 ++4 +-22 -19 -+9 --15 --6 --7 +-2 +4 --3 ++18 -8 +-19 +-16 ++6 +16 +-7 ++4 ++8 +-20 +-11 +-11 ++20 +15 --26 -+18 --22 --16 --3 --17 ++19 ++30 ++19 ++19 +-11 -18 +4 -+5 -+3 ++1 ++21 +-9 +-9 ++12 ++18 -1 -+3 -+2 -+3 -+7 --5 -+20 --11 -+15 --6 --19 --24 -6 --20 -+13 --4 --18 -+15 -+9 -+16 +12 --5 +16 -+9 --4 --14 --9 --19 --5 --16 --16 -+5 --4 +12 ++3 ++1 +-9 -15 -15 --14 -+4 +-1 +-1 -18 --12 --4 +-32 +-11 +-17 +-20 ++4 ++14 +-1 ++16 ++20 ++53 ++19 ++16 ++14 ++14 +6 -+9 --12 +-8 +14 -+9 --6 +-17 +-5 +3 -+8 -+4 -+7 --14 ++14 +-3 -15 --18 -+12 +-10 -19 --14 --15 --16 -+15 --8 ++17 ++3 ++17 ++5 +14 --4 --21 -+7 --12 +-7 ++5 ++14 ++18 -2 --10 +-19 ++8 ++10 ++15 -6 -+14 -+14 --9 --14 --16 -+2 -+7 -+5 ++13 +-11 ++19 ++10 -15 -15 -+19 --16 --5 --16 --19 ++2 +-4 +-3 ++11 ++2 ++18 ++3 ++17 +9 --27 +-16 -8 +6 ++15 +-11 +-3 ++20 +-1 ++12 +1 --14 --9 --50 --9 --21 -+5 --20 --19 -+6 --11 -+6 -+1 --17 --14 --2 +8 --4 ++11 +-8 ++6 ++7 ++17 +-12 ++13 +-8 +16 -+1 --3 --9 -+20 +15 --22 --3 --13 +-8 -12 --25 ++7 +-12 +-18 +5 --13 --2 ++5 ++7 -8 --11 ++6 +-4 +-15 +17 -+3 --7 -+13 -+1 -+18 --13 -+1 -+7 -15 --43 --6 -10 +-1 ++17 ++18 +-21 +-19 -9 --20 --7 --8 ++19 ++16 ++18 +-5 -15 ++19 +-11 ++10 ++9 +-1 ++4 ++4 ++9 +-5 ++20 ++4 +-18 ++17 ++12 +-18 ++14 +-21 +-15 +-5 +-18 +-17 +-19 ++11 ++12 +-20 +-19 ++12 -9 -+13 --9 -+24 -+13 ++8 +-1 +-22 ++4 ++15 ++14 ++9 +-10 +-26 +-22 +-10 ++7 ++9 +-1 +-16 +-21 +-18 ++8 ++6 ++18 ++1 +5 ++11 ++18 ++6 ++20 +-7 +-10 +-5 ++33 +-21 -8 --15 --4 --4 --4 +-13 +-5 ++7 ++1 +-7 +-39 -14 --21 +-12 +16 -+15 -14 --20 --20 -15 --19 --41 ++4 ++2 ++12 +-11 +16 -+3 +8 --22 --30 -+8 -+40 +15 -+24 +-1 ++21 +-5 ++13 +-18 -7 -+22 -+10 -+15 -+9 -+28 -+14 +2 -+14 +-15 ++19 ++8 ++26 +-24 ++37 ++59 ++21 +-19 ++18 +6 -+17 -+1 -+30 +-11 ++15 +-2 +5 -2 -+46 -+24 -+46 --3 ++20 ++8 ++2 -7 -+35 ++18 +-8 +-1 +-8 ++3 ++17 ++12 ++18 ++2 +-8 ++3 ++40 ++3 ++21 ++5 +-1 ++15 ++2 +16 -+13 --21 -+28 -+13 --25 -+180 -+25 +-50 ++15 +5 ++21 +-31 +-17 +-15 +-10 -9 -+23 --5 -2 +-18 +-14 ++7 +-14 ++6 +-20 +-8 +13 --12 +-27 ++8 +2 --25 --5 -+18 --24 -+45 -+17 --7 -+22 --152 --11 -+4 -+50 -+129 +-16 ++3 -7 -+22 -+109 --56 -+80 -+29 --75060 +-6 ++23 ++31 ++52 +-6 +-1 ++2 +-54 +-42 +-6 ++18 ++2 ++1 +-29 ++36 +-42 ++7 +-146 -2 -14 -+7 --17 --5 -+6 +-13 ++19 +-8 ++19 +-15 +-18 +-4 +-18 +-1 ++2 -3 ++11 +-16 +-23 +-9 +-30 +-31 ++58 +18 +-19 ++45 ++11 ++14 +-16 +19 --14 +-10 ++17 ++12 +-2 +-8 ++19 +-50 ++8 +-114 +-40 +-62 +-153 +-60 ++12 +-23 +-40 +-87 ++377 +-79652 -12 --6 -+9 ++1 +-9 -5 --13 --6 +-12 +-14 ++9 -3 +-2 ++8 -15 -+13 -+3 -18 --11 -+16 --2 -+10 --14 --7 +19 --20 -+15 -+10 -+12 -+10 -+16 --11 -+14 --5 -+14 +-6 ++4 +-14 +-2 +-18 +-19 +-8 ++2 +11 --5 -+1 --13 +-19 +-11 ++5 +-3 ++4 +1 -+18 --10 -+25 -+13 --17 +-3 ++9 ++7 +15 --18 -+1 --6 --12 -+5 --7 -+18 +-9 +-2 -6 --18 --17 ++3 ++2 ++10 ++9 ++3 -5 +-4 ++12 ++15 +-6 +-11 +-9 -15 --1 --15 -+1 --12 -+9 -+16 -+8 -+5 -+1 +9 -+18 --13 --30 --27 -19 -+2 +-15 ++4 ++7 ++7 -16 ++15 +-4 -19 --10 +-8 +-11 -18 -+21 -+18 --5 -+14 -+9 --6 +-14 +-18 +-16 ++5 ++5 ++12 +-9 +-16 +2 +-6 +-2 ++20 +-13 +-9 -16 ++4 +-1 ++2 +-3 +-17 +-2 -11 +1 -+18 --4 -+19 -+7 +6 --8 -+25 --50 ++17 +-4 +-15 +-3 +-19 +6 --21 +-16 +-18 +-6 ++2 ++12 +-9 +6 --2 -+14 --11 -+10 ++7 +-6 ++7 -9 -+16 -+25 --61 +-18 +-3 ++10 -17 --14 ++11 ++16 ++16 ++6 +13 +-17 ++2 +3 --14 --4 --1 -+9 ++17 +-10 ++8 -1 --4 --5 -+7 +-9 -15 -+7 -+16 -+6 --2 --11 --11 --13 +11 ++2 +-14 ++7 -12 --13 --8 +-19 +-3 +-7 +-7 +-15 +-17 ++5 +3 --13 -+16 --1 --14 -+19 -+4 -+10 -+12 -+26 ++14 +6 -+4 -+24 --31 -+9 --10 --23 --13 -+3 --18 +-5 ++17 -9 --6 +-5 +1 --2 -+15 --10 --17 --17 -+15 -+9 -+4 -+19 --4 -+12 -+11 --25 +-8 +-13 ++7 ++17 +-12 +-14 +-18 ++10 +16 -+4 --2 -+39 -+49 -+9 -+96 -+4 -+19 -+3 -+11 -+25 +-3 +-19 ++7 ++14 +14 +11 --5 --26 --8 --16 -+11 -+2 -+2 -1 --10 --45 --160 --31 --3 --43 --18 -+19 -+3 -+4 -+11 ++18 -16 --19 --22 -+6 -+2 +-4 +10 --11 +-14 ++9 -17 -+7 +-1 ++4 +-14 +-16 +1 -+15 -+17 --19 -+6 --26 +-14 -7 --9 ++1 ++7 +-11 +-4 +-14 +19 +-11 ++8 ++14 ++8 +-15 +-18 +2 +-17 +-19 ++13 ++3 +6 --22 -+2 --8 -+5 ++8 +-10 +7 -+9 +-3 +-9 +8 ++12 +-4 ++15 ++7 ++10 ++11 +-10 +-20 -16 -+2 --19 ++11 +-1 +-21 +-13 -19 --3 +-15 +-15 +-7 ++10 ++19 +-17 +-6 +-10 +-13 ++12 +-18 ++16 ++10 +-13 ++12 +-1 ++12 ++12 ++10 +16 -+5 -+4 -18 --11 --19 +-5 +-10 +-13 +-7 ++19 ++14 ++18 +-2 +-4 +-13 ++14 ++18 +-7 ++11 +-13 +-21 +-12 ++14 +16 --15 +7 --1 --9 -19 -+11 --17 --5 ++16 +7 --11 ++2 +-5 ++1 ++19 +16 --19 -+5 -+21 --2 ++14 ++9 +12 +14 --7 -+18 -8 -+10 --11 +-28 ++13 ++11 ++13 -17 -+3 --13 +-21 ++15 +-37 +-11 +19 --3 -+10 -+17 -+17 --18 -+7 --2 --2 ++1 ++8 +12 --6 -+19 --20 +-1 +-27 +-21 +-19 +-21 ++15 +-24 +-7 +-11 +-17 -15 -11 --27 +-11 +8 -+18 -+9 --1 --2 -+19 -+7 -+12 -+32 -+14 -+13 -9 --14 -+20 -+18 +-9 -4 --49 -6 ++15 -10 --7 --4 --9 --6 --37 --21 --2 -+5 -+12 -+4 --33 --4 --13 --4 -+13 --19 --7 --16 --1 --1 +-3 ++19 ++9 +8 ++6 ++11 +-4 -14 +-1 ++3 -15 -+11 ++17 ++18 -10 --14 -22 +-15 ++14 ++6 +-14 -11 -+10 --3 -+7 ++4 +-18 ++5 +-16 +-9 +-13 ++14 ++3 +9 --15 -+16 --7 ++17 +-9 ++22 +-12 +-8 +-5 -1 -+2 +-6 +-6 +-19 ++17 ++9 ++28 +20 ++14 +11 --1 +6 ++7 -2 -+3 -+3 ++17 ++17 +-12 ++7 ++21 +-2 +-4 ++10 +2 ++12 +-19 +-22 +-8 -20 --10 -+11 --10 -+13 ++25 +-17 +-15 +-8 -1 -+20 -+7 -+7 -+5 -+1 -+4 --13 ++13 +-15 ++16 +15 --3 -+2 ++19 +-128 +-4 +-16 -4 --3 --1 +13 --15 --14 --2 --6 -+3 +-10 ++20 +-16 +-13 ++15 ++9 ++1 +-11 +-16 +13 --19 ++1 ++15 ++1 +-31 ++11 +-53 +-11 +-9 +-16 -11 -+8 --21 --2 --23 --18 -+20 +7 +6 --28 -+7 --33 +13 ++6 +-12 +-3 ++16 ++2 +-5 ++7 -16 --18 --10 --18 -+13 -+8 --14 ++17 -13 --2 -+4 --14 --7 --18 --20 --4 --5 +-9 ++15 +-3 ++13 +8 --4 -+16 -+21 -+20 ++5 -8 --14 +-1 +-5 +-21 ++25 ++23 ++12 ++1 -2 --14 --22 ++7 +-25 +-51 +-5 ++4 +14 -+36 -+32 --28 -+1 +-8 ++4 ++11 +-5 -21 +-14 ++9 +8 -+14 -+75784 \ No newline at end of file +-13 ++4 ++80915 \ 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..7e120a061 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,19 @@ +const fs = require("fs"); +const input = fs.readFileSync("input.txt", "utf8"); +const changes = input.trim().split("\n").map(Number); + +let frequency = 0; +const seen = new Set([0]); + +while (true) { + for (const change of changes) { + frequency += change; + if (seen.has(frequency)) { + console.log(frequency); + process.exit(0); + } + seen.add(frequency); + } +} + +console.log(total);