Advertisement
philRG

aoc day 16 (draft)

Dec 16th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. class SinglePacket:
  2.     version: int
  3.     a: int
  4.     b: int
  5.     c: int
  6.  
  7.     def __init__(self, version: int):
  8.         self.version = version
  9.  
  10.  
  11. file = 'input.txt'
  12. with open(file) as f:
  13.     binary_msg = bin(int(f.read(), 16))[2:]
  14.  
  15. """
  16.    Every packet begins with a standard header: the first three bits encode the packet version, and the next three bits encode the packet type ID.
  17. """
  18. print(binary_msg)
  19.  
  20. """ Packets with type ID 4 represent a literal value. Literal value packets encode a single binary number.
  21.        The three bits labeled V (110) are the packet version, 6.
  22.        The three bits labeled T (100) are the packet type ID, 4, which means the packet is a literal value.
  23.        The five bits labeled A (10111) start with a 1 (not the last group, keep reading) and contain the first four bits of the number, 0111.
  24.        The five bits labeled B (11110) start with a 1 (not the last group, keep reading) and contain four more bits of the number, 1110.
  25.        The five bits labeled C (00101) start with a 0 (last group, end of packet) and contain the last four bits of the number, 0101.
  26.        The three unlabeled 0 bits at the end are extra due to the hexadecimal representation and should be ignored.
  27. """
  28. """ Every other type of packet (any packet with a type ID other than 4) represent an operator that performs some calculation on one or more sub-packets contained within.
  29. """
  30. packet_start, packet_end = True, False
  31. while binary_msg:
  32.     if packet_start:
  33.         version = int(binary_msg[:3])
  34.         type_id = int(binary_msg[3:6])
  35.         if type_id == 4:
  36.             packet = SinglePacket(version)
  37.             packet.a = int(binary_msg[6: 11])
  38.             packet.b = int(binary_msg[11: 16])
  39.             packet.c = int(binary_msg[16: 21])
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement