Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,21 @@
"logic"
]
},
{
"slug": "prism",
"name": "Prism",
"uuid": "ef463b82-bf5c-4761-a821-29eeabee3050",
"practices": [],
"prerequisites": [
"arithmetic-operators",
"arrays",
"array-loops",
"comparison",
"conditionals",
"for-loops"
],
"difficulty": 5
},
{
"slug": "satellite",
"name": "Satellite",
Expand Down
36 changes: 36 additions & 0 deletions exercises/practice/prism/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Instructions

Before activating the laser array, you must predict the exact order in which crystals will be hit, identified by their sample IDs.

## Example Test Case

Consider this crystal array configuration:

```json
{
"start": { "x": 0, "y": 0, "angle": 0 },
"prisms": [
{ "id": 3, "x": 30, "y": 10, "angle": 45 },
{ "id": 1, "x": 10, "y": 10, "angle": -90 },
{ "id": 2, "x": 10, "y": 0, "angle": 90 },
{ "id": 4, "x": 20, "y": 0, "angle": 0 }
]
}
```

## What's Happening

The laser starts at the origin `(0, 0)` and fires horizontally to the right at angle 0°.
Here's the step-by-step beam path:

**Step 1**: The beam travels along the x-axis (y = 0) and first encounters **Crystal #2** at position `(10, 0)`.
This crystal has a refraction angle of 90°, which means it bends the beam perpendicular to its current path.
The beam, originally traveling at 0°, is now redirected to 90° (straight up).

**Step 2**: The beam now travels vertically upward from position `(10, 0)` and strikes **Crystal #1** at position `(10, 10)`.
This crystal has a refraction angle of -90°, bending the beam by -90° relative to its current direction.
The beam was traveling at 90°, so after refraction it's now at 0° (90° + (-90°) = 0°), traveling horizontally to the right again.

**Step 3**: From position `(10, 10)`, the beam travels horizontally and encounters **Crystal #3** at position `(30, 10)`.
This crystal refracts the beam by 45°, changing its direction to 45°.
The beam continues into empty space beyond the array.
5 changes: 5 additions & 0 deletions exercises/practice/prism/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

You're a researcher at **PRISM** (Precariously Redirected Illumination Safety Management), working with a precision laser calibration system that tests experimental crystal prisms.
These crystals are being developed for next-generation optical computers, and each one has unique refractive properties based on its molecular structure.
The lab's laser system can damage crystals if they receive unexpected illumination, so precise path prediction is critical.
5 changes: 5 additions & 0 deletions exercises/practice/prism/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/bin/configlet
/bin/configlet.exe
/package-lock.json
/yarn.lock
25 changes: 25 additions & 0 deletions exercises/practice/prism/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"prism.js"
],
"test": [
"prism.spec.js"
],
"example": [
".meta/proof.ci.js"
]
},
"blurb": "Calculate the path of a laser through reflective prisms.",
"source": "FraSanga",
"source_url": "https://github.com/exercism/problem-specifications/pull/2625",
"custom": {
"version.tests.compatibility": "jest-27",
"flag.tests.task-per-describe": false,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false
}
}
41 changes: 41 additions & 0 deletions exercises/practice/prism/.meta/proof.ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export const findSequence = (start, prisms) => {
let { x, y, angle } = start;
const sequence = [];

while (true) {
const rad = (angle * Math.PI) / 180;
const dirX = Math.cos(rad);
const dirY = Math.sin(rad);

let nearest = null;
let nearestDist = Infinity;

for (const prism of prisms) {
const dx = prism.x - x;
const dy = prism.y - y;

const dist = dx * dirX + dy * dirY;
const baseTolerance = 1e-6;
if (dist <= baseTolerance) continue;

const crossProductSquared =
(dx - dist * dirX) ** 2 + (dy - dist * dirY) ** 2;
const relativeTolerance = baseTolerance * Math.max(1, dist * dist);
if (crossProductSquared >= relativeTolerance) continue;

if (dist < nearestDist) {
nearestDist = dist;
nearest = prism;
}
}

if (!nearest) break;

sequence.push(nearest.id);
x = nearest.x;
y = nearest.y;
angle = (angle + nearest.angle) % 360;
}

return sequence;
};
52 changes: 52 additions & 0 deletions exercises/practice/prism/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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.

