Skip to content

Instantly share code, notes, and snippets.

@shirriff shirriff/ac2.py
Created Dec 7, 2017

Embed
What would you like to do?
Analyze air conditioner checksum by reversing bytes and summing nibbles.
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')]
data = [(a[::-1], b[::-1]) for a, b in data]
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)])
for i in range(0, len(data)):
# Convert left nibbles to integers
l = [int(x, 2) for x in re.findall('....', data[i][0])]
# Convert right nibbles to integers
r = [int(x, 2) for x in re.findall('....', data[i][1])]
s1 = l[0]+l[2]+l[4]
print data[i][0], s1, r[0], '*' if (-s1-2) & 0xf != r[0] else ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.