Skip to content

Commit 230448d

Browse files
committed
new exercise: saddle-points
1 parent 0e5f2ef commit 230448d

File tree

14 files changed

+440
-0
lines changed

14 files changed

+440
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,14 @@
685685
"uuid": "9edd0fda-cc73-40a2-a32c-b16918c65167",
686686
"practices": [],
687687
"prerequisites": [],
688+
"difficulty": 2
689+
},
690+
{
691+
"slug": "saddle-points",
692+
"name": "Saddle Points",
693+
"uuid": "5efb23b7-fa38-4667-a344-4455877beb03",
694+
"practices": [],
695+
"prerequisites": [],
688696
"difficulty": 4
689697
}
690698
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Input format
2+
3+
The input will be in the format of a string. To keep this exercise beginner-friendly, you need not expect numbers with multiple digits.
4+
5+
Bytes 64-191 of the linear memory are reserved for the input string.
6+
7+
## Output format
8+
9+
The output is expected in pairs of row and column as u8 directly concatenated.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Your task is to find the potential trees where you could build your tree house.
4+
5+
The data company provides the data as grids that show the heights of the trees.
6+
The rows of the grid represent the east-west direction, and the columns represent the north-south direction.
7+
8+
An acceptable tree will be the largest in its row, while being the smallest in its column.
9+
10+
A grid might not have any good trees at all.
11+
Or it might have one, or even several.
12+
13+
Here is a grid that has exactly one candidate tree.
14+
15+
```text
16+
17+
1 2 3 4
18+
|-----------
19+
1 | 9 8 7 8
20+
→ 2 |[5] 3 2 4
21+
3 | 6 6 7 1
22+
```
23+
24+
- Row 2 has values 5, 3, 2, and 4. The largest value is 5.
25+
- Column 1 has values 9, 5, and 6. The smallest value is 5.
26+
27+
So the point at `[2, 1]` (row: 2, column: 1) is a great spot for a tree house.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Introduction
2+
3+
You plan to build a tree house in the woods near your house so that you can watch the sun rise and set.
4+
5+
You've obtained data from a local survey company that show the height of every tree in each rectangular section of the map.
6+
You need to analyze each grid on the map to find good trees for your tree house.
7+
8+
A good tree is both:
9+
10+
- taller than every tree to the east and west, so that you have the best possible view of the sunrises and sunsets.
11+
- shorter than every tree to the north and south, to minimize the amount of tree climbing.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"root": true,
3+
"extends": "@exercism/eslint-config-javascript",
4+
"env": {
5+
"jest": true
6+
},
7+
"overrides": [
8+
{
9+
"files": [
10+
"*.spec.js"
11+
],
12+
"excludedFiles": [
13+
"custom.spec.js"
14+
],
15+
"extends": "@exercism/eslint-config-javascript/maintainers"
16+
}
17+
]
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"authors": [
3+
"atk"
4+
],
5+
"files": {
6+
"solution": [
7+
"saddle-points.wat"
8+
],
9+
"test": [
10+
"saddle-points.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.wat"
14+
],
15+
"invalidator": [
16+
"package.json"
17+
]
18+
19+
},
20+
"blurb": "Detect saddle points in a matrix.",
21+
"source": "J Dalbey's Programming Practice problems",
22+
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html",
23+
"custom": {
24+
"version.tests.compatibility": "jest-27",
25+
"flag.tests.task-per-describe": false,
26+
"flag.tests.may-run-long": false,
27+
"flag.tests.includes-optional": false
28+
}
29+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
(module
2+
(memory (export "mem") 1)
3+
4+
(global $outputOffset i32 (i32.const 1280))
5+
(global $parsedOffset i32 (i32.const 512))
6+
(global $rowMaxOffset i32 (i32.const 768))
7+
(global $colMinOffset i32 (i32.const 1024))
8+
(global $zero i32 (i32.const 48))
9+
(global $br i32 (i32.const 10))
10+
11+
;;
12+
;; Find the points in the matrix that are the largest in row and smallest in column
13+
;;
14+
;; @param {i32} $inputOffset - offset of the matrix in linear memory
15+
;; @param {i32} $inputLength - length of the matrix in linear memory
16+
;;
17+
;; @result {(i32,i32)} - offset and length of row-column-pairs in linear memory
18+
;;
19+
(func (export "saddlePoints") (param $inputOffset i32) (param $inputLength i32) (result i32 i32)
20+
(local $row i32)
21+
(local $column i32)
22+
(local $lineLength i32)
23+
(local $rowsLength i32)
24+
(local $char i32)
25+
(local $pos i32)
26+
(local $height i32)
27+
(local $outputLength i32)
28+
(if (i32.eqz (local.get $inputLength)) (then (return (global.get $outputOffset) (i32.const 0))))
29+
(loop $scanInput
30+
(local.set $char (i32.load8_u (i32.add (local.get $inputOffset) (local.get $pos))))
31+
(if (i32.ge_u (local.get $char) (global.get $zero))
32+
(then (local.set $height (i32.add (i32.mul (local.get $height) (i32.const 10))
33+
(i32.sub (local.get $char) (global.get $zero)))))
34+
(else (i32.store8 (i32.add (global.get $parsedOffset) (local.get $outputLength)) (local.get $height))
35+
(local.set $outputLength (i32.add (local.get $outputLength) (i32.const 1))
36+
(if (i32.gt_u (local.get $height) (i32.load8_u (i32.add (global.get $rowMaxOffset) (local.get $row))))
37+
(then (i32.store8 (i32.add (global.get $rowMaxOffset) (local.get $row)) (local.get $height))))
38+
(if (i32.le_u (local.get $height) (i32.sub (i32.load8_u (i32.add (global.get $colMinOffset)
39+
(local.get $column))) (i32.const 1)))
40+
(then (i32.store8 (i32.add (global.get $colMinOffset) (local.get $column)) (local.get $height))))
41+
(local.set $column (i32.add (local.get $column) (i32.const 1)))
42+
(local.set $height (i32.const 0)))))
43+
(if (i32.eq (local.get $char) (global.get $br)) (then
44+
(if (i32.eqz (local.get $lineLength)) (then (local.set $lineLength (local.get $pos))))
45+
(local.set $row (i32.add (local.get $row) (i32.const 1))
46+
(local.set $column (i32.const 0)))))
47+
(local.set $pos (i32.add (local.get $pos) (i32.const 1)))
48+
(br_if $scanInput (i32.le_u (local.get $pos) (local.get $inputLength))))
49+
(local.set $rowsLength (i32.add (local.get $row) (i32.const 1)))
50+
(local.set $lineLength (i32.div_u (local.get $outputLength) (local.get $rowsLength)))
51+
(local.set $outputLength (i32.const 0))
52+
(local.set $row (i32.const 0))
53+
(loop $rows
54+
(local.set $column (i32.const 0))
55+
(loop $columns
56+
(local.set $height (i32.load8_u (i32.add (global.get $parsedOffset)
57+
(i32.add (i32.mul (local.get $row) (local.get $lineLength)) (local.get $column)))))
58+
(if (i32.and (i32.eq (local.get $height)
59+
(i32.load8_u (i32.add (global.get $rowMaxOffset) (local.get $row))))
60+
(i32.eq (local.get $height) (i32.load8_u (i32.add (global.get $colMinOffset) (local.get $column)))))
61+
(then (i32.store16 (i32.add (global.get $outputOffset) (local.get $outputLength))
62+
(i32.or (i32.shl (i32.add (local.get $column) (i32.const 1)) (i32.const 8))
63+
(i32.add (local.get $row) (i32.const 1))))
64+
(local.set $outputLength (i32.add (local.get $outputLength) (i32.const 2)))))
65+
(local.set $column (i32.add (local.get $column) (i32.const 1)))
66+
(br_if $columns (i32.lt_u (local.get $column) (local.get $lineLength))))
67+
(local.set $row (i32.add (local.get $row) (i32.const 1)))
68+
(br_if $rows (i32.lt_u (local.get $row) (local.get $rowsLength))))
69+
(global.get $outputOffset) (local.get $outputLength)
70+
)
71+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[3e374e63-a2e0-4530-a39a-d53c560382bd]
13+
description = "Can identify single saddle point"
14+
15+
[6b501e2b-6c1f-491f-b1bb-7f278f760534]
16+
description = "Can identify that empty matrix has no saddle points"
17+
18+
[8c27cc64-e573-4fcb-a099-f0ae863fb02f]
19+
description = "Can identify lack of saddle points when there are none"
20+
21+
[6d1399bd-e105-40fd-a2c9-c6609507d7a3]
22+
description = "Can identify multiple saddle points in a column"
23+
24+
[3e81dce9-53b3-44e6-bf26-e328885fd5d1]
25+
description = "Can identify multiple saddle points in a row"
26+
27+
[88868621-b6f4-4837-bb8b-3fad8b25d46b]
28+
description = "Can identify saddle point in bottom right corner"
29+
30+
[5b9499ca-fcea-4195-830a-9c4584a0ee79]
31+
description = "Can identify saddle points in a non square matrix"
32+
33+
[ee99ccd2-a1f1-4283-ad39-f8c70f0cf594]
34+
description = "Can identify that saddle points in a single column matrix are those with the minimum value"
35+
36+
[63abf709-a84b-407f-a1b3-456638689713]
37+
description = "Can identify that saddle points in a single row matrix are those with the maximum value"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
presets: ["@exercism/babel-preset-javascript"],
3+
plugins: [],
4+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@exercism/wasm-saddle-points",
3+
"description": "Exercism exercises in WebAssembly.",
4+
"author": "Alex Lohr",
5+
"type": "module",
6+
"private": true,
7+
"license": "MIT",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/exercism/wasm",
11+
"directory": "exercises/practice/saddle-points"
12+
},
13+
"jest": {
14+
"maxWorkers": 1
15+
},
16+
"devDependencies": {
17+
"@babel/core": "^7.23.3",
18+
"@exercism/babel-preset-javascript": "^0.4.0",
19+
"@exercism/eslint-config-javascript": "^0.6.0",
20+
"@types/jest": "^29.5.8",
21+
"@types/node": "^20.9.1",
22+
"babel-jest": "^29.7.0",
23+
"core-js": "^3.33.2",
24+
"eslint": "^8.54.0",
25+
"jest": "^29.7.0"
26+
},
27+
"dependencies": {
28+
"@exercism/wasm-lib": "^0.2.0"
29+
},
30+
"scripts": {
31+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./*",
32+
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch ./*",
33+
"lint": "eslint ."
34+
}
35+
}

0 commit comments

Comments
 (0)