Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # Input: lines like "<code>-<code>"
  2. # Output: how many sets of three codes are connected to each other,
  3. #         and at least one starts with "t"?
  4.  
  5. connections = {}
  6.  
  7. file = open("23_input.txt", "r")
  8. for line in file:
  9.   line = line.replace("\n", "")
  10.   parts = line.split("-")
  11.   if not (parts[0] in connections):
  12.     connections[parts[0]] = []
  13.   connections[parts[0]].append(parts[1])
  14.   if not (parts[1] in connections):
  15.     connections[parts[1]] = []
  16.   connections[parts[1]].append(parts[0])
  17.  
  18. total = 0
  19. for code in connections:
  20.   if code[0] == "t":
  21.     other_codes = connections[code]
  22.     for i in range(len(other_codes) - 1):
  23.       if other_codes[i][0] == "t" and other_codes[i] < code:
  24.         continue
  25.       for i2 in range(i + 1, len(other_codes)):
  26.         if other_codes[i2][0] == "t" and other_codes[i2] < code:
  27.           continue
  28.         if other_codes[i] in connections[other_codes[i2]]:
  29.           total += 1
  30. print (total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement