diff --git a/docs/README.md b/docs/README.md index d6cd26c4e..007869388 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,7 +25,7 @@ slug. | [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) | |:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:| -| 0 | 1 | 2 | 26 | 48 | 439 | 603 | 226 | 56 | 82 | +| 0 | 1 | 2 | 26 | 48 | 440 | 603 | 226 | 56 | 82 | **Note:** The source code is written in Java 17 and may use language features that are incompatible with Java 8, 11. diff --git a/kata/6-kyu/death-by-coffee/README.md b/kata/6-kyu/death-by-coffee/README.md new file mode 100644 index 000000000..25a13b632 --- /dev/null +++ b/kata/6-kyu/death-by-coffee/README.md @@ -0,0 +1,64 @@ +# [Death by Coffee](https://www.codewars.com/kata/death-by-coffee "https://www.codewars.com/kata/57db78d3b43dfab59c001abe") + +## Background + +I drink too much coffee. Eventually it will probably kill me. + +*Or will it...?* + +Anyway, there's no way to know. + +*Or is there...?* + +## The Discovery of the Formula + +I proudly announce my discovery of a formula for measuring the life-span of coffee drinkers! + +For + +* ```h``` is a health number assigned to each person (8 digit date of birth YYYYMMDD) +* ```CAFE``` is a cup of *regular* coffee +* ```DECAF``` is a cup of *decaffeinated* coffee + +To determine the life-time coffee limits: + +* Drink cups of coffee (i.e. add to ```h```) until any part of the health number includes `DEAD` +* If the test subject can survive drinking five thousand cups without being ```DEAD``` then coffee has no ill effect on them + +# Kata Task + +Given the test subject's date of birth, return an array describing their life-time coffee limits + +```[ regular limit , decaffeinated limit ]``` + +## Notes + +* The limits are ```0``` if the subject is unaffected as described above + +* At least 1 cup must be consumed (Just thinking about coffee cannot kill you!) + +# Examples + +* John was born 19/Jan/1950 so ```h=19500119```. His coffee limits are ```[2645, 1162]``` which is only about 1 cup per week. You + better cut back John...How are you feeling? You don't look so well. + + +* Susan (11/Dec/1965) is unaffected by decaffeinated coffee, but regular coffee is very bad for her ```[111, 0]```. Just stick to the + decaf Susan. + + +* Elizabeth (28/Nov/1964) is allergic to decaffeinated coffee. Dead after only 11 cups ```[0, 11]```. Read the label carefully Lizzy! + Is it worth the risk? + + +* Peter (4/Sep/1965) can drink as much coffee as he likes ```[0, 0]```. You're a legend Peter!! + +
+ +Hint: https://en.wikipedia.org/wiki/Hexadecimal + +
+ +
+*Note: A life-span prediction formula this accurate has got to be worth a lot of money to somebody! I am willing to sell my copyright to the highest bidder. Contact me via the discourse section of this Kata.* +
\ No newline at end of file diff --git a/kata/6-kyu/death-by-coffee/main/Dinglemouse.java b/kata/6-kyu/death-by-coffee/main/Dinglemouse.java new file mode 100644 index 000000000..92202f35a --- /dev/null +++ b/kata/6-kyu/death-by-coffee/main/Dinglemouse.java @@ -0,0 +1,14 @@ +import static java.util.stream.IntStream.range; + +import java.util.function.ToIntBiFunction; + +interface Dinglemouse { + static int[] coffeeLimits(int year, int month, int day) { + ToIntBiFunction limit = (h, c) -> range(1, 5000) + .filter(i -> Integer.toHexString(h[0] += c).contains("dead")).findFirst().orElse(0); + return new int[]{ + limit.applyAsInt(new int[]{10000 * year + 100 * month + day}, 0xCAFE), + limit.applyAsInt(new int[]{10000 * year + 100 * month + day}, 0xDECAF) + }; + } +} \ No newline at end of file diff --git a/kata/6-kyu/death-by-coffee/test/DinglemouseTest.java b/kata/6-kyu/death-by-coffee/test/DinglemouseTest.java new file mode 100644 index 000000000..16a356447 --- /dev/null +++ b/kata/6-kyu/death-by-coffee/test/DinglemouseTest.java @@ -0,0 +1,18 @@ +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class DinglemouseTest { + @ParameterizedTest + @CsvSource(textBlock = """ + 1965, 9, 4, 0, 0 + 1964, 11, 28, 0, 11 + 1965, 12, 11, 111, 0 + 1950, 1, 19, 2645, 1162 + 1880, 3, 1, 0, 3909 + """) + void sample(int y, int m, int d, int cafe, int decaf) { + assertArrayEquals(new int[]{cafe, decaf}, Dinglemouse.coffeeLimits(y, m, d)); + } +} \ No newline at end of file diff --git a/kata/6-kyu/index.md b/kata/6-kyu/index.md index 8ec44c89d..a2a589c1f 100644 --- a/kata/6-kyu/index.md +++ b/kata/6-kyu/index.md @@ -106,6 +106,7 @@ - [Data compression using run-length encoding](data-compression-using-run-length-encoding) - [Data Reverse](data-reverse) - [Dead Ants](dead-ants) +- [Death by Coffee](death-by-coffee) - [Decode the Morse code](decode-the-morse-code) - [Decode the woofs!](decode-the-woofs) - [Delete occurrences of an element if it occurs more than n times](delete-occurrences-of-an-element-if-it-occurs-more-than-n-times)