From 2cee6a1d19adf0b450b518c7d3fe84323d3dd9c4 Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Thu, 26 Jun 2025 22:37:21 +1000 Subject: [PATCH 1/2] Add book-store closes #166 --- config.json | 8 + .../book-store/.docs/instructions.append.md | 23 +++ .../practice/book-store/.docs/instructions.md | 61 +++++++ exercises/practice/book-store/.eslintrc | 18 ++ .../practice/book-store/.meta/config.json | 28 ++++ .../practice/book-store/.meta/proof.ci.wat | 143 ++++++++++++++++ .../practice/book-store/.meta/tests.toml | 64 ++++++++ exercises/practice/book-store/.npmrc | 1 + exercises/practice/book-store/LICENSE | 21 +++ exercises/practice/book-store/babel.config.js | 4 + .../practice/book-store/book-store.spec.js | 155 ++++++++++++++++++ exercises/practice/book-store/book-store.wat | 15 ++ exercises/practice/book-store/package.json | 34 ++++ 13 files changed, 575 insertions(+) create mode 100644 exercises/practice/book-store/.docs/instructions.append.md create mode 100644 exercises/practice/book-store/.docs/instructions.md create mode 100644 exercises/practice/book-store/.eslintrc create mode 100644 exercises/practice/book-store/.meta/config.json create mode 100644 exercises/practice/book-store/.meta/proof.ci.wat create mode 100644 exercises/practice/book-store/.meta/tests.toml create mode 100644 exercises/practice/book-store/.npmrc create mode 100644 exercises/practice/book-store/LICENSE create mode 100644 exercises/practice/book-store/babel.config.js create mode 100644 exercises/practice/book-store/book-store.spec.js create mode 100644 exercises/practice/book-store/book-store.wat create mode 100644 exercises/practice/book-store/package.json diff --git a/config.json b/config.json index c3ed6f3..5918c82 100644 --- a/config.json +++ b/config.json @@ -615,6 +615,14 @@ "prerequisites": [], "difficulty": 6 }, + { + "slug": "book-store", + "name": "Book Store", + "uuid": "8fe40b8b-d6f4-46a7-83cc-29bac3494858", + "practices": [], + "prerequisites": [], + "difficulty": 7 + }, { "slug": "meetup", "name": "Meetup", diff --git a/exercises/practice/book-store/.docs/instructions.append.md b/exercises/practice/book-store/.docs/instructions.append.md new file mode 100644 index 0000000..1aa1106 --- /dev/null +++ b/exercises/practice/book-store/.docs/instructions.append.md @@ -0,0 +1,23 @@ +# Instruction append + +## WebAssembly-specific Notes + +The function signature for the WebAssembly export `total` is as follows: + +```wasm +(func (export "total") + (param $basketOffset i32) + (param $basketLength i32) + (result i32) +) +``` + +The two parameters `$basketOffset` and `$basketLength` express the base offset and length of an array of 32-bit integers. The length parameter is sized in number of elements in the array, not bytes. Prior to calling this function, the caller writes this array into the WebAssembly linear memory beginning at offset `$basketOffset`. WebAssembly linear memory is always expressed in little-endian. + +For example, the caller would encode the basket `[1,2]` as the following eight byte sequence. + +``` +| 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | +| --- basket[0] --- | --- basket[1] --- | +,0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x00, +``` diff --git a/exercises/practice/book-store/.docs/instructions.md b/exercises/practice/book-store/.docs/instructions.md new file mode 100644 index 0000000..54403f1 --- /dev/null +++ b/exercises/practice/book-store/.docs/instructions.md @@ -0,0 +1,61 @@ +# Instructions + +To try and encourage more sales of different books from a popular 5 book series, a bookshop has decided to offer discounts on multiple book purchases. + +One copy of any of the five books costs $8. + +If, however, you buy two different books, you get a 5% discount on those two books. + +If you buy 3 different books, you get a 10% discount. + +If you buy 4 different books, you get a 20% discount. + +If you buy all 5, you get a 25% discount. + +Note that if you buy four books, of which 3 are different titles, you get a 10% discount on the 3 that form part of a set, but the fourth book still costs $8. + +Your mission is to write code to calculate the price of any conceivable shopping basket (containing only books of the same series), giving as big a discount as possible. + +For example, how much does this basket of books cost? + +- 2 copies of the first book +- 2 copies of the second book +- 2 copies of the third book +- 1 copy of the fourth book +- 1 copy of the fifth book + +One way of grouping these 8 books is: + +- 1 group of 5 (1st, 2nd,3rd, 4th, 5th) +- 1 group of 3 (1st, 2nd, 3rd) + +This would give a total of: + +- 5 books at a 25% discount +- 3 books at a 10% discount + +Resulting in: + +- 5 × (100% - 25%) × $8 = 5 × $6.00 = $30.00, plus +- 3 × (100% - 10%) × $8 = 3 × $7.20 = $21.60 + +Which equals $51.60. + +However, a different way to group these 8 books is: + +- 1 group of 4 books (1st, 2nd, 3rd, 4th) +- 1 group of 4 books (1st, 2nd, 3rd, 5th) + +This would give a total of: + +- 4 books at a 20% discount +- 4 books at a 20% discount + +Resulting in: + +- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60, plus +- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60 + +Which equals $51.20. + +And $51.20 is the price with the biggest discount. diff --git a/exercises/practice/book-store/.eslintrc b/exercises/practice/book-store/.eslintrc new file mode 100644 index 0000000..1dbeac2 --- /dev/null +++ b/exercises/practice/book-store/.eslintrc @@ -0,0 +1,18 @@ +{ + "root": true, + "extends": "@exercism/eslint-config-javascript", + "env": { + "jest": true + }, + "overrides": [ + { + "files": [ + "*.spec.js" + ], + "excludedFiles": [ + "custom.spec.js" + ], + "extends": "@exercism/eslint-config-javascript/maintainers" + } + ] +} diff --git a/exercises/practice/book-store/.meta/config.json b/exercises/practice/book-store/.meta/config.json new file mode 100644 index 0000000..6933616 --- /dev/null +++ b/exercises/practice/book-store/.meta/config.json @@ -0,0 +1,28 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "book-store.wat" + ], + "test": [ + "book-store.spec.js" + ], + "example": [ + ".meta/proof.ci.wat" + ], + "invalidator": [ + "package.json" + ] + }, + "blurb": "To try and encourage more sales of different books from a popular 5 book series, a bookshop has decided to offer discounts of multiple-book purchases.", + "source": "Inspired by the harry potter kata from Cyber-Dojo.", + "source_url": "https://cyber-dojo.org", + "custom": { + "version.tests.compatibility": "jest-27", + "flag.tests.task-per-describe": false, + "flag.tests.may-run-long": false, + "flag.tests.includes-optional": false + } +} diff --git a/exercises/practice/book-store/.meta/proof.ci.wat b/exercises/practice/book-store/.meta/proof.ci.wat new file mode 100644 index 0000000..4f04fa3 --- /dev/null +++ b/exercises/practice/book-store/.meta/proof.ci.wat @@ -0,0 +1,143 @@ +(module + (memory (export "mem") 1) + + (global $tableOffset i32 (i32.const 0)) + + ;; + ;; Calculate the price of shopping basket of books + ;; + ;; @param {i32} basketOffset - offset of input u32[] array + ;; @param {i32} basketLength - length of input u32[] array in elements + ;; + ;; @return {i32} - price of shopping basket + ;; + (func (export "total") (param $basketOffset i32) (param $basketLength i32) (result i32) + (local $stop i32) + (local $inputPtr i32) + (local $tablePtr i32) + (local $i i32) + (local $j i32) + (local $tableI i32) + (local $tableJ i32) + (local $one i32) + (local $two i32) + (local $three i32) + (local $four i32) + (local $five i32) + (local $adjustment i32) + + (local.set $stop (i32.add (local.get $basketOffset) + (i32.mul (local.get $basketLength) + (i32.const 4)))) + (local.set $inputPtr (local.get $basketOffset)) + + (local.set $i (i32.const 0)) + (loop $zero + (if (i32.lt_u (local.get $i) (i32.const 20)) (then + (i32.store (i32.add (global.get $tableOffset) + (local.get $i)) (i32.const 0)) + (local.set $i (i32.add (local.get $i) + (i32.const 4))) + (br $zero) + )) + ) + + (loop $read + (if (i32.ne (local.get $inputPtr) (local.get $stop)) (then + (local.set $tablePtr (i32.add (global.get $tableOffset) + (i32.mul (i32.sub (i32.load (local.get $inputPtr)) + (i32.const 1)) + (i32.const 4)))) + (i32.store (local.get $tablePtr) (i32.add (i32.load (local.get $tablePtr)) + (i32.const 1))) + (local.set $inputPtr (i32.add (local.get $inputPtr) + (i32.const 4))) + (br $read) + )) + ) + + (local.set $i (i32.const 4)) + (loop $sortOuter + (if (i32.lt_u (local.get $i) (i32.const 20)) (then + (local.set $tableI (i32.load (i32.add (global.get $tableOffset) + (local.get $i)))) + (local.set $j (local.get $i)) + + (loop $sortInner + (local.set $j (i32.sub (local.get $j) + (i32.const 4))) + (if (i32.ge_s (local.get $j) (i32.const 0)) (then + (local.set $tableJ (i32.load (i32.add (global.get $tableOffset) + (local.get $j)))) + (if (i32.ge_u (local.get $tableJ) (local.get $tableI)) (then + (i32.store (i32.add (global.get $tableOffset) + (i32.add (local.get $j) + (i32.const 4))) + (local.get $tableJ)) + (br $sortInner) + )) + )) + ) + + (i32.store (i32.add (global.get $tableOffset) + (i32.add (local.get $j) + (i32.const 4))) + (local.get $tableI)) + + (local.set $i (i32.add (local.get $i) + (i32.const 4))) + (br $sortOuter) + )) + ) + + (local.set $five (i32.load (i32.add (global.get $tableOffset) + (i32.const 0)))) + (local.set $four (i32.load (i32.add (global.get $tableOffset) + (i32.const 4)))) + (local.set $three (i32.load (i32.add (global.get $tableOffset) + (i32.const 8)))) + (local.set $two (i32.load (i32.add (global.get $tableOffset) + (i32.const 12)))) + (local.set $one (i32.load (i32.add (global.get $tableOffset) + (i32.const 16)))) + + (local.set $one (i32.sub (local.get $one) + (local.get $two))) + (local.set $two (i32.sub (local.get $two) + (local.get $three))) + (local.set $three (i32.sub (local.get $three) + (local.get $four))) + (local.set $four (i32.sub (local.get $four) + (local.get $five))) + + (local.set $adjustment (local.get $three)) + (if (i32.lt_u (local.get $five) (local.get $three)) (then + (local.set $adjustment (local.get $five)) + )) + + (local.set $three (i32.sub (local.get $three) + (local.get $adjustment))) + (local.set $five (i32.sub (local.get $five) + (local.get $adjustment))) + (local.set $four (i32.add (local.get $four) + (i32.mul (local.get $adjustment) + (i32.const 2)))) + + (local.set $one (i32.mul (local.get $one) + (i32.const 800))) + (local.set $two (i32.mul (local.get $two) + (i32.const 1520))) + (local.set $three (i32.mul (local.get $three) + (i32.const 2160))) + (local.set $four (i32.mul (local.get $four) + (i32.const 2560))) + (local.set $five (i32.mul (local.get $five) + (i32.const 3000))) + + (return (i32.add (local.get $one) + (i32.add (i32.add (local.get $two) + (local.get $three)) + (i32.add (local.get $four) + (local.get $five))))) + ) +) diff --git a/exercises/practice/book-store/.meta/tests.toml b/exercises/practice/book-store/.meta/tests.toml new file mode 100644 index 0000000..4b7ce98 --- /dev/null +++ b/exercises/practice/book-store/.meta/tests.toml @@ -0,0 +1,64 @@ +# 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. + +[17146bd5-2e80-4557-ab4c-05632b6b0d01] +description = "Only a single book" + +[cc2de9ac-ff2a-4efd-b7c7-bfe0f43271ce] +description = "Two of the same book" + +[5a86eac0-45d2-46aa-bbf0-266b94393a1a] +description = "Empty basket" + +[158bd19a-3db4-4468-ae85-e0638a688990] +description = "Two different books" + +[f3833f6b-9332-4a1f-ad98-6c3f8e30e163] +description = "Three different books" + +[1951a1db-2fb6-4cd1-a69a-f691b6dd30a2] +description = "Four different books" + +[d70f6682-3019-4c3f-aede-83c6a8c647a3] +description = "Five different books" + +[78cacb57-911a-45f1-be52-2a5bd428c634] +description = "Two groups of four is cheaper than group of five plus group of three" + +[f808b5a4-e01f-4c0d-881f-f7b90d9739da] +description = "Two groups of four is cheaper than groups of five and three" + +[fe96401c-5268-4be2-9d9e-19b76478007c] +description = "Group of four plus group of two is cheaper than two groups of three" + +[68ea9b78-10ad-420e-a766-836a501d3633] +description = "Two each of first four books and one copy each of rest" + +[c0a779d5-a40c-47ae-9828-a340e936b866] +description = "Two copies of each book" + +[18fd86fe-08f1-4b68-969b-392b8af20513] +description = "Three copies of first book and two each of remaining" + +[0b19a24d-e4cf-4ec8-9db2-8899a41af0da] +description = "Three each of first two books and two each of remaining books" + +[bb376344-4fb2-49ab-ab85-e38d8354a58d] +description = "Four groups of four are cheaper than two groups each of five and three" + +[5260ddde-2703-4915-b45a-e54dbbac4303] +description = "Check that groups of four are created properly even when there are more groups of three than groups of five" + +[b0478278-c551-4747-b0fc-7e0be3158b1f] +description = "One group of one and four is cheaper than one group of two and three" + +[cf868453-6484-4ae1-9dfc-f8ee85bbde01] +description = "One group of one and two plus three groups of four is cheaper than one group of each size" diff --git a/exercises/practice/book-store/.npmrc b/exercises/practice/book-store/.npmrc new file mode 100644 index 0000000..d26df80 --- /dev/null +++ b/exercises/practice/book-store/.npmrc @@ -0,0 +1 @@ +audit=false diff --git a/exercises/practice/book-store/LICENSE b/exercises/practice/book-store/LICENSE new file mode 100644 index 0000000..90e73be --- /dev/null +++ b/exercises/practice/book-store/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Exercism + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/exercises/practice/book-store/babel.config.js b/exercises/practice/book-store/babel.config.js new file mode 100644 index 0000000..9c17ba5 --- /dev/null +++ b/exercises/practice/book-store/babel.config.js @@ -0,0 +1,4 @@ +export default { + presets: ["@exercism/babel-preset-javascript"], + plugins: [], +}; diff --git a/exercises/practice/book-store/book-store.spec.js b/exercises/practice/book-store/book-store.spec.js new file mode 100644 index 0000000..80c23ce --- /dev/null +++ b/exercises/practice/book-store/book-store.spec.js @@ -0,0 +1,155 @@ +import { compileWat, WasmRunner } from "@exercism/wasm-lib"; + +let wasmModule; +let currentInstance; + +beforeAll(async () => { + try { + const watPath = new URL("./book-store.wat", import.meta.url); + const { buffer } = await compileWat(watPath); + wasmModule = await WebAssembly.compile(buffer); + } catch (err) { + console.log(`Error compiling *.wat: \n${err}`); + process.exit(1); + } +}); + +function total(basket) { + const inputOffset = 64; + const inputBuffer = currentInstance.get_mem_as_i32( + inputOffset, + basket.length + ); + + inputBuffer.set(basket, 0); + + // Pass offset and length to WebAssembly function + return currentInstance.exports.total( + inputOffset, + basket.length + ); +} + +describe("total", () => { + beforeEach(async () => { + currentInstance = null; + if (!wasmModule) { + return Promise.reject(); + } + try { + currentInstance = await new WasmRunner(wasmModule); + return Promise.resolve(); + } catch (err) { + console.log(`Error instantiating WebAssembly module: ${err}`); + return Promise.reject(); + } + }); + + test('Only a single book', () => { + const expected = 800; + const actual = total([1]); + expect(actual).toEqual(expected); + }); + + xtest('Two of the same book', () => { + const expected = 1600; + const actual = total([2, 2]); + expect(actual).toEqual(expected); + }); + + xtest('Empty basket', () => { + const expected = 0; + const actual = total([]); + expect(actual).toEqual(expected); + }); + + xtest('Two different books', () => { + const expected = 1520; + const actual = total([1, 2]); + expect(actual).toEqual(expected); + }); + + xtest('Three different books', () => { + const expected = 2160; + const actual = total([1, 2, 3]); + expect(actual).toEqual(expected); + }); + + xtest('Four different books', () => { + const expected = 2560; + const actual = total([1, 2, 3, 4]); + expect(actual).toEqual(expected); + }); + + xtest('Five different books', () => { + const expected = 3000; + const actual = total([1, 2, 3, 4, 5]); + expect(actual).toEqual(expected); + }); + + xtest('Two groups of four is cheaper than group of five plus group of three', () => { + const expected = 5120; + const actual = total([1, 1, 2, 2, 3, 3, 4, 5]); + expect(actual).toEqual(expected); + }); + + xtest('Two groups of four is cheaper than groups of five and three', () => { + const expected = 5120; + const actual = total([1, 1, 2, 3, 4, 4, 5, 5]); + expect(actual).toEqual(expected); + }); + + xtest('Group of four plus group of two is cheaper than two groups of three', () => { + const expected = 4080; + const actual = total([1, 1, 2, 2, 3, 4]); + expect(actual).toEqual(expected); + }); + + xtest('Two each of first four books and one copy each of rest', () => { + const expected = 5560; + const actual = total([1, 1, 2, 2, 3, 3, 4, 4, 5]); + expect(actual).toEqual(expected); + }); + + xtest('Two copies of each book', () => { + const expected = 6000; + const actual = total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]); + expect(actual).toEqual(expected); + }); + + xtest('Three copies of first book and two each of remaining', () => { + const expected = 6800; + const actual = total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1]); + expect(actual).toEqual(expected); + }); + + xtest('Three each of first two books and two each of remaining books', () => { + const expected = 7520; + const actual = total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2]); + expect(actual).toEqual(expected); + }); + + xtest('Four groups of four are cheaper than two groups each of five and three', () => { + const expected = 10240; + const actual = total([1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]); + expect(actual).toEqual(expected); + }); + + xtest('Check that groups of four are created properly even when there are more groups of three than groups of five', () => { + const expected = 14560; + const actual = total([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5]); + expect(actual).toEqual(expected); + }); + + xtest('One group of one and four is cheaper than one group of two and three', () => { + const expected = 3360; + const actual = total([1, 1, 2, 3, 4]); + expect(actual).toEqual(expected); + }); + + xtest('One group of one and two plus three groups of four is cheaper than one group of each size', () => { + const expected = 10000; + const actual = total([1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]); + expect(actual).toEqual(expected); + }); +}); diff --git a/exercises/practice/book-store/book-store.wat b/exercises/practice/book-store/book-store.wat new file mode 100644 index 0000000..98e961f --- /dev/null +++ b/exercises/practice/book-store/book-store.wat @@ -0,0 +1,15 @@ +(module + (memory (export "mem") 1) + + ;; + ;; Calculate the price of shopping basket of books + ;; + ;; @param {i32} basketOffset - offset of input u32[] array + ;; @param {i32} basketLength - length of input u32[] array in elements + ;; + ;; @return {i32} - price of shopping basket + ;; + (func (export "total") (param $basketOffset i32) (param $basketLength i32) (result i32) + (return (i32.const 0)) + ) +) diff --git a/exercises/practice/book-store/package.json b/exercises/practice/book-store/package.json new file mode 100644 index 0000000..d8b6364 --- /dev/null +++ b/exercises/practice/book-store/package.json @@ -0,0 +1,34 @@ +{ + "name": "@exercism/wasm-book-store", + "description": "Exercism exercises in WebAssembly.", + "type": "module", + "private": true, + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/exercism/wasm", + "directory": "exercises/practice/book-store" + }, + "jest": { + "maxWorkers": 1 + }, + "devDependencies": { + "@babel/core": "^7.23.3", + "@exercism/babel-preset-javascript": "^0.4.0", + "@exercism/eslint-config-javascript": "^0.6.0", + "@types/jest": "^29.5.8", + "@types/node": "^20.9.1", + "babel-jest": "^29.7.0", + "core-js": "^3.33.2", + "eslint": "^8.54.0", + "jest": "^29.7.0" + }, + "dependencies": { + "@exercism/wasm-lib": "^0.2.0" + }, + "scripts": { + "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./*", + "watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch ./*", + "lint": "eslint ." + } +} From 13b12e120d5cec9b1a231400226af03d7041648a Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Fri, 27 Jun 2025 11:10:24 +1000 Subject: [PATCH 2/2] memory.fill --- exercises/practice/book-store/.meta/proof.ci.wat | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/exercises/practice/book-store/.meta/proof.ci.wat b/exercises/practice/book-store/.meta/proof.ci.wat index 4f04fa3..8b8093d 100644 --- a/exercises/practice/book-store/.meta/proof.ci.wat +++ b/exercises/practice/book-store/.meta/proof.ci.wat @@ -31,16 +31,7 @@ (i32.const 4)))) (local.set $inputPtr (local.get $basketOffset)) - (local.set $i (i32.const 0)) - (loop $zero - (if (i32.lt_u (local.get $i) (i32.const 20)) (then - (i32.store (i32.add (global.get $tableOffset) - (local.get $i)) (i32.const 0)) - (local.set $i (i32.add (local.get $i) - (i32.const 4))) - (br $zero) - )) - ) + (memory.fill (global.get $tableOffset) (i32.const 0) (i32.const 20)) (loop $read (if (i32.ne (local.get $inputPtr) (local.get $stop)) (then