|
| 1 | +import java.util.ArrayList; |
1 | 2 | import java.util.List; |
2 | 3 |
|
3 | 4 | public class IntergalacticTransmission { |
4 | 5 |
|
5 | | - public static List<String> getTransmitSequence(List<String> message) { |
6 | | - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); |
7 | | - } |
| 6 | + private static final byte INIT_UPPER_MASK = (byte) 0xFE; |
8 | 7 |
|
9 | | - public static List<String> decodeSequence(List<String> sequence) { |
10 | | - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); |
11 | | - } |
| 8 | + public static List<Integer> getTransmitSequence(List<Integer> message) { |
| 9 | + List<Integer> transmitSeq = new ArrayList<>(); |
| 10 | + byte carry = 0; |
| 11 | + byte upperMask = INIT_UPPER_MASK; |
12 | 12 |
|
| 13 | + for (int i = 0; i < message.size(); i++) { |
| 14 | + byte currentByte = message.get(i).byteValue(); |
| 15 | + |
| 16 | + if (upperMask == 0) { |
| 17 | + transmitSeq.add((int) addParity(carry) & 0xFF); |
| 18 | + carry = 0; |
| 19 | + upperMask = (byte) 0xFE; |
| 20 | + } |
| 21 | + |
| 22 | + int shiftPlaces = Integer.numberOfTrailingZeros(upperMask & 0xFF); |
| 23 | + int current = ((carry & 0xFF) << (8 - shiftPlaces)) | ((currentByte & 0xFF) >>> shiftPlaces); |
| 24 | + transmitSeq.add((int) addParity((byte) current) & 0xFF); |
| 25 | + |
| 26 | + carry = (byte) (currentByte & ~upperMask); |
| 27 | + upperMask = (byte) (upperMask << 1); |
| 28 | + } |
| 29 | + |
| 30 | + if (upperMask != INIT_UPPER_MASK) { |
| 31 | + byte lastGroup = (byte) ((carry & 0xFF) << Integer.bitCount(upperMask & 0xFF)); |
| 32 | + transmitSeq.add((int) addParity(lastGroup) & 0xFF); |
| 33 | + } |
| 34 | + |
| 35 | + return transmitSeq; |
| 36 | + } |
| 37 | + |
| 38 | + private static byte addParity(byte source) { |
| 39 | + if (Integer.bitCount(source & 0x7F) % 2 == 0) { |
| 40 | + return (byte) (source << 1); |
| 41 | + } else { |
| 42 | + return (byte) ((source << 1) | 1); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static List<Integer> decodeSequence(List<Integer> receivedSeq) { |
| 47 | + if (receivedSeq.isEmpty()) { |
| 48 | + return new ArrayList<>(); |
| 49 | + } |
| 50 | + |
| 51 | + List<Integer> decodedMessage = new ArrayList<>(); |
| 52 | + byte byteToAdd = 0x00; |
| 53 | + byte upperMask = (byte) 0xFF; |
| 54 | + |
| 55 | + for (int i = 0; i < receivedSeq.size(); i++) { |
| 56 | + byte currentByte = receivedSeq.get(i).byteValue(); |
| 57 | + |
| 58 | + if (upperMask == (byte) 0xFF) { |
| 59 | + byteToAdd = getByteData(currentByte); |
| 60 | + upperMask = (byte) 0x80; |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + byte currentByteData = getByteData(currentByte); |
| 65 | + int shiftPlaces = Integer.numberOfTrailingZeros(upperMask & 0xFF); |
| 66 | + byte contribution = (byte) ((currentByteData & 0xFF) >>> shiftPlaces); |
| 67 | + decodedMessage.add((byteToAdd | contribution) & 0xFF); |
| 68 | + |
| 69 | + byteToAdd = (byte) (((currentByteData & ~(upperMask | 0x01)) & 0xFF) << Integer.bitCount(upperMask & 0xFF)); |
| 70 | + upperMask = (byte) (((upperMask & 0xFF) >>> 1) | 0x80); |
| 71 | + } |
| 72 | + |
| 73 | + return decodedMessage; |
| 74 | + } |
| 75 | + |
| 76 | + private static byte getByteData(byte data) { |
| 77 | + if (Integer.bitCount(data & 0xFF) % 2 != 0) { |
| 78 | + throw new IllegalArgumentException("Byte has incorrect parity"); |
| 79 | + } |
| 80 | + return (byte) (data & 0xFE); |
| 81 | + } |
13 | 82 | } |
0 commit comments