Skip to content

Instantly share code, notes, and snippets.

@shirriff
Created December 7, 2017 18:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirriff/f4a45ba24ad19216225aa85525492f6b to your computer and use it in GitHub Desktop.
Save shirriff/f4a45ba24ad19216225aa85525492f6b to your computer and use it in GitHub Desktop.
Process air conditioner remote control data: find inputs differing in one bit and print the difference in the output.
import re
data="""\
10100001 10010011 01100011 => 01110111
10100001 10010011 01100100 => 01110001
10100001 10010011 01100101 => 01110000
10100001 10010011 01100110 => 01110010
10100001 10010011 01100111 => 01110011
10100001 10010011 01101000 => 01111001
10100001 10010011 01101001 => 01111000
10100001 10010011 01101010 => 01111010
10100001 10010011 01101011 => 01111011
10100001 10010011 01101100 => 01111110
10100001 10010011 01101101 => 01111111
10100001 10010011 01101110 => 01111100
10100001 10010011 01101111 => 01111101
10100001 10010011 01110001 => 01100100
10100001 10010011 01110010 => 01100110
10100001 10010011 01110011 => 01100111
10100001 10010011 01110100 => 01100001
10100001 10010011 01110101 => 01100000
10100001 10010011 01110111 => 01100011
10100001 10010011 01110111 => 01100011
10100001 10010011 01111000 => 01101001
10100001 10010011 01101000 => 01111001
10100001 00010011 01101000 => 11111001
10100001 00010011 01101100 => 11111110
10100001 10010011 01101100 => 01111110
10100001 10010100 01111110 => 01101011
10100001 10000010 01101100 => 01100000
10100001 10000001 01101100 => 01100011
10100001 10010011 01101100 => 01111110
10100001 10010000 01101100 => 01111100
10100001 10011000 01101100 => 01110100
10100001 10001000 01101100 => 01101100
10100001 10010000 01101100 => 01111100
10100001 10011000 01101100 => 01110100
10100010 00000010 11111111 => 01111110"""
data = [x.split('=>') for x in data.replace(' ', '').split('\n')]
def hamming(a, b):
c = 0
for i in range(0, len(a)):
if a[i] != b[i]:
c += 1
return c
def xor(a, b):
return ''.join(['0' if x[0] == x[1] else '1' for x in zip(a, b)])
# Prettyprint in groups of 4
def p(x):
return '/'.join(re.findall('....', x))
for i in range(0, len(data)):
for j in range(i+1, len(data)):
di = data[i]
dj = data[j]
ki = di[0]
kj = dj[0]
vi = di[1]
vj = dj[1]
if hamming(ki, kj) in [1]:
# print p(ki), p(kj), p(xor(ki, kj)), ':', p(vi), p(vj), p(xor(vi, vj))
print xor(ki, kj), ':', xor(vi, vj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment