Skip to content

Commit 6ac2e04

Browse files
committed
Add intergalactic transmission exercise
1 parent eea9a6c commit 6ac2e04

File tree

11 files changed

+670
-1
lines changed

11 files changed

+670
-1
lines changed

config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,18 @@
27132713
"for-loops"
27142714
],
27152715
"difficulty": 7
2716+
},
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+
],
2727+
"difficulty": 6
27162728
}
27172729
],
27182730
"foregone": [

exercises/Exercises.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.0.31808.319
@@ -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
Global
361363
GlobalSection(SolutionConfigurationPlatforms) = preSolution
362364
Debug|Any CPU = Debug|Any CPU
@@ -1063,6 +1065,10 @@ Global
10631065
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Debug|Any CPU.Build.0 = Debug|Any CPU
10641066
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Release|Any CPU.ActiveCfg = Release|Any CPU
10651067
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Release|Any CPU.Build.0 = Release|Any CPU
1068+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1069+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
1070+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
1071+
{E81F1BA3-1F99-4DCB-B875-78D1F4750BD5}.Release|Any CPU.Build.0 = Release|Any CPU
10661072
EndGlobalSection
10671073
GlobalSection(SolutionProperties) = preSolution
10681074
HideSolutionNode = FALSE
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Instructions
2+
3+
Your job is to help implement the message sequencer to add the parity bit to the messages and the decoder to receive messages.
4+
5+
The entire message, itself, is sequence of a number of bytes.
6+
The transmitters and receivers can only transmit and receive one byte at a time, so parity bit needs to be added every eighth bit.
7+
The algorithm for adding the bits is as follows:
8+
1. Divide the message bits into groups of 7, starting from the left (the message is transmitted from left to right).
9+
2. If the last group has less than 7 bits, append some 0s to pad the group to 7 bits.
10+
3. For each group, determine if there are an odd or even number of 1s.
11+
4. If the group has even number of 1s or none at all, append a 0 to the group. Otherwise, append 1 if there is odd number.
12+
13+
For example, consider the message 0xC0_01_C0_DE.
14+
Writing this out in binary and locating every 7th bit:
15+
16+
```text
17+
C 0 0 1 C 0 D E
18+
1100_0000 0000_0001 1100_0000 1101_1110
19+
↑ ↑ ↑ ↑ (7th bits)
20+
```
21+
22+
The last group has only 4 bits (0b1110), so three 0 bits are appended to make it 7 bits:
23+
24+
```text
25+
| 1100_000 | 0000_000 | 0111_000 | 0001_101 | 1110_000 |
26+
```
27+
28+
The first group contains two 1s (an even number of 1s), so 0 is appended to the group.
29+
The second group has none, so append 0.
30+
The rest have three, so they append 1.
31+
32+
```text
33+
| 1100_0000 | 0000_0000 | 0111_0001 | 0001_1011 | 1110_0001 |
34+
| C 0 | 0 0 | 7 1 | 1 B | E 1 | (in hex)
35+
```
36+
37+
Thus, the transmission sequence is 0xC0_00_71_1B_E1.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Introduction
2+
3+
Trillions upon trillions of messages are exchanged between Earth and the neighboring galaxies every millisecond.
4+
But transmission over such long distances faces many obstacles.
5+
Pesky solar flares, worm holes, temporal distortions, stray forces, heck even the flap of a space butterfly wing can cause a random bit to flip during transmission.
6+
The difference between an "o" and a "k", a "[" and "{" or a cowboy emoji and a clown emoji can have huge ramifications, from missed orders to Galactic Stockmarket crashes.
7+
It is important for the receiver to know when the message has been corrupted so that the receiver can ask for the message to sent again.
8+
9+
One day, an important message comes through.
10+
People have just heard about the legend about a way of transmitting in such a way that the receiver can tell when bit flipped ... the parity bit ...
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"kahgoh"
4+
],
5+
"files": {
6+
"solution": [
7+
"IntergalacticTransmission.cs"
8+
],
9+
"test": [
10+
"IntergalacticTransmissionTests.cs"
11+
],
12+
"example": [
13+
".meta/Example.cs"
14+
]
15+
},
16+
"blurb": "Help detect errors in transmissions to neighboring galaxies"
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public static class IntergalacticTransmission
2+
{
3+
public static byte[] GetTransmitSequence(byte[] message)
4+
{
5+
throw new NotImplementedException("You need to implement this method.");
6+
}
7+
8+
public static byte[] DecodeSequence(byte[] receivedSeq)
9+
{
10+
throw new NotImplementedException("You need to implement this method.");
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net9.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<Using Include="Xunit" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
14+
<PackageReference Include="xunit.v3" Version="1.1.0" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" />
16+
<PackageReference Include="Exercism.Tests.xunit.v3" Version="0.1.0-beta1" />
17+
</ItemGroup>
18+
</Project>

0 commit comments

Comments
 (0)