Skip to content

Commit 361be80

Browse files
authored
Add intergalactic-transmission (#1062)
* Add intergalactic-transmission * remove redundant #define
1 parent c430789 commit 361be80

File tree

14 files changed

+4491
-0
lines changed

14 files changed

+4491
-0
lines changed

config.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,20 @@
11421142
"functions",
11431143
"memory_management"
11441144
]
1145+
},
1146+
{
1147+
"slug": "intergalactic-transmission",
1148+
"name": "Intergalactic Transmission",
1149+
"uuid": "aa30f9ef-014e-4abb-aee3-fc0613fe20ae",
1150+
"practices": [],
1151+
"prerequisites": [],
1152+
"difficulty": 6,
1153+
"topics": [
1154+
"arrays",
1155+
"bitwise_operations",
1156+
"control_flow_if_statements",
1157+
"control_flow_loops"
1158+
]
11451159
}
11461160
]
11471161
},
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [],
3+
"files": {
4+
"solution": [
5+
"intergalactic_transmission.c",
6+
"intergalactic_transmission.h"
7+
],
8+
"test": [
9+
"test_intergalactic_transmission.c"
10+
],
11+
"example": [
12+
".meta/example.c",
13+
".meta/example.h"
14+
]
15+
},
16+
"blurb": "Add parity bits to a message for transmission",
17+
"source": "Kah Goh",
18+
"source_url": "https://github.com/exercism/problem-specifications/pull/2543"
19+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "intergalactic_transmission.h"
2+
3+
int transmit_sequence(uint8_t *buffer, const uint8_t *message,
4+
int message_length)
5+
{
6+
uint8_t cur = 0;
7+
int parity = 0;
8+
int byte_count = 0;
9+
int bit_count = 0;
10+
// go over each input byte
11+
for (int i = 0; i < message_length; ++i) {
12+
// read bits of input byte, starting from most significant bit
13+
for (int j = 0; j < 8; ++j) {
14+
uint8_t const bit = (message[i] >> (7 - j)) & 1;
15+
// keep track of number of set bits
16+
parity += bit;
17+
// set bit in output byte and shift to make room for next bit
18+
cur = (cur | bit) << 1;
19+
// if 7 bits have been written, output byte is ready
20+
if (++bit_count == 7) {
21+
// set parity bit
22+
cur |= parity & 1;
23+
// and write to output buffer
24+
buffer[byte_count++] = cur;
25+
// reset state for next output byte
26+
parity = 0;
27+
bit_count = 0;
28+
cur = 0;
29+
}
30+
}
31+
}
32+
// make sure to write remaining state
33+
if (bit_count > 0) {
34+
// shift to the left to fill up with zero bits
35+
cur <<= 7 - bit_count;
36+
// set parity bit
37+
cur |= parity & 1;
38+
// and write to output buffer
39+
buffer[byte_count++] = cur;
40+
}
41+
return byte_count;
42+
}
43+
44+
int decode_message(uint8_t *buffer, const uint8_t *message, int message_length)
45+
{
46+
uint8_t cur_dec = 0;
47+
int num_bits_left = 8;
48+
int num_decoded = 0;
49+
// iterate over input message: each byte has 7 bits payload and parity bit
50+
for (int i = 0; i < message_length; ++i) {
51+
uint8_t const payload = message[i] >> 1;
52+
uint8_t const parity = message[i] & 1;
53+
// check parity bit and return early if mismatch found
54+
if ((__builtin_popcount(payload) & 1) != parity)
55+
return WRONG_PARITY;
56+
// write payload bits to current output byte, starting from MSB
57+
for (int j = 6; j >= 0; --j) {
58+
// read bit
59+
uint8_t const bit = (payload >> j) & 1;
60+
// write bit to currently decoded byte
61+
cur_dec |= bit << (num_bits_left - 1);
62+
// bits left?
63+
if (--num_bits_left == 0) {
64+
// no bits left, write decoded byte to buffer
65+
buffer[num_decoded++] = cur_dec;
66+
// reset state
67+
cur_dec = 0;
68+
num_bits_left = 8;
69+
}
70+
}
71+
}
72+
return num_decoded;
73+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef INTERGALACTIC_TRANSMISSION_H
2+
#define INTERGALACTIC_TRANSMISSION_H
3+
4+
#include <stdint.h>
5+
6+
#define WRONG_PARITY -1
7+
8+
int transmit_sequence(uint8_t *buffer, const uint8_t *message,
9+
int message_length);
10+
11+
int decode_message(uint8_t *buffer, const uint8_t *message, int message_length);
12+
13+
#endif
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[f99d4046-b429-4582-9324-f0bcac7ab51c]
13+
description = "calculate transmit sequences -> empty message"
14+
15+
[ee27ea2d-8999-4f23-9275-8f6879545f86]
16+
description = "calculate transmit sequences -> 0x00 is transmitted as 0x0000"
17+
18+
[97f27f98-8020-402d-be85-f21ba54a6df0]
19+
description = "calculate transmit sequences -> 0x02 is transmitted as 0x0300"
20+
21+
[24712fb9-0336-4e2f-835e-d2350f29c420]
22+
description = "calculate transmit sequences -> 0x06 is transmitted as 0x0600"
23+
24+
[7630b5a9-dba1-4178-b2a0-4a376f7414e0]
25+
description = "calculate transmit sequences -> 0x05 is transmitted as 0x0581"
26+
27+
[ab4fe80b-ef8e-4a99-b4fb-001937af415d]
28+
description = "calculate transmit sequences -> 0x29 is transmitted as 0x2881"
29+
30+
[4e200d84-593b-4449-b7c0-4de1b6a0955e]
31+
description = "calculate transmit sequences -> 0xc001c0de is transmitted as 0xc000711be1"
32+
33+
[fbc537e9-6b21-4f4a-8c2b-9cf9b702a9b7]
34+
description = "calculate transmit sequences -> six byte message"
35+
36+
[d5b75adf-b5fc-4f77-b4ab-77653e30f07c]
37+
description = "calculate transmit sequences -> seven byte message"
38+
39+
[6d8b297b-da1d-435e-bcd7-55fbb1400e73]
40+
description = "calculate transmit sequences -> eight byte message"
41+
42+
[54a0642a-d5aa-490c-be89-8e171a0cab6f]
43+
description = "calculate transmit sequences -> twenty byte message"
44+
45+
[9a8084dd-3336-474c-90cb-8a852524604d]
46+
description = "decode received messages -> empty message"
47+
48+
[879af739-0094-4736-9127-bd441b1ddbbf]
49+
description = "decode received messages -> zero message"
50+
51+
[7a89eeef-96c5-4329-a246-ec181a8e959a]
52+
description = "decode received messages -> 0x0300 is decoded to 0x02"
53+
54+
[3e515af7-8b62-417f-960c-3454bca7f806]
55+
description = "decode received messages -> 0x0581 is decoded to 0x05"
56+
57+
[a1b4a3f7-9f05-4b7a-b86e-d7c6fc3f16a9]
58+
description = "decode received messages -> 0x2881 is decoded to 0x29"
59+
60+
[2e99d617-4c91-4ad5-9217-e4b2447d6e4a]
61+
description = "decode received messages -> first byte has wrong parity"
62+
63+
[507e212d-3dae-42e8-88b4-2223838ff8d2]
64+
description = "decode received messages -> second byte has wrong parity"
65+
66+
[b985692e-6338-46c7-8cea-bc38996d4dfd]
67+
description = "decode received messages -> 0xcf4b00 is decoded to 0xce94"
68+
69+
[7a1f4d48-696d-4679-917c-21b7da3ff3fd]
70+
description = "decode received messages -> 0xe2566500 is decoded to 0xe2ad90"
71+
72+
[467549dc-a558-443b-80c5-ff3d4eb305d4]
73+
description = "decode received messages -> six byte message"
74+
75+
[1f3be5fb-093a-4661-9951-c1c4781c71ea]
76+
description = "decode received messages -> seven byte message"
77+
78+
[6065b8b3-9dcd-45c9-918c-b427cfdb28c1]
79+
description = "decode received messages -> last byte has wrong parity"
80+
81+
[98af97b7-9cca-4c4c-9de3-f70e227a4cb1]
82+
description = "decode received messages -> eight byte message"
83+
84+
[aa7d4785-2bb9-43a4-a38a-203325c464fb]
85+
description = "decode received messages -> twenty byte message"
86+
87+
[4c86e034-b066-42ac-8497-48f9bc1723c1]
88+
description = "decode received messages -> wrong parity on 16th byte"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "intergalactic_transmission.h"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef INTERGALACTIC_TRANSMISSION_H
2+
#define INTERGALACTIC_TRANSMISSION_H
3+
4+
#include <stdint.h>
5+
6+
#define WRONG_PARITY -1
7+
8+
int transmit_sequence(uint8_t *buffer, const uint8_t *message,
9+
int message_length);
10+
11+
int decode_message(uint8_t *buffer, const uint8_t *message, int message_length);
12+
13+
#endif
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
### If you wish to use extra libraries (math.h for instance),
2+
### add their flags here (-lm in our case) in the "LIBS" variable.
3+
4+
LIBS = -lm
5+
6+
###
7+
CFLAGS = -std=c99
8+
CFLAGS += -g
9+
CFLAGS += -Wall
10+
CFLAGS += -Wextra
11+
CFLAGS += -pedantic
12+
CFLAGS += -Werror
13+
CFLAGS += -Wmissing-declarations
14+
CFLAGS += -DUNITY_SUPPORT_64 -DUNITY_OUTPUT_COLOR
15+
16+
ASANFLAGS = -fsanitize=address
17+
ASANFLAGS += -fno-common
18+
ASANFLAGS += -fno-omit-frame-pointer
19+
20+
.PHONY: test
21+
test: tests.out
22+
@./tests.out
23+
24+
.PHONY: memcheck
25+
memcheck: ./*.c ./*.h
26+
@echo Compiling $@
27+
@$(CC) $(ASANFLAGS) $(CFLAGS) test-framework/unity.c ./*.c -o memcheck.out $(LIBS)
28+
@./memcheck.out
29+
@echo "Memory check passed"
30+
31+
.PHONY: clean
32+
clean:
33+
rm -rf *.o *.out *.out.dSYM
34+
35+
tests.out: ./*.c ./*.h
36+
@echo Compiling $@
37+
@$(CC) $(CFLAGS) test-framework/unity.c ./*.c -o tests.out $(LIBS)

0 commit comments

Comments
 (0)