Skip to content
Open
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
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,19 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "space-pirates",
"name": "space-pirates",
"uuid": "c9e78be6-8e53-4507-a143-ae927069d901",
"practices": [],
"prerequisites": [],
"difficulty": 5,
"topics": [
"processes",
"references",
"concurrency"
]
}
],
"foregone": [
Expand Down
92 changes: 92 additions & 0 deletions exercises/practice/space-pirates/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
## General

The Basics of Process Communication

In Erlang, every communication between processes happens through message passing.

Think of your ships not as objects with methods, but as independent beings that can only talk to each other by sending messages.

When a ship goes down, it doesn't call a function - it simply stops receiving messages.

How would other ships know it's gone? You'll need to establish connections between your processes that can detect when one disappears.

Remember: in space, no one can hear you scream - unless you've set up the right message handling!

___

Monitoring Your Fleet

To build a robust fleet communication system, you'll need to use these key Erlang concepts:

`link/1`` - Creates a bidirectional connection between processes
`process_flag(trap_exit, true)`` - Makes a process receive notifications when linked processes die
`spawn_link/1` - Creates a new process that's automatically linked to its parent

When setting up your command ship, make sure it can "trap exits" so it receives notifications when ships are destroyed.

Your message pattern matching should look for `{'EXIT', Pid, Reason}` messages that indicate a ship has gone down.

For secure communications, remember that `make_ref/0` creates a unique reference that can't be guessed - perfect for authenticating messages between ships!

___

Implementation Blueprint

Here's a detailed structure for your solution:

```erlang

%% Command ship state management
-record(command_state, {
ships = #{}, % Map of ship name to Pid
trap_exit = false, % Is the ship currently trapping exits?
distress_log = [], % List of received distress signals
auth_refs = sets:new() % Set of valid authentication references
}).

%% Pirate ship state management
-record(ship_state, {
name,
messages = #{}, % Map of Ref to message content
auth_refs = sets:new() % Set of valid authentication refs
}).

%% Handle the 'EXIT' signal in your command ship loop
command_loop(State) ->
receive
{'EXIT', Pid, {ship_destroyed, Reason}} ->
%% 1. Find the ship's name from its Pid
ShipName = maps:fold(fun(Name, P, Acc) ->
if P =:= Pid -> Name; true -> Acc end
end, undefined, State#command_state.ships),

%% 2. Remove the ship from the registry
NewShips = maps:remove(ShipName, State#command_state.ships),

%% 3. Log the distress
NewLog = [{ShipName, Reason, erlang:timestamp()} | State#command_state.distress_log],

%% 4. Spawn replacement if auto-replace is enabled
FinalShips = case State#command_state.auto_replace of
true ->
NewShip = spawn_link(fun() -> ship_loop(#ship_state{name=ShipName}) end),
maps:put(ShipName, NewShip, NewShips);
false ->
NewShips
end,

command_loop(State#command_state{ships=FinalShips, distress_log=NewLog});

%% Handle other messages...
end.

```

For secure message authentication:

Create references with `make_ref()`
Register them with both sender and receiver
Verify that incoming messages contain a registered reference
Remember that pirates can't be trusted - always verify before accepting a message!

The command ship should maintain the fleet registry and be the central point for monitoring ship status.
42 changes: 42 additions & 0 deletions exercises/practice/space-pirates/.docs/instructions.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Globally, Exercism prefers one sentence per line for Markdown (https://exercism.org/docs/building/markdown/markdown#h-one-sentence-per-line).

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Instructions

Your task is to implement a reliable communication system for Captain Nova's pirate fleet using Erlang's process model.

The system needs to handle ship-to-ship communication, distress signals, authentication, and automatic ship replacement when one is destroyed.

Core Requirements

- Command Ship - Implement a central command ship process that acts as a supervisor for the fleet
- Pirate Ships - Create ship processes that can send and receive authenticated messages
- Distress Beacon - Enable command ships to trap exit signals when pirate ships are destroyed
- Message Authentication - Use Erlang references to create secure communications between ships
- Ship Registry - Maintain a registry of active ships in the fleet
- Auto-Replacement - When a ship is destroyed, automatically spawn a replacement

Concepts You'll Be Using

- Process Creation: Using spawn/1 and spawn_link/1 to create processes
- Message Passing: Sending messages between processes using the `!` operator
- Process Linking: Using link/1 to connect processes
- Exit Signal Handling: Using process_flag(trap_exit, true) to catch process termination
- References: Using make_ref/0 to create unique identifiers for messages
- Process Registration: Maintaining a registry of processes

## Your Task

Implement the functions in the `space_pirates.erl` module to create a robust, fault-tolerant messaging system that can:

- Start a command ship that can monitor other ships
- Spawn pirate ships that can communicate with each other
- Enable the distress beacon to trap exit signals
- Create unique references for mission authentication
- Send authenticated messages between ships
- Handle ship destruction and automatic replacement

Remember, in deep space, there's no room for failure.

Ships get destroyed, communications get intercepted, and the Empire is always watching.

Your system needs to be reliable, secure, and resilient.

Good luck, Communication Officer. The fleet's success depends on you.
10 changes: 10 additions & 0 deletions exercises/practice/space-pirates/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Introduction

In the 30th century, interstellar travel has become commonplace, and with it, the age-old profession of piracy has expanded to the stars. Captains with fast ships and loyal crews plunder trade routes across the galaxy, always staying one step ahead of the Imperial Fleet.

You've recently joined the crew of the notorious Captain Nova, whose fleet of pirate ships is known throughout the Perseus Arm. As the new communications officer, you've been tasked with implementing their distributed messaging system. The previous officer was spaced after a critical failure that left three ships stranded during a raid on an Imperial convoy.

"Listen carefully," Captain Nova tells you on your first day. "Our ships operate across vast distances. A fleet is only as strong as its communication network. When a ship goes down—and they do go down—I need to know immediately. If a ship sends a distress signal, it needs to reach command with proper authentication. No Imperial interference, no message spoofing. And most importantly, when we lose a ship, I need an immediate replacement dispatched. Understood?"

You gulp nervously. "I'll need to use **Erlang**'s concurrency model—processes, links, monitors, and references."
Captain Nova grins. "*Precisely*. Now get to work before I find another communications officer."
17 changes: 17 additions & 0 deletions exercises/practice/space-pirates/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"Maartz"
],
"files": {
"solution": [
"src/space_pirates.erl"
],
"test": [
"test/space_pirates_tests.erl"
],
"example": [
".meta/example.erl"
]
},
"blurb": "Learn Erlang's process model by building a communication system for a fleet of space pirates."
}
Loading