Advertisement
Guest User

Advent Day 25 Part 1

a guest
Dec 25th, 2023
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | Software | 0 0
  1. import re
  2. import sys
  3. from pprint import pprint
  4.  
  5. def advent_day25_part1(filestr):
  6.     field = [x for x in filestr.split('\n') if x]
  7.     nrows, ncols = len(field), len(field[0])
  8.  
  9.     connections = {}
  10.     for line in field:
  11.         m = re.split(r'[: ]+', line)
  12.         x = m[0]
  13.         for y in m[1:]:
  14.             connections.setdefault(x, set()).add(y)
  15.             connections.setdefault(y, set()).add(x)
  16.  
  17.     def group(item, ignore=set()):
  18.         visited = set()
  19.         q = [item]
  20.         while q:
  21.             i = q.pop()
  22.             if i in visited:
  23.                 continue
  24.             visited.add(i)
  25.             for j in connections[i]:
  26.                 segment = (min(i, j), max(i, j))
  27.                 if segment not in ignore:
  28.                     q.append(j)
  29.         return visited
  30.  
  31.     def walkspan(start):
  32.         q = [(start, tuple())]
  33.         span = {}
  34.         while q:
  35.             item, path = q.pop(0)
  36.             if item in span:
  37.                 continue
  38.             path += (item,)
  39.             if len(path) > 1:
  40.                 span[item] = path
  41.             for child in connections[item]:
  42.                 q.append((child, path))
  43.         return span
  44.  
  45.     spans = {}
  46.     for item in connections.keys():
  47.         spans[item] = walkspan(item)
  48.  
  49.     def countworn(spans):
  50.         worn = {}
  51.         for span in spans.values():
  52.             for route in span.values():
  53.                 n0 = route[0]
  54.                 for n1 in route[1:]:
  55.                     p = (min(n0, n1), max(n0, n1))
  56.                     if p in worn:
  57.                         worn[p] += 1
  58.                     else:
  59.                         worn[p] = 1
  60.                     n0 = n1
  61.         return worn
  62.  
  63.     worn = countworn(spans)
  64.     worn_ranked = sorted(worn.items(), key=lambda x: -x[1])
  65.     ignore = set(x[0] for x in worn_ranked[:3])
  66.     print('num nodes', len(connections))
  67.     print('top 10 visited segments')
  68.     pprint(worn_ranked[:10])
  69.     print('cuts', ignore)
  70.     g1 = group(worn_ranked[0][0][0], ignore)
  71.     g2 = group(worn_ranked[0][0][1], ignore)
  72.     print('group 1 size', len(g1))
  73.     print('group 2 size', len(g2))
  74.     print('product', len(g1) * len(g2))
  75.  
  76.  
  77. def readfile(file_path):
  78.     with open(file_path, 'r') as file:
  79.         return file.read()
  80.  
  81. if __name__ == '__main__':
  82.     if len(sys.argv) >= 2:
  83.         filestr = readfile(sys.argv[1])
  84.     else:
  85.         filestr = ''
  86.     advent_day25_part1(filestr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement