Advertisement
Guest User

Untitled

a guest
Dec 19th, 2023
1,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | Source Code | 0 0
  1. # Input: lines like "<stuff> -> <other stuff>"
  2. #        <stuff> starts with %: initially off, low and off -> on and sends high, low and on -> off and low
  3. #                            &: initially remember each input as low, receives -> all high -> low, else high
  4. #                "broadcaster" sends input to all outputs in order
  5. #        evaluate breadth-first
  6. # Output: how many times must you send low to "broadcaster" before "rx" receives a low pulse?
  7.  
  8. rules = {}
  9. button_count = 0
  10.  
  11. file = open("20_input.txt", "r")
  12. for line in file:
  13.     line = line.replace("\n", "")
  14.     if line == "":
  15.         continue
  16.     prefix, suffix = line.split(" -> ")
  17.     if prefix == "broadcaster":
  18.         type = ""
  19.         module = prefix
  20.     else:
  21.         type = prefix[0]
  22.         module = prefix[1:]
  23.     self_state = "off" # only relevant for "%"
  24.     input_states = {} # only relevant for "&"
  25.     rules[module] = [type, suffix.split(", "), self_state, input_states]
  26.  
  27. for module in rules:
  28.     for target in rules[module][1]:
  29.         if target in rules:
  30.             rules[target][3][module] = "low"
  31.  
  32. finished = False
  33. while not finished:
  34.     inputs = [["button", "broadcaster", "low"]]
  35.     button_count += 1
  36.     while len(inputs) > 0:
  37.         input = inputs[0]
  38.         inputs = inputs[1:]
  39.         source, module, level = input[0], input[1], input[2]
  40.         if module == "rx" and level == "low":
  41.             finished = True
  42.             break
  43.         if not (module in rules):
  44.             continue
  45.         if module == "broadcaster":
  46.             for target in rules[module][1]:
  47.                 inputs.append([module, target, level])
  48.             continue
  49.         if rules[module][0] == "%" and level == "low":
  50.             if rules[module][2] == "off":
  51.                 rules[module][2] = "on"
  52.                 for target in rules[module][1]:
  53.                     inputs.append([module, target, "high"])
  54.             else:
  55.                 rules[module][2] = "off"
  56.                 for target in rules[module][1]:
  57.                     inputs.append([module, target, "low"])
  58.         if rules[module][0] == "&":
  59.             rules[module][3][source] = level
  60.             if "low" in rules[module][3].values():
  61.                 new_level = "high"
  62.             else:
  63.                 new_level = "low"
  64.             # in your case, the endpoints are &(these) -> &gh -> rx
  65.             if module in ["rk", "cd", "zf", "qx"] and new_level == "high":
  66.                 print ("Debug", button_count, "/", module, "/", new_level)
  67.             # and the results are 3733 rk, 3793 cd, 3947 zf, 4057 qx
  68.             # so the answer is lcm(those values)
  69.             for target in rules[module][1]:
  70.                 inputs.append([module, target, new_level])
  71.  
  72. print (button_count)
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement