Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 new exercise: robot-simulator #139

Merged
merged 2 commits into from
Apr 24, 2024
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
"uuid": "aa8d3ec5-da37-45e2-87c2-ec94eacf6cb4",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "pascals-triangle",
"name": "Pascal's Triangle",
Expand Down
35 changes: 0 additions & 35 deletions exercises/practice/binary-search/.docs/introduction.md

This file was deleted.

42 changes: 0 additions & 42 deletions exercises/practice/protein-translation/.docs/introduction.md

This file was deleted.

25 changes: 25 additions & 0 deletions exercises/practice/robot-simulator/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instructions

Write a robot simulator.

A robot factory's test facility needs a program to verify robot movements.

The robots have three possible movements:

- turn right
- turn left
- advance

Robots are placed on a hypothetical infinite grid, facing a particular direction (north, east, south, or west) at a set of {x,y} coordinates,
e.g., {3,8}, with coordinates increasing to the north and east.

The robot then receives a number of instructions, at which point the testing facility verifies the robot's new position, and in which direction it is pointing.

- The letter-string "RAALAL" means:
- Turn right
- Advance twice
- Turn left
- Advance once
- Turn left yet again
- Say a robot starts at {7, 3} facing north.
Then running this stream of instructions should leave it at {9, 4} facing west.
22 changes: 22 additions & 0 deletions exercises/practice/robot-simulator/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"kapitaali"
],
"files": {
"solution": [
"src/robot-simulator.cob"
],
"test": [
"tst/robot-simulator/robot-simulator.cut"
],
"example": [
".meta/proof.ci.cob"
],
"invalidator": [
"test.sh",
"test.ps1"
]
},
"blurb": "Write a robot simulator.",
"source": "Inspired by an interview question at a famous company."
}
68 changes: 68 additions & 0 deletions exercises/practice/robot-simulator/.meta/proof.ci.cob
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. ROBOT-SIMULATOR.
AUTHOR. kapitaali.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-X-COORD PIC S999.
01 WS-Y-COORD PIC S999.
01 WS-DIRECTION PIC X(20).
01 WS-INSTRUCTIONS PIC X(60).
01 INSTRU PIC X.
01 LEN PIC 99.
01 CC PIC 99.


PROCEDURE DIVISION.

CREATE-ROBOT.
CONTINUE.


MOVE-ROBOT.
PERFORM VARYING LEN FROM 60 BY -1
UNTIL WS-INSTRUCTIONS(LEN:1) IS NOT EQUAL TO " "
CONTINUE
END-PERFORM.
PERFORM VARYING CC FROM 1 BY 1 UNTIL CC > LEN
MOVE WS-INSTRUCTIONS(CC:1) TO INSTRU
PERFORM PROCESS-INSTRUCTION
END-PERFORM.


PROCESS-INSTRUCTION.
EVALUATE INSTRU
WHEN 'L'
EVALUATE WS-DIRECTION
WHEN 'north'
MOVE 'west' TO WS-DIRECTION
WHEN 'west'
MOVE 'south' TO WS-DIRECTION
WHEN 'south'
MOVE 'east' TO WS-DIRECTION
WHEN 'east'
MOVE 'north' TO WS-DIRECTION
END-EVALUATE
WHEN 'R'
EVALUATE WS-DIRECTION
WHEN 'north'
MOVE 'east' TO WS-DIRECTION
WHEN 'west'
MOVE 'north' TO WS-DIRECTION
WHEN 'south'
MOVE 'west' TO WS-DIRECTION
WHEN 'east'
MOVE 'south' TO WS-DIRECTION
END-EVALUATE
WHEN 'A'
EVALUATE WS-DIRECTION
WHEN 'north'
ADD 1 TO WS-Y-COORD
WHEN 'east'
ADD 1 TO WS-X-COORD
WHEN 'south'
SUBTRACT 1 FROM WS-Y-COORD
WHEN 'west'
SUBTRACT 1 FROM WS-X-COORD
END-EVALUATE
END-EVALUATE.
63 changes: 63 additions & 0 deletions exercises/practice/robot-simulator/bin/fetch-cobolcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash

# This file is a copy of the
# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet file.
# Please submit bugfixes/improvements to the above file to ensure that all tracks benefit from the changes.

# set -eo pipefail

readonly LATEST='https://api.github.com/repos/0xE282B0/cobol-check/releases/latest'

case "$(uname)" in
Darwin*) os='mac' ;;
Linux*) os='linux' ;;
Windows*) os='windows' ;;
MINGW*) os='windows' ;;
MSYS_NT-*) os='windows' ;;
*) os='linux' ;;
esac

case "${os}" in
windows*) ext='.exe' ;;
*) ext='' ;;
esac

arch="$(uname -m)"

curlopts=(
--silent
--show-error
--fail
--location
--retry 3
)

if [[ -n "${GITHUB_TOKEN}" ]]; then
curlopts+=(--header "authorization: Bearer ${GITHUB_TOKEN}")
fi

suffix="${os}-${arch}${ext}"

get_download_url() {
curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${LATEST}" |
grep "\"browser_download_url\": \".*/download/.*/cobol-check.*${suffix}\"$" |
cut -d'"' -f4
}

main() {
if [[ -d ./bin ]]; then
output_dir="./bin"
elif [[ $PWD == */bin ]]; then
output_dir="$PWD"
else
echo "Error: no ./bin directory found. This script should be ran from a repo root." >&2
return 1
fi

output_path="${output_dir}/cobolcheck${ext}"
download_url="$(get_download_url)"
curl "${curlopts[@]}" --output "${output_path}" "${download_url}"
chmod +x "${output_path}"
}

main
28 changes: 28 additions & 0 deletions exercises/practice/robot-simulator/bin/fetch-cobolcheck.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file is a copy of the
# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet.ps1 file.
# Please submit bugfixes/improvements to the above file to ensure that all tracks
# benefit from the changes.

$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"

$requestOpts = @{
Headers = If ($env:GITHUB_TOKEN) { @{ Authorization = "Bearer ${env:GITHUB_TOKEN}" } } Else { @{ } }
MaximumRetryCount = 3
RetryIntervalSec = 1
}

$arch = If ([Environment]::Is64BitOperatingSystem) { "amd64" } Else { "x86" }
$fileName = "cobol-check-windows-$arch.exe"

Function Get-DownloadUrl {
$latestUrl = "https://api.github.com/repos/0xE282B0/cobol-check/releases/latest"
Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @requestOpts
| Select-Object -ExpandProperty assets
| Where-Object { $_.browser_download_url -match $FileName }
| Select-Object -ExpandProperty browser_download_url
}

$downloadUrl = Get-DownloadUrl
$outputFile = Join-Path -Path $PSScriptRoot -ChildPath "cobolcheck.exe"
Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts
Loading
Loading