diff --git a/config.json b/config.json index bbc8a52..de4606e 100644 --- a/config.json +++ b/config.json @@ -114,6 +114,19 @@ ], "difficulty": 1 }, + { + "slug": "eliuds-eggs", + "name": "Eliuds Eggs", + "uuid": "0875c5f0-8336-494b-b32a-b9d789d32ea7", + "practices": [ + "loops", + "numbers" + ], + "prerequisites": [ + "basics" + ], + "difficulty": 1 + }, { "slug": "darts", "name": "Darts", diff --git a/exercises/practice/eliuds-eggs/.docs/instructions.md b/exercises/practice/eliuds-eggs/.docs/instructions.md new file mode 100644 index 0000000..b0c2df5 --- /dev/null +++ b/exercises/practice/eliuds-eggs/.docs/instructions.md @@ -0,0 +1,8 @@ +# Instructions + +Your task is to count the number of 1 bits in the binary representation of a number. + +## Restrictions + +Keep your hands off that bit-count functionality provided by your standard library! +Solve this one yourself using other basic tools instead. diff --git a/exercises/practice/eliuds-eggs/.docs/introduction.md b/exercises/practice/eliuds-eggs/.docs/introduction.md new file mode 100644 index 0000000..2b2e5c4 --- /dev/null +++ b/exercises/practice/eliuds-eggs/.docs/introduction.md @@ -0,0 +1,65 @@ +# Introduction + +Your friend Eliud inherited a farm from her grandma Tigist. +Her granny was an inventor and had a tendency to build things in an overly complicated manner. +The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up. + +Eliud is asking you to write a program that shows the actual number of eggs in the coop. + +The position information encoding is calculated as follows: + +1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot. +2. Convert the number from binary to decimal. +3. Show the result on the display. + +## Example 1 + +![Seven individual nest boxes arranged in a row whose first, third, fourth and seventh nests each have a single egg.](https://assets.exercism.org/images/exercises/eliuds-eggs/example-1-coop.svg) + +```text + _ _ _ _ _ _ _ +|E| |E|E| | |E| +``` + +### Resulting Binary + +![1011001](https://assets.exercism.org/images/exercises/eliuds-eggs/example-1-binary.svg) + +```text + _ _ _ _ _ _ _ +|1|0|1|1|0|0|1| +``` + +### Decimal number on the display + +89 + +### Actual eggs in the coop + +4 + +## Example 2 + +![Seven individual nest boxes arranged in a row where only the fourth nest has an egg.](https://assets.exercism.org/images/exercises/eliuds-eggs/example-2-coop.svg) + +```text + _ _ _ _ _ _ _ +| | | |E| | | | +``` + +### Resulting Binary + +![0001000](https://assets.exercism.org/images/exercises/eliuds-eggs/example-2-binary.svg) + +```text + _ _ _ _ _ _ _ +|0|0|0|1|0|0|0| +``` + +### Decimal number on the display + +8 + +### Actual eggs in the coop + +1 diff --git a/exercises/practice/eliuds-eggs/.meta/Example.bat b/exercises/practice/eliuds-eggs/.meta/Example.bat new file mode 100644 index 0000000..8e3dd07 --- /dev/null +++ b/exercises/practice/eliuds-eggs/.meta/Example.bat @@ -0,0 +1,15 @@ +@echo off +setlocal enabledelayedexpansion + +set /a n=%~1 +set /a count=0 + +:loop +if %n% EQU 0 goto :done +set /a bit=n %% 2 +set /a count+=bit +set /a n=n/2 +goto :loop + +:done +echo %count% diff --git a/exercises/practice/eliuds-eggs/.meta/config.json b/exercises/practice/eliuds-eggs/.meta/config.json new file mode 100644 index 0000000..26b7452 --- /dev/null +++ b/exercises/practice/eliuds-eggs/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "nastamattina" + ], + "files": { + "solution": [ + "EliudsEggs.bat" + ], + "test": [ + "EliudsEggsTest.bat" + ], + "example": [ + ".meta/Example.bat" + ] + }, + "blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.", + "source": "Christian Willner, Eric Willigers", + "source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5" +} diff --git a/exercises/practice/eliuds-eggs/.meta/tests.toml b/exercises/practice/eliuds-eggs/.meta/tests.toml new file mode 100644 index 0000000..e11683c --- /dev/null +++ b/exercises/practice/eliuds-eggs/.meta/tests.toml @@ -0,0 +1,22 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[559e789d-07d1-4422-9004-3b699f83bca3] +description = "0 eggs" + +[97223282-f71e-490c-92f0-b3ec9e275aba] +description = "1 egg" + +[1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5] +description = "4 eggs" + +[0c18be92-a498-4ef2-bcbb-28ac4b06cb81] +description = "13 eggs" diff --git a/exercises/practice/eliuds-eggs/EliudsEggs.bat b/exercises/practice/eliuds-eggs/EliudsEggs.bat new file mode 100644 index 0000000..11d9ee1 --- /dev/null +++ b/exercises/practice/eliuds-eggs/EliudsEggs.bat @@ -0,0 +1,10 @@ +@echo off +setlocal enabledelayedexpansion + +set /a n=%~1 +set "result=" + +REM Your code goes here + + +echo %result% diff --git a/exercises/practice/eliuds-eggs/EliudsEggsTest.bat b/exercises/practice/eliuds-eggs/EliudsEggsTest.bat new file mode 100644 index 0000000..76d950b --- /dev/null +++ b/exercises/practice/eliuds-eggs/EliudsEggsTest.bat @@ -0,0 +1,120 @@ +@echo off +setlocal enabledelayedexpansion +REM --------------------------------------------------- +REM EliudsEggs Unit Testing +REM +REM sUnit Testing Framework version: 0.3 +REM --------------------------------------------------- + +set isTestRunner=false +if "%1" == "test-runner" ( + set isTestRunner=true +) + +set "successCount=0" +set "failCount=0" + +:Main + REM Initalize result variable + set "slug=EliudsEggs" + + REM -------------------- + REM Test Case Start \/\/ + REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/eliuds-eggs/canonical-data.json + REM -------------------- + + set "expected=0" + set "if_success=Test passed" + set "if_failed=Test failed: 0 eggs." + CALL :Assert "0" + + set "expected=1" + set "if_success=Test passed" + set "if_failed=Test failed: 1 egg." + CALL :Assert "16" + + set "expected=4" + set "if_success=Test passed" + set "if_failed=Test failed: 4 eggs." + CALL :Assert "89" + + set "expected=13" + set "if_success=Test passed" + set "if_failed=Test failed: 13 eggs." + CALL :Assert "2000000000" + + REM -------------------- + REM Test Case End /\/\/\ + REM -------------------- + + CALL :ResolveStatus + exit /b %errorlevel% +REM End of Main + +REM --------------------------------------------------- +REM Assert [..Parameters(up to 9)] +REM --------------------------------------------------- +GOTO :End REM Prevents the code below from being executed +:Assert +set "stdout=" + +REM Run the program and capture the output then delete the file +set filePath=%slug%.bat +if "%isTestRunner%"=="true" ( + set filePath=.meta\Example.bat +) +set batPath=%~dp0 +CALL %batPath%%filePath% %~1 %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9 > stdout.bin 2>&1 +for /f "delims=" %%A in (stdout.bin) do ( + set "line=%%A" + if defined stdout ( + set "stdout=!stdout!\n!line!" + ) else ( + set "stdout=!line!" + ) +) +del stdout.bin + +REM Check if the result is correct +if "%stdout%" == "%expected%" ( + if defined if_success ( + echo %if_success% + + REM Reset the variable to avoid duplicating the message. + set "if_success=" + set "if_failed=" + ) + + REM If the result is correct, exit with code 0 + set /a successCount+=1 + exit /b 0 +) else ( + if defined if_failed ( + echo %if_failed% + + REM Reset the variable to avoid duplicating the message. + set "if_success=" + set "if_failed=" + ) + + REM If the result is incorrect, exit with code 1 + set /a failCount+=1 + exit /b 1 +) +GOTO :EOF REM Go back to the line after the call to :Assert + +:ResolveStatus +set "status=" +if %failCount% gtr 0 ( + REM status: Fail + REM message: The test failed. + exit /b 1 + +) else ( + REM status: Pass + exit /b 0 + +) +GOTO :EOF REM Go back to the line after the call to :ExportResultAsJson + +:End