From efbb21a6de1042103b3170b112900c08a17677db Mon Sep 17 00:00:00 2001 From: "Capt. Cutlass" <5120290+ParanoidUser@users.noreply.github.com> Date: Wed, 3 Jul 2024 21:51:18 -0400 Subject: [PATCH] feat(7-kyu): kata/blowing-birthday-candles (#593) --- docs/README.md | 2 +- kata/7-kyu/blowing-birthday-candles/README.md | 60 +++++++++++++++++++ .../blowing-birthday-candles/main/Kata.java | 12 ++++ .../test/SolutionTest.java | 18 ++++++ kata/7-kyu/index.md | 1 + 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 kata/7-kyu/blowing-birthday-candles/README.md create mode 100644 kata/7-kyu/blowing-birthday-candles/main/Kata.java create mode 100644 kata/7-kyu/blowing-birthday-candles/test/SolutionTest.java diff --git a/docs/README.md b/docs/README.md index 23ab95eb8..d998e48c8 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 | 426 | 569 | 210 | 56 | 79 | +| 0 | 1 | 2 | 26 | 48 | 426 | 570 | 210 | 56 | 79 | **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/7-kyu/blowing-birthday-candles/README.md b/kata/7-kyu/blowing-birthday-candles/README.md new file mode 100644 index 000000000..855407d49 --- /dev/null +++ b/kata/7-kyu/blowing-birthday-candles/README.md @@ -0,0 +1,60 @@ +# [Blowing Birthday Candles](https://www.codewars.com/kata/blowing-birthday-candles "https://www.codewars.com/kata/6630da20f925eb3007c5a498") + +Today is the special day you've been waiting for — it's your birthday! It's 8 AM, and you're setting up your birthday +cake for the party. It's time to put the candles on top. + +You take out all the candles you've bought. As you are about to put them on the cake, you just realize that there +are numbers on each candle. What are these numbers?! After searching about it on the internet, turns out these are +special candles. These candles need to be blown a certain number of times before they're finally extinguished, and those +numbers on the candles are the required times to blow them. + +Being one who plans meticulously, you want to determine the total number of blows you need to extinguish all the candles +once you've put them on the cake. + +## Task + +Given a string containing digits (representing the strength of the candles), calculate the number of blows you need to +extinguish all the candles. + +Starting at the beginning of the string, each blow can only reach 3 candles, reducing +their strength by one each. You can only reach more candles once those directly in front of you are extinguished. + +## Examples + +``` +Input: \"1321\" + +Move 1 | \"(132)1\" -> \"0211\" +Move 2 | \"0(211)\" -> \"0100\" +Move 3 | \"0(100)\" -> \"0000\" + +This should return 3. +``` + +``` +Input: \"0323456\" + +Move 1 | \"0(323)456\" -> \"0212456\" +Move 2 | \"0(212)456\" -> \"0101456\" +Move 3 | \"0(101)456\" -> \"0000456\" +Move 4 | \"0000(456)\" -> \"0000345\" +Move 5 | \"0000(345)\" -> \"0000234\" +Move 6 | \"0000(234)\" -> \"0000123\" +Move 7 | \"0000(123)\" -> \"0000012\" +Move 8 | \"00000(12)\" -> \"0000001\" +Move 9 | \"000000(1)\" -> \"0000000\" + +This should return 9. +``` + +``` +Input: \"2113\" + +Move 1 | \"(211)3\" -> \"1003\" +Move 2 | \"(100)3\" -> \"0003\" +Move 3 | \"000(3)\" -> \"0002\" +Move 4 | \"000(2)\" -> \"0001\" +Move 5 | \"000(1)\" -> \"0000\" + +This should return 5. +``` \ No newline at end of file diff --git a/kata/7-kyu/blowing-birthday-candles/main/Kata.java b/kata/7-kyu/blowing-birthday-candles/main/Kata.java new file mode 100644 index 000000000..504a63602 --- /dev/null +++ b/kata/7-kyu/blowing-birthday-candles/main/Kata.java @@ -0,0 +1,12 @@ +interface Kata { + static int blowCandles(String str) { + int[] blows = new int[4]; + for (char c : str.toCharArray()) { + blows[0] = Math.max(c - 48 - blows[1] - blows[2], 0); + blows[2] = blows[1]; + blows[1] = blows[0]; + blows[3] += blows[0]; + } + return blows[3]; + } +} \ No newline at end of file diff --git a/kata/7-kyu/blowing-birthday-candles/test/SolutionTest.java b/kata/7-kyu/blowing-birthday-candles/test/SolutionTest.java new file mode 100644 index 000000000..362783adb --- /dev/null +++ b/kata/7-kyu/blowing-birthday-candles/test/SolutionTest.java @@ -0,0 +1,18 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SolutionTest { + @ParameterizedTest + @CsvSource(textBlock = """ + 1321, 3 + 0323456, 9 + 2113, 5 + 1110, 1 + 121, 2 + """) + void sample(String str, int expected) { + assertEquals(expected, Kata.blowCandles(str)); + } +} \ No newline at end of file diff --git a/kata/7-kyu/index.md b/kata/7-kyu/index.md index 5a9bda62a..999d192a0 100644 --- a/kata/7-kyu/index.md +++ b/kata/7-kyu/index.md @@ -56,6 +56,7 @@ - [Binary sXORe](binary-sxore) - [Bingo (Or Not)](bingo-or-not) - [Bits Battle](bits-battle) +- [Blowing Birthday Candles](blowing-birthday-candles) - [Breaking chocolate problem](breaking-chocolate-problem) - [Broken sequence](broken-sequence) - [Bubblesort Once](bubblesort-once)