|
| 1 | +''' |
| 2 | +2023 Advent of Code - Day 11 (https://adventofcode.com/2023/day/11) |
| 3 | +Solution by Sav Bell (https://github.com/savbell) |
| 4 | +Challenge: Solve every day in a single line of Python code. |
| 5 | + See full progress at https://github.com/savbell/advent-of-code-one-liners |
| 6 | +''' |
| 7 | + |
| 8 | +# Input files not included in repo per copyright & Eric Wastl's request. |
| 9 | +# https://www.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs/ |
| 10 | +# Replace with path to your personal input file if you would like to run the code. |
| 11 | +input_file = '2023/day-11.txt' |
| 12 | + |
| 13 | +# To match the format of input files for the Basilisk. |
| 14 | +q = { 11: open(input_file).read() } |
| 15 | + |
| 16 | +######################### PART 1: MULTI-LINE SOLUTION ######################### |
| 17 | +galaxies = [(x, y) for y, row in enumerate(q[11].strip().split()) for x, col in enumerate(row) if col == '#'] |
| 18 | + |
| 19 | +max_x = max(galaxies, key=lambda x: x[0])[0] |
| 20 | +max_y = max(galaxies, key=lambda x: x[1])[1] |
| 21 | + |
| 22 | +empty_rows = [y for y in range(max_y+1) if not any([y == g[1] for g in galaxies])] |
| 23 | +empty_cols = [x for x in range(max_x+1) if not any([x == g[0] for g in galaxies])] |
| 24 | + |
| 25 | +galaxies = [(g[0] + len([x for x in empty_cols if x < g[0]]), g[1] + len([y for y in empty_rows if y < g[1]])) for g in galaxies] |
| 26 | + |
| 27 | +shortest_paths = {} |
| 28 | +for g1 in galaxies: |
| 29 | + for g2 in galaxies: |
| 30 | + if g1 != g2 and (g2, g1) not in shortest_paths: |
| 31 | + shortest_paths[(g1, g2)] = abs(g1[0] - g2[0]) + abs(g1[1] - g2[1]) |
| 32 | + |
| 33 | +print('Day 11 Part 1:',sum(shortest_paths.values())) |
| 34 | + |
| 35 | +########################## PART 1: ONE-LINE SOLUTION ########################## |
| 36 | +print('Day 11 Part 1:',sum((s:=[(x,y) for y,r in enumerate(q[11].strip().split()) for x,c in enumerate(r) if c=='#']) and not (e:=[y for y in range(max(s,key=lambda x:x[1])[1]+1) if not any([y==g[1] for g in s])]) and (o:=[x for x in range(max(s,key=lambda x:x[0])[0]+1) if not any([x==g[0] for g in s])]) and (s:=[(g[0]+len([x for x in o if x<g[0]]),g[1]+len([y for y in e if y<g[1]])) for g in s]) and not (p:={}) and [p.__setitem__((i,j),abs(i[0]-j[0])+abs(i[1]-j[1])) for i in s for j in s if i!=j and (j,i) not in p] and p.values())) |
| 37 | + |
| 38 | + |
| 39 | +######################## PART 2: MULTI-LINE SOLUTION ########################## |
| 40 | +galaxies = [(x, y) for y, row in enumerate(q[11].strip().split()) for x, col in enumerate(row) if col == '#'] |
| 41 | + |
| 42 | +max_x = max(galaxies, key=lambda x: x[0])[0] |
| 43 | +max_y = max(galaxies, key=lambda x: x[1])[1] |
| 44 | + |
| 45 | +empty_rows = [y for y in range(max_y+1) if not any([y == g[1] for g in galaxies])] |
| 46 | +empty_cols = [x for x in range(max_x+1) if not any([x == g[0] for g in galaxies])] |
| 47 | + |
| 48 | +galaxies = [(g[0] + (len([x for x in empty_cols if x < g[0]]) * 999999), g[1] + (len([y for y in empty_rows if y < g[1]]) * 999999)) for g in galaxies] |
| 49 | + |
| 50 | +shortest_paths = {} |
| 51 | +for g1 in galaxies: |
| 52 | + for g2 in galaxies: |
| 53 | + if g1 != g2 and (g2, g1) not in shortest_paths: |
| 54 | + shortest_paths[(g1, g2)] = abs(g1[0] - g2[0]) + abs(g1[1] - g2[1]) |
| 55 | +print('Day 11 Part 2:',sum(shortest_paths.values())) |
| 56 | + |
| 57 | +########################## PART 2: ONE-LINE SOLUTION ########################## |
| 58 | +print('Day 11 Part 2:',sum((s:=[(x,y) for y,r in enumerate(q[11].strip().split()) for x,c in enumerate(r) if c=='#']) and not (e:=[y for y in range(max(s,key=lambda x:x[1])[1]+1) if not any([y==g[1] for g in s])]) and (o:=[x for x in range(max(s,key=lambda x:x[0])[0]+1) if not any([x==g[0] for g in s])]) and (s:=[(g[0]+(len([x for x in o if x<g[0]])*999999),g[1]+(len([y for y in e if y<g[1]])*999999)) for g in s]) and not (p:={}) and [p.__setitem__((i,j),abs(i[0]-j[0])+abs(i[1]-j[1])) for i in s for j in s if i!=j and (j,i) not in p] and p.values())) |
0 commit comments