Skip to content

Commit

Permalink
Implement exercise 'eliuds-eggs'
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Sep 16, 2024
1 parent 41fa41a commit dfa2898
Show file tree
Hide file tree
Showing 16 changed files with 392 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "eliuds-eggs",
"name": "Eliud's Eggs",
"uuid": "40b2b3ea-21ef-4cb3-8089-eafa597c985f",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "secret-handshake",
"name": "Secret Handshake",
Expand Down
7 changes: 7 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ exercises:
prerequisites: []
difficulty: 3

- slug: eliuds-eggs
name: Eliud's Eggs
uuid: 40b2b3ea-21ef-4cb3-8089-eafa597c985f
practices: []
prerequisites: []
difficulty: 3

- slug: secret-handshake
name: Secret Handshake
uuid: e2176eb3-3f20-4050-aa18-d627ad0628bd
Expand Down
8 changes: 8 additions & 0 deletions exercises/practice/eliuds-eggs/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 47 additions & 0 deletions exercises/practice/eliuds-eggs/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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:

```text
Chicken Coop:
_ _ _ _ _ _ _
|E| |E|E| | |E|
Resulting Binary:
1 0 1 1 0 0 1
Decimal number on the display:
89
Actual eggs in the coop:
4
```

Example 2:

```text
Chicken Coop:
_ _ _ _ _ _ _ _
| | | |E| | | | |
Resulting Binary:
0 0 0 1 0 0 0 0
Decimal number on the display:
16
Actual eggs in the coop:
1
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.75
28 changes: 28 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.76

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

YS_LOCAL_BIN := $(YS_LOCAL_PREFIX)/bin

YS_BIN := $(YS_LOCAL_BIN)/ys-$(YS_VERSION)

TEST_FILE ?= $(wildcard *-test.ys)


export PATH := $(YS_LOCAL_BIN):$(PATH)

export YSPATH := $(BASE)


default:

test: $(YS_BIN)
prove -v $(TEST_FILE)

$(YS_BIN):
curl -s https://yamlscript.org/install | \
BIN=1 VERSION=$(YS_VERSION) PREFIX=$(YS_LOCAL_PREFIX) bash >/dev/null
23 changes: 23 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"ingydotnet"
],
"files": {
"solution": [
"eliuds-eggs.ys"
],
"test": [
"eliuds-eggs-test.ys",
"GNUmakefile",
"Makefile",
".yamlscript/exercise.mk",
".yamlscript/exercism-ys-installer"
],
"example": [
".meta/eliuds-eggs.ys"
]
},
"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"
}
28 changes: 28 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/eliuds-eggs-test.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env ys-0

require ys::taptest: :all

use: eliuds-eggs

test::
- name: 0 eggs
code: egg-count(0)
want: 0
uuid: 559e789d-07d1-4422-9004-3b699f83bca3

- name: 1 egg
code: egg-count(16)
want: 1
uuid: 97223282-f71e-490c-92f0-b3ec9e275aba

- name: 4 eggs
code: egg-count(89)
want: 4
uuid: 1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5

- name: 13 eggs
code: egg-count(2000000000)
want: 13
uuid: 0c18be92-a498-4ef2-bcbb-28ac4b06cb81

done: 4
7 changes: 7 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
!yamlscript/v0

defn egg-count(number):
loop num number, eggs 0:
if num.?:
recur: quot(num 2), (eggs + (num % 2))
else: eggs + num
22 changes: 22 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions exercises/practice/eliuds-eggs/.yamlscript/exercise.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.75
127 changes: 127 additions & 0 deletions exercises/practice/eliuds-eggs/.yamlscript/exercism-ys-installer
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/env/bin/env bash

set -euo pipefail

intro-prompt() (
cat <<...
--------------------------------------------------------------------------------
This YAMLScript Exercism exercise requires the YAMLScript version $version
interpreter command file to be installed here:
$prefix/bin/ys
You can install it by pressing Enter now, or by running this command:
$make install-ys
This should only take a few seconds and you only need to do this once.
Other exercises will use the same file.
See https://yamlscript.org/doc/install/ for more YAMLScript installation info.
--------------------------------------------------------------------------------
Would you like to install the 'ys' file now?
...

printf "Press Enter to install. Ctl-C to Quit."; read -r
)

main() {
setup "$@"

install-from-local

$auto && intro-prompt

installed || install-from-release || true
installed || install-from-build || true
installed ||
die "Installing '$installed' failed. Giving up." \
"Consider filing an issue at: $gh_issue_url"

echo
echo 'Success!'
echo "$installed is now installed."
echo
}

installed() {
[[ -f $installed ]]
}

install-from-local() {
local path
path=$(command -v "$ysfq") || true
if [[ -f $path ]]; then
mkdir -p "$bin"
cp "$path" "$bin"/
ln -fs "$ysfq" "$bin/ys-0"
ln -fs "$ysfq" "$bin/ys"
(installed && $auto) && exit
true
fi
}

install-from-release() (
set -x
curl -s https://yamlscript.org/install |
BIN=1 VERSION="$version" PREFIX="$prefix" bash
)

install-from-build() (
cat <<...
The binary release installation failed.
We can attempt to build and install $ysfq now.
This can take from 1 to 5 minutes to complete.
...

printf "Press Enter to install. Ctl-C to Quit."; read -r

[[ -d /tmp && -w /tmp ]] ||
die "Can't write to /tmp" \
'Giving up.'

set -x

rm -fr "$yamlscript_clone"

git clone --branch="$version" "$yamlscript_repo" "$yamlscript_clone"

"$make" -C "$yamlscript_clone/ys" install PREFIX="$prefix"
)

setup() {
version=$1
prefix=$2
make=$3
auto=false
[[ ${4-} ]] && auto=true

[[ $version =~ ^0\.1\.[0-9]+$ ]] ||
die "Invalid YS_VERSION '$version'"

bin=$prefix/bin
ysfq=ys-$version
installed=$bin/$ysfq

if installed; then
echo "'$installed' is already installed."
exit
fi

yamlscript_repo=https://github.com/yaml/yamlscript
yamlscript_clone=/tmp/yamlscript-exercism
gh_issue_url=https://github.com/exercism/yamlscript/issues
}

die() {
printf '%s\n' "$@" >&2
exit 1
}

main "$@"
49 changes: 49 additions & 0 deletions exercises/practice/eliuds-eggs/GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
SHELL := bash

BASE := $(shell pwd)

include .yamlscript/exercise.mk

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
YS_LOCAL_PREFIX := $(shell cd .. && pwd -P)/.local/v$(YS_VERSION)
endif

YS_LOCAL_BIN := $(YS_LOCAL_PREFIX)/bin
YS_BIN := $(YS_LOCAL_BIN)/ys-$(YS_VERSION)

YS_INSTALLER := .yamlscript/exercism-ys-installer
YS_INSTALLER_CMD := \
bash $(YS_INSTALLER) $(YS_VERSION) $(YS_LOCAL_PREFIX) $(MAKE)

TEST_FILE ?= $(wildcard *-test.ys)

export PATH := $(YS_LOCAL_BIN):$(PATH)
export YSPATH := $(BASE)


#-------------------------------------------------------------------------------
default:
@echo " No default make rule. Try 'make test'."

test: $(YS_BIN)
prove -v $(TEST_FILE)

install-ys:
@$(YS_INSTALLER_CMD)

uninstall-ys:
rm -fr $(YS_LOCAL_PREFIX)


#-------------------------------------------------------------------------------
ifdef EXERCISM_YAMLSCRIPT_GHA
$(YS_BIN):

else ifeq (/mnt/,$(dir $(BASE)))
$(YS_BIN):

else
$(YS_BIN):
@$(YS_INSTALLER_CMD) auto
endif
8 changes: 8 additions & 0 deletions exercises/practice/eliuds-eggs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This Makefile is a decoy to attempt to detect when a non-GNU make is being
# used and alert the user.

test:
@echo "You appear to be using a non-GNU version of the 'make' program."
@echo "The YAMLScript Exercism track requires you to use GNU make."
@echo "Please try 'make $@' again using GNU make."
@exit 1
Loading

0 comments on commit dfa2898

Please sign in to comment.