[ec65d3b3-f7bf-4015-8156-0609c141c4c4]
description = "zero prisms"

[ec0ca17c-0c5f-44fb-89ba-b76395bdaf1c]
description = "one prism one hit"

[0db955f2-0a27-4c82-ba67-197bd6202069]
description = "one prism zero hits"

[8d92485b-ebc0-4ee9-9b88-cdddb16b52da]
description = "going up zero hits"

[78295b3c-7438-492d-8010-9c63f5c223d7]
description = "going down zero hits"

[acc723ea-597b-4a50-8d1b-b980fe867d4c]
description = "going left zero hits"

[3f19b9df-9eaa-4f18-a2db-76132f466d17]
description = "negative angle"

[96dacffb-d821-4cdf-aed8-f152ce063195]
description = "large angle"

[513a7caa-957f-4c5d-9820-076842de113c]
description = "upward refraction two hits"

[d452b7c7-9761-4ea9-81a9-2de1d73eb9ef]
description = "downward refraction two hits"

[be1a2167-bf4c-4834-acc9-e4d68e1a0203]
description = "same prism twice"

[df5a60dd-7c7d-4937-ac4f-c832dae79e2e]
description = "simple path"

[8d9a3cc8-e846-4a3b-a137-4bfc4aa70bd1]
description = "multiple prisms floating point precision"

[e077fc91-4e4a-46b3-a0f5-0ba00321da56]
description = "complex path with multiple prisms floating point precision"
1 change: 1 addition & 0 deletions exercises/practice/prism/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/prism/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions exercises/practice/prism/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: [['@exercism/babel-preset-javascript', { corejs: '3.40' }]],
plugins: [],
};
45 changes: 45 additions & 0 deletions exercises/practice/prism/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @ts-check

import config from '@exercism/eslint-config-javascript';
import maintainersConfig from '@exercism/eslint-config-javascript/maintainers.mjs';

import globals from 'globals';

export default [
...config,
...maintainersConfig,
{
files: maintainersConfig[1].files,
rules: {
'jest/expect-expect': ['warn', { assertFunctionNames: ['expect*'] }],
},
},
{
files: ['scripts/**/*.mjs'],
languageOptions: {
globals: {
...globals.node,
},
},
},
// <<inject-rules-here>>
{
ignores: [
// # Protected or generated
'/.appends/**/*',
'/.github/**/*',
'/.vscode/**/*',

// # Binaries
'/bin/*',

// # Configuration
'/config',
'/babel.config.js',

// # Typings
'/exercises/**/global.d.ts',
'/exercises/**/env.d.ts',
],
},
];
22 changes: 22 additions & 0 deletions exercises/practice/prism/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
verbose: true,
projects: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/test/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test).[jt]s?(x)',
],
testPathIgnorePatterns: [
'/(?:production_)?node_modules/',
'.d.ts$',
'<rootDir>/test/fixtures',
'<rootDir>/test/helpers',
'__mocks__',
],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
moduleNameMapper: {
'^(\\.\\/.+)\\.js$': '$1',
},
};
34 changes: 34 additions & 0 deletions exercises/practice/prism/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@exercism/javascript-prism",
"description": "Exercism exercises in Javascript.",
"author": "Katrina Owen",
"private": true,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/exercism/javascript",
"directory": "exercises/practice/prism"
},
"devDependencies": {
"@exercism/babel-preset-javascript": "^0.5.1",
"@exercism/eslint-config-javascript": "^0.8.1",
"@jest/globals": "^29.7.0",
"@types/node": "^24.3.0",
"@types/shelljs": "^0.8.17",
"babel-jest": "^29.7.0",
"core-js": "~3.42.0",
"diff": "^8.0.2",
"eslint": "^9.28.0",
"expect": "^29.7.0",
"globals": "^16.3.0",
"jest": "^29.7.0"
},
"dependencies": {},
"scripts": {
"lint": "corepack pnpm eslint .",
"test": "corepack pnpm jest",
"watch": "corepack pnpm jest --watch",
"format": "corepack pnpm prettier -w ."
},
"packageManager": "pnpm@9.15.2"
}
8 changes: 8 additions & 0 deletions exercises/practice/prism/prism.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// This is only a SKELETON file for the 'Prism' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const findSequence = () => {
throw new Error('Remove this line and implement the function');
};
Loading