Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Input: lines like "<code>-<code>"
- # Output: how many sets of three codes are connected to each other,
- # and at least one starts with "t"?
- connections = {}
- file = open("23_input.txt", "r")
- for line in file:
- line = line.replace("\n", "")
- parts = line.split("-")
- if not (parts[0] in connections):
- connections[parts[0]] = []
- connections[parts[0]].append(parts[1])
- if not (parts[1] in connections):
- connections[parts[1]] = []
- connections[parts[1]].append(parts[0])
- total = 0
- for code in connections:
- if code[0] == "t":
- other_codes = connections[code]
- for i in range(len(other_codes) - 1):
- if other_codes[i][0] == "t" and other_codes[i] < code:
- continue
- for i2 in range(i + 1, len(other_codes)):
- if other_codes[i2][0] == "t" and other_codes[i2] < code:
- continue
- if other_codes[i] in connections[other_codes[i2]]:
- total += 1
- print (total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement