Skip to content

Instantly share code, notes, and snippets.

@michaelskyba
Last active October 8, 2022 00:46
Show Gist options
  • Save michaelskyba/efc05b2c7aedc991c3563db65e6a73ec to your computer and use it in GitHub Desktop.
Save michaelskyba/efc05b2c7aedc991c3563db65e6a73ec to your computer and use it in GitHub Desktop.
import sys
# https://classroom.google.com/c/NTU0OTE4MjA4NzMw/p/NTU1MjExNjY2MTI0/details
# CLI usage: python rings.py input.txt
routes = []
with open(sys.argv[1]) as file:
routes = file.readlines()
for i, route in enumerate(routes):
routes[i] = route.rstrip()
routes[i] = routes[i].split(" ")
# Remove the first two entries (ID+1, R), which are useless metadata
routes[i] = routes[i][2:]
# Remove the first line, which is useless since we're going to be using one file
# for each "dataset"
routes = routes[1:]
# Find minimum roundabout diameter
min = int(routes[0][0])
for route in routes:
for roundabout in route:
num = int(roundabout)
if num < min:
min = num
# Find routes that contain that diameter
congested = []
for i, route in enumerate(routes):
for roundabout in route:
if int(roundabout) == min:
congested.append(i)
break
# The output wants 1-based indices and not 0-based
for i, route in enumerate(congested):
congested[i] += 1
# Convert congested to strings for output
for i, route in enumerate(congested):
congested[i] = str(congested[i])
# Follow output format
print(min, "{%s}" % ','.join(congested))
@michaelskyba
Copy link
Author

michaelskyba commented Oct 8, 2022

  • CBA to do user error handling
  • Assumes input is ordered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment