Skip to content

Commit 9561781

Browse files
Add intergalactic transmission exercise (#2398)
* Add intergalactic transmission exercise * Update exercises/practice/intergalactic-transmission/.docs/introduction.md Co-authored-by: Erik Schierboom <[email protected]> * Adjust tests * Sync intergalactic transmission with problem specs * Add missing prerequisites Co-authored-by: Erik Schierboom <[email protected]> * Remove sln file * Fix Exercises.sln --------- Co-authored-by: Erik Schierboom <[email protected]>
1 parent 1607b8f commit 9561781

File tree

13 files changed

+823
-15
lines changed

13 files changed

+823
-15
lines changed

config.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,23 @@
27142714
],
27152715
"difficulty": 7
27162716
},
2717+
{
2718+
"slug": "intergalactic-transmission",
2719+
"name": "intergalactic-transmission",
2720+
"uuid": "845c8cf4-207c-438f-9d18-c5e7875ee729",
2721+
"practices": [
2722+
"bit-manipulation"
2723+
],
2724+
"prerequisites": [
2725+
"bit-manipulation",
2726+
"arrays",
2727+
"exceptions",
2728+
"integral-numbers",
2729+
"for-loops",
2730+
"if-statements"
2731+
],
2732+
"difficulty": 6
2733+
},
27172734
{
27182735
"slug": "split-second-stopwatch",
27192736
"name": "Split-Second Stopwatch",

exercises/Exercises.sln

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateOfTicTacToe", "practic
357357
EndProject
358358
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BottleSong", "practice\bottle-song\BottleSong.csproj", "{6BD384E6-225E-4F8A-856C-3079957C6E36}"
359359
EndProject
360+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntergalacticTransmission", "practice\intergalactic-transmission\IntergalacticTransmission.csproj", "{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}"
361+
EndProject
360362
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SplitSecondStopwatch", "practice\split-second-stopwatch\SplitSecondStopwatch.csproj", "{0A6CD51A-2120-4A72-A17B-08CCC2E6D365}"
361363
EndProject
362364
Global
@@ -2469,18 +2471,18 @@ Global
24692471
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Release|x64.Build.0 = Release|Any CPU
24702472
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Release|x86.ActiveCfg = Release|Any CPU
24712473
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Release|x86.Build.0 = Release|Any CPU
2472-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2473-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Debug|Any CPU.Build.0 = Debug|Any CPU
2474-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Debug|x64.ActiveCfg = Debug|Any CPU
2475-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Debug|x64.Build.0 = Debug|Any CPU
2476-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Debug|x86.ActiveCfg = Debug|Any CPU
2477-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Debug|x86.Build.0 = Debug|Any CPU
2478-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Release|Any CPU.ActiveCfg = Release|Any CPU
2479-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Release|Any CPU.Build.0 = Release|Any CPU
2480-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Release|x64.ActiveCfg = Release|Any CPU
2481-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Release|x64.Build.0 = Release|Any CPU
2482-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Release|x86.ActiveCfg = Release|Any CPU
2483-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692}.Release|x86.Build.0 = Release|Any CPU
2474+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2475+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
2476+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Debug|x64.ActiveCfg = Debug|Any CPU
2477+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Debug|x64.Build.0 = Debug|Any CPU
2478+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Debug|x86.ActiveCfg = Debug|Any CPU
2479+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Debug|x86.Build.0 = Debug|Any CPU
2480+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
2481+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Release|Any CPU.Build.0 = Release|Any CPU
2482+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Release|x64.ActiveCfg = Release|Any CPU
2483+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Release|x64.Build.0 = Release|Any CPU
2484+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Release|x86.ActiveCfg = Release|Any CPU
2485+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Release|x86.Build.0 = Release|Any CPU
24842486
{0A6CD51A-2120-4A72-A17B-08CCC2E6D365}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24852487
{0A6CD51A-2120-4A72-A17B-08CCC2E6D365}.Debug|Any CPU.Build.0 = Debug|Any CPU
24862488
{0A6CD51A-2120-4A72-A17B-08CCC2E6D365}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -2673,9 +2675,8 @@ Global
26732675
{8E276065-1371-4CFA-BA20-95225EC6AEBC} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
26742676
{67E9BAB3-9805-42F1-9298-E9BBB795140E} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
26752677
{6BD384E6-225E-4F8A-856C-3079957C6E36} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
2676-
{DA4856F5-A260-43A0-9C9C-6DD2E2B08692} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
2677-
{B587F905-CC4E-0FB1-25B1-84C433FD33E0} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
2678-
{0A6CD51A-2120-4A72-A17B-08CCC2E6D365} = {B587F905-CC4E-0FB1-25B1-84C433FD33E0}
2678+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
2679+
{0A6CD51A-2120-4A72-A17B-08CCC2E6D365} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
26792680
EndGlobalSection
26802681
GlobalSection(ExtensibilityGlobals) = postSolution
26812682
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Instructions
2+
3+
Your job is to help implement
4+
5+
- the transmitter, which calculates the transmission sequence, and
6+
- the receiver, which decodes it.
7+
8+
A parity bit is simple way of detecting transmission errors.
9+
The transmitters and receivers can only transmit and receive _exactly_ eight bits at a time (including the parity bit).
10+
The parity bit is set so that there is an _even_ number 1s in each transmission and is always the first bit from the right.
11+
So if the receiver receives `11000001`, `01110101` or `01000000` (i.e. a transmission with an odd number of 1 bits), it knows there is an error.
12+
13+
However, messages are rarely this short, and need to be transmitted in a sequence when they are longer.
14+
15+
For example, consider the message `11000000 00000001 11000000 11011110` (or `C0 01 C0 DE` in hex).
16+
17+
Since each transmission contains exactly eight bits, it can only contain seven bits of data and the parity bit.
18+
A parity bit must then be inserted after every seven bits of data:
19+
20+
```text
21+
11000000 00000001 11000000 11011110
22+
↑ ↑ ↑ ↑ (7th bits)
23+
```
24+
25+
The transmission sequence for this message looks like this:
26+
27+
```text
28+
1100000_ 0000000_ 0111000_ 0001101_ 1110
29+
↑ ↑ ↑ ↑ (parity bits)
30+
```
31+
32+
The data in the first transmission in the sequence (`1100000`) has two 1 bits (an even number), so the parity bit is 0.
33+
The first transmission becomes `11000000` (or `C0` in hex).
34+
35+
The data in the next transmission (`0000000`) has zero 1 bits (an even number again), so the parity bit is 0 again.
36+
The second transmission thus becomes `00000000` (or `00` in hex).
37+
38+
The data for the next two transmissions (`0111000` and `0001101`) have three 1 bits.
39+
Their parity bits are set to 1 so that they have an even number of 1 bits in the transmission.
40+
They are transmitted as `01110001` and `00011011` (or `71` and `1B` in hex).
41+
42+
The last transmission (`1110`) has only four bits of data.
43+
Since exactly eight bits are transmitted at a time and the parity bit is the right most bit, three 0 bits and then the parity bit are added to make up eight bits.
44+
It now looks like this (where `_` is the parity bit):
45+
46+
```text
47+
1110 000_
48+
↑↑↑ (added 0 bits)
49+
```
50+
51+
There is an odd number of 1 bits again, so the parity bit is 1.
52+
The last transmission in the sequence becomes `11100001` (or `E1` in hex).
53+
54+
The entire transmission sequence for this message is `11000000 00000000 01110001 00011011 11100001` (or `C0 00 71 1B E1` in hex).
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Introduction
2+
3+
Trillions upon trillions of messages zip between Earth and neighboring galaxies every millisecond.
4+
But transmitting over such long distances is tricky.
5+
Pesky solar flares, temporal distortions, stray forces, and even the flap of a space butterfly's wing can cause a random bit to change during transmission.
6+
7+
Now imagine the consequences:
8+
9+
- Crashing the Intergalactic Share Market when "buy low" turns to "sell now".
10+
- Losing contact with the Kepler Whirl system when "save new worm hole" becomes "cave new worm hole".
11+
- Or plunging the universe into existential horror by replacing a cowboy emoji 🤠 with a clown emoji 🤡.
12+
13+
Detecting corrupted messages isn't just important — it's critical.
14+
The receiver _must_ know when something has gone wrong before disaster strikes.
15+
16+
But how?
17+
Scientists and engineers from across the universe have been battling this problem for eons.
18+
Entire cosmic AI superclusters churn through the data.
19+
And then, one day, a legend resurfaces — an ancient, powerful method, whispered in debugging forums, muttered by engineers who've seen too much...
20+
21+
The Parity Bit!
22+
23+
A method so simple, so powerful, that it might just save interstellar communication.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
###############################
2+
# Core EditorConfig Options #
3+
###############################
4+
5+
; This file is for unifying the coding style for different editors and IDEs.
6+
; More information at:
7+
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
8+
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
9+
10+
root = true
11+
12+
[*]
13+
indent_style = space
14+
15+
[IntergalacticTransmission.cs]
16+
indent_size = 4
17+
18+
###############################
19+
# .NET Coding Conventions #
20+
###############################
21+
22+
# Organize usings
23+
dotnet_sort_system_directives_first = true
24+
dotnet_separate_import_directive_groups = true
25+
26+
# this. preferences
27+
dotnet_style_qualification_for_field = false:suggestion
28+
dotnet_style_qualification_for_property = false:suggestion
29+
dotnet_style_qualification_for_method = false:suggestion
30+
dotnet_style_qualification_for_event = false:suggestion
31+
32+
# Language keywords vs BCL types preferences
33+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
34+
dotnet_style_predefined_type_for_member_access = true:suggestion
35+
36+
# Parentheses preferences
37+
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
38+
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
39+
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
40+
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
41+
42+
# Modifier preferences
43+
dotnet_style_require_accessibility_modifiers = always:suggestion
44+
dotnet_style_readonly_field = true:suggestion
45+
46+
# Expression-level preferences
47+
dotnet_style_object_initializer = true:suggestion
48+
dotnet_style_collection_initializer = true:suggestion
49+
dotnet_style_explicit_tuple_names = true:suggestion
50+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
51+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
52+
dotnet_style_prefer_auto_properties = true:suggestion
53+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
54+
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
55+
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
56+
dotnet_style_coalesce_expression = true:suggestion
57+
dotnet_style_null_propagation = true:suggestion
58+
59+
###############################
60+
# Naming Conventions #
61+
###############################
62+
63+
# Style Definitions
64+
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
65+
66+
# Use PascalCase for constant fields
67+
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
68+
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
69+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
70+
dotnet_naming_symbols.constant_fields.applicable_kinds = field
71+
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
72+
dotnet_naming_symbols.constant_fields.required_modifiers = const
73+
74+
###############################
75+
# C# Code Style Rules #
76+
###############################
77+
78+
# var preferences
79+
csharp_style_var_for_built_in_types = true:none
80+
csharp_style_var_when_type_is_apparent = true:none
81+
csharp_style_var_elsewhere = true:none
82+
83+
# Expression-bodied members
84+
csharp_style_expression_bodied_methods = true:suggestion
85+
csharp_style_expression_bodied_constructors = true:suggestion
86+
csharp_style_expression_bodied_operators = true:suggestion
87+
csharp_style_expression_bodied_properties = true:suggestion
88+
csharp_style_expression_bodied_indexers = true:suggestion
89+
csharp_style_expression_bodied_accessors = true:suggestion
90+
91+
# Pattern-matching preferences
92+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
93+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
94+
95+
# Null-checking preferences
96+
csharp_style_throw_expression = true:suggestion
97+
csharp_style_conditional_delegate_call = true:suggestion
98+
99+
# Modifier preferences
100+
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
101+
102+
# Expression-level preferences
103+
csharp_prefer_braces = true:none
104+
csharp_prefer_simple_default_expression = true:suggestion
105+
csharp_style_deconstructed_variable_declaration = true:suggestion
106+
csharp_style_pattern_local_over_anonymous_function = true:suggestion
107+
csharp_style_inlined_variable_declaration = true:suggestion
108+
109+
###############################
110+
# C# Formatting Rules #
111+
###############################
112+
113+
# New line preferences
114+
csharp_new_line_before_open_brace = all
115+
csharp_new_line_before_else = true
116+
csharp_new_line_before_catch = true
117+
csharp_new_line_before_finally = true
118+
csharp_new_line_before_members_in_object_initializers = false
119+
csharp_new_line_before_members_in_anonymous_types = false
120+
csharp_new_line_between_query_expression_clauses = true
121+
122+
# Indentation preferences
123+
csharp_indent_case_contents = true
124+
csharp_indent_switch_labels = true
125+
csharp_indent_labels = flush_left
126+
127+
# Space preferences
128+
csharp_space_after_cast = false
129+
csharp_space_after_keywords_in_control_flow_statements = true
130+
csharp_space_between_method_declaration_parameter_list_parentheses = false
131+
csharp_space_between_method_call_parameter_list_parentheses = false
132+
csharp_space_before_colon_in_inheritance_clause = true
133+
csharp_space_after_colon_in_inheritance_clause = true
134+
csharp_space_around_binary_operators = before_and_after
135+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
136+
csharp_space_between_method_call_name_and_opening_parenthesis = false
137+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
138+
139+
# Wrapping preferences
140+
csharp_preserve_single_line_blocks = true
141+
csharp_preserve_single_line_statements = true
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
public static class IntergalacticTransmission
2+
{
3+
private const byte InitUpperMask = (byte)0xFE;
4+
5+
public static byte[] GetTransmitSequence(byte[] message)
6+
{
7+
List<byte> transmitSeq = new List<byte>();
8+
byte carry = 0;
9+
byte upperMask = InitUpperMask;
10+
11+
for (int i = 0; i < message.Length; i++)
12+
{
13+
if (upperMask == 0)
14+
{
15+
// The carry now contains 7 bits. Flush the carry out.
16+
transmitSeq.Add(AddParity(carry));
17+
carry = 0;
18+
upperMask = 0xFE;
19+
}
20+
21+
int shiftPlaces = byte.TrailingZeroCount(upperMask);
22+
int current = (carry << (8 - shiftPlaces)) | (message[i] >>> shiftPlaces);
23+
transmitSeq.Add(AddParity((byte)current));
24+
25+
// Update parameters for next round.
26+
carry = (byte)(message[i] & (~upperMask));
27+
28+
// Shorten the upper mask.
29+
upperMask = (byte)(upperMask << 1);
30+
}
31+
32+
if (upperMask != InitUpperMask)
33+
{
34+
byte lastGroup = (byte)(carry << byte.PopCount(upperMask));
35+
// We have left over carry data
36+
transmitSeq.Add(AddParity(lastGroup));
37+
}
38+
return transmitSeq.ToArray();
39+
}
40+
41+
private static byte AddParity(byte source)
42+
{
43+
if (byte.PopCount((byte)(source & 0x7F)) % 2 == 0)
44+
{
45+
return (byte)(source << 1);
46+
}
47+
return (byte)((source << 1) | 1);
48+
}
49+
50+
public static byte[] DecodeSequence(byte[] receivedSeq)
51+
{
52+
if (receivedSeq.Length == 0)
53+
{
54+
return [];
55+
}
56+
57+
List<byte> decodedMessage = new List<byte>();
58+
byte byteToAdd = 0x00;
59+
byte upperMask = 0xFF;
60+
for (int i = 0; i < receivedSeq.Length; i++)
61+
{
62+
if (upperMask == 0xFF)
63+
{
64+
// We've completed a complete round.
65+
// Current byte too short.
66+
byteToAdd = GetByteData(receivedSeq[i]);
67+
upperMask = 0x80;
68+
continue;
69+
}
70+
71+
byte currentByteData = GetByteData(receivedSeq[i]);
72+
byte contribution = (byte)(currentByteData >>> byte.TrailingZeroCount(upperMask));
73+
decodedMessage.Add((byte)(byteToAdd | contribution));
74+
75+
// Update parameters for next round
76+
byteToAdd = (byte)((currentByteData & ~(upperMask | 0x01)) << byte.PopCount(upperMask));
77+
upperMask = (byte)((upperMask >> 1) | 0x80);
78+
}
79+
return decodedMessage.ToArray();
80+
}
81+
82+
private static byte GetByteData(byte data)
83+
{
84+
if (byte.PopCount(data) % 2 != 0)
85+
{
86+
throw new ArgumentException("Byte has incorrect parity");
87+
}
88+
89+
return (byte)(data & 0xFE);
90+
}
91+
}

0 commit comments

Comments
 (0)