|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +def load_data(filename): |
| 4 | + with open(filename, 'r') as f: |
| 5 | + for line in f: |
| 6 | + line = line.rstrip('\n') |
| 7 | + yield line |
| 8 | + |
| 9 | +# Part One |
| 10 | + |
| 11 | +office_designer_s_favorite_number = int(next(load_data('input.txt'))) |
| 12 | + |
| 13 | +def is_wall(x, y): |
| 14 | + global office_designer_s_favorite_number |
| 15 | + n = x*x + 3*x + 2*x*y + y + y*y |
| 16 | + n += office_designer_s_favorite_number |
| 17 | + return n.bit_count() & 1 |
| 18 | + |
| 19 | +import networkx as nx |
| 20 | + |
| 21 | +G = nx.Graph() |
| 22 | + |
| 23 | +s = (1, 1) |
| 24 | +e = (31, 39) |
| 25 | + |
| 26 | +for y in range(50): |
| 27 | + for x in range(50): |
| 28 | + if is_wall(x, y): |
| 29 | + continue |
| 30 | + if not is_wall(x+1, y): |
| 31 | + G.add_edge( (x, y), (x+1, y), weight=1 ) |
| 32 | + if not is_wall(x, y+1): |
| 33 | + G.add_edge( (x, y), (x, y+1), weight=1 ) |
| 34 | + |
| 35 | +result = len(nx.shortest_path(G, s, e)) - 1 |
| 36 | + |
| 37 | +print(result) |
| 38 | + |
| 39 | +# Part Two |
| 40 | + |
| 41 | +lengths = nx.single_source_dijkstra_path_length(G, s, cutoff=50, weight='weight') |
| 42 | + |
| 43 | +result = len(lengths) |
| 44 | + |
| 45 | +print(result) |
| 46 | + |
0 commit comments