from struct import pack with open('inputbin', 'wb') as f: for action, start, end in (({'turn on': 1, 'turn off': -1, 'toggle': 2}[action], tuple(map(int, begin.split(','))), tuple(map(int, end.split(',')))) for action, begin, end in ((*l.rsplit(' ', maxsplit=1), r) for l, r in (l.rstrip('\n').split(' through ') for l in open('input')))): f.write(pack('>bHHHH', action, *start, *end))