r/adventofcode • • Dec 11 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 11 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's secret ingredient is… *whips off cloth covering and gestures grandly*

Upping the Ante Again

Chefs should always strive to improve themselves. Keep innovating, keep trying new things, and show us how far you've come!

  • If you thought Day 1's secret ingredient was fun with only two variables, this time around you get one!
  • Don’t use any hard-coded numbers at all. Need a number? I hope you remember your trigonometric identities...
  • Esolang of your choice
  • Impress VIPs with fancy buzzwords like quines, polyglots, reticulating splines, multi-threaded concurrency, etc.

ALLEZ CUISINE!

Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!


--- Day 11: Cosmic Expansion ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:09:18, megathread unlocked!

27 Upvotes

843 comments sorted by

11

u/Smylers Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Vim keystrokes]

Another visual one!† Load your input, type in the following, and watch the galaxies get marked off and the total increase:

:se fo=⟨Enter⟩qa:g/^\.\+$/y|pu⟨Enter⟩Go⟨Esc⟩q
qbqqb/\%^.⟨Enter⟩⟨Ctrl+V⟩}kd:$pu⟨Enter⟩v'[gJ@bq@b{dgg@a@b{kdgg
/#⟨Enter⟩r|mmqcqqcnr-gg
C⟨Ctrl+R⟩=⟨Ctrl+R⟩-+line("''")-line("'m")+abs(col("''")-col("'m"))⟨Enter⟩⟨Esc⟩
@cq@cqdqqd:%s/-/#/g⟨Enter⟩gg:redr⟨Enter⟩/#⟨Enter⟩r|mm:norm@c⟨Enter⟩@dq@dgg
  • Record @a, which duplicates any rows containing no galaxies.
  • With @b, loop round turning columns into rows, to transpose the grid.
  • Do @a again, to duplicate rows that were originally columns.
  • Do @b again, to put the rows and columns back. (I suspect this step is unnecessary and the maths would've worked out the same with a transposed grid, but it was easier to check against the example with it the right way round.)
  • Stick a zero on the top line.
  • Find the first galaxy we haven't measured from yet. Replace it with | to mark it as processed, and remember its position with mm.
  • Record @c, which loops round each remaining #, replaces it with -, and updates the total with the vertical and horizontal difference from the galaxy found in the previous step.
  • Record @d, which turns all the -s back into #s, goes to the first one, then invokes @c to measure from that galaxy, then loops itself with @d to process from each galaxy in turn.

Update: Removed the only number from my solution, to comply with [Allez Cuisine!]. It was a zero, to initialize the totalizer. But it turned out not to be needed: D happily deletes nothing, ⟨Ctrl+R⟩-, happily re-inserts that nothing, and the first distance calculation of the form +2-1+abs(8-4) works just fine without a leading zero; instead of the first + adding the previous total and the new distance it simply act as a no-op unary plus on the first number.

I'm claiming my solution already contained exactly one ‘variable’, because mm is used to store the cursor position in the 'm mark, and a named mark is pretty much the same thing as a variable. (Whereas keyboard macros are more like subroutines, containing re-usable sets of instructions.)

Just a shame I couldn't think of an esoteric language to program this in ...

† I solved yesterday's quite late. It also animates as it solves, so if you like this kind of thing and haven't seen it, do take a look.

2

u/daggerdragon Dec 11 '23

Update: Removed the only number from my solution, to comply with [Allez Cuisine!].

Well done, chef :D

4

u/Fyvaproldje Dec 11 '23

[LANGUAGE: Raku] [Allez Cuisine!]

https://github.com/DarthGandalf/advent-of-code/blob/master/2023/DayEleven.rakumod

Here's a sample of a digitless calculation of the distance the stars travelled.

my $dist = 'xxx';
my $how = $dist ~ $dist;
$dist = $dist x $dist.chars;
$dist = $dist.chars x $how.chars;

1

u/daggerdragon Dec 11 '23

digitless calculation

Ain't nobody got time for numbers anyway. Well done, chef!

→ More replies (2)

4

u/Efficient_Beyond5000 Dec 11 '23 edited Dec 11 '23

[Language: Scratch]

https://scratch.mit.edu/projects/938348263/

Press R to reset, right click on the list to import the test or the input.

Click on the green flag to start computation and see a visualization of the galaxies.

[edit: remember to use turbo warp, to enable shift+green flag]

2

u/daggerdragon Dec 11 '23

[edit: remember to use turbo warp, to enable shift+green flag]

Turbo warp, you say...

FWOOOM

BARF: "What the heck was that?"
LONE STARR: "... Spaceball One..."
BARF: "They've gone to plaid!"

3

u/flwyd Dec 11 '23 edited Dec 11 '23

[Language: Julia] (on GitHub)

The Allez Cuisine challenge "Don't use any hard-coded numbers at all" when the problem statement involves the number one million :-/

Amusingly, I started playing around to see how to insert rows and columns in a 2D array in Julia, but couldn't find an elegant way to do it, so went for manhattan distance plus the range between end points intersecting with a list of empty rows and empty columns. Part 2 wasn't quite changing one number, but almost as easy.

part1(lines) = solve(lines, 2)
part2(lines) = solve(lines, 1_000_000)

function solve(lines, factor)
  grid, galaxies, emptyrows, emptycols = parseinput(lines)
  map(enumerate(galaxies)) do (i, g)
    [dist(g, x, emptyrows, emptycols, factor) for x in galaxies[i+1:end]]
  end |> Iterators.flatten |> sum
end

function dist(a, b, emptyrows, emptycols, factor)
  arow, acol = Tuple(a)
  brow, bcol = Tuple(b)
  minrow, maxrow = minmax(arow, brow)
  mincol, maxcol = minmax(acol, bcol)
  height = maxrow - minrow + (factor-1) * length((minrow:maxrow) ∩ emptyrows)
  width = maxcol - mincol + (factor-1) * length((mincol:maxcol) ∩ emptycols)
  width + height
end

function parseinput(lines)
  grid = reduce(hcat, [collect(line) for line in lines])
  galaxies = findall(==('#'), grid)
  emptyrows = findall(x -> all(==('.'), x), eachrow(grid))
  emptycols = findall(x -> all(==('.'), x), eachcol(grid))
  grid, galaxies, emptyrows, emptycols
end

There are probably ways to make this terser, but I'm tired and I think this is nicely readable.

2

u/daggerdragon Dec 11 '23

The Allez Cuisine challenge "Don't use any hard-coded numbers at all" when the problem statement involves the number one million :-/

Oh, it can be done. A couple of other chefs in this megathread have come up with some truly inspired solutions. Imma issue you a...

MODERATOR CHALLENGE

(note: this is only for /u/flwyd to complete!)

Alter your Day 11 solution to use no hard-coded numbers whatsoever. When you're done, reply to this challenge comment and show us your new code!

→ More replies (2)

3

u/arthurno1 Dec 11 '23 edited Dec 11 '23

[LANGUAGE: EmacsLisp]

The code was too long, so to please an angry bot here is the link:

https://github.com/amno1/AOC2023/blob/main/11.el

No expansion needed, it is just to calculate coefficient, which was relatively easy in itself.

1

u/daggerdragon Dec 11 '23

Your code block is too long for the megathreads. Please edit your comment to replace your oversized code with an external link to your code.

3

u/[deleted] Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Forth] [Allez Cuisine!]

Who needs numbers and variables when you have a stack and ✨words✨? Forth's core library gives you all you could ever need for numbers: true (-1), false (0), 1+, and 1-. Calculating the multiplier for part 2 was a piece of cake after defining some helper words:

false 9+ 10* 9+ 10* 9+ 10* 9+ 10* 9+ 10* 9+ empty-multiplier !

For non-constant values that need to be remembered throughout the program, I kept a pointer to some dictionary cells on the bottom of the stack. Then, some helper words returned addresses indexed from that pointer:

here \ keep on stack for entire program
false , false , false 1+ , false , false ,

: width             depth 1- pick ;
: height            depth 1- pick cell+ ;
: empty-multiplier  depth 1- pick cell+ cell+ ;
: empty-rows        depth 1- pick cell+ cell+ cell+ ;
: empty-cols        depth 1- pick cell+ cell+ cell+ cell+ ;
: _map              depth 1- pick cell+ cell+ cell+ cell+ cell+ ;

Cuisine code here edit: Refactored to remove all instances of digits in the code.

Regular, optimized code here. Got it down to solving both parts in around 5ms.

2

u/daggerdragon Dec 11 '23

Who needs numbers and variables when you have a stack and ✨words✨?

*chef's kiss* Bravo, chef.

2

u/naclmolecule Dec 11 '23

[LANGUAGE: Python]

I'm sure people smarter than me have already figured out that the axes are independent:

def parse_raw():
    universe = RAW.splitlines()
    ys = [y for y, line in enumerate(universe) for char in line if char == "#"]
    xs = [x for line in universe for x, char in enumerate(line) if char == "#"]
    ys.sort()
    xs.sort()
    return np.array(ys, np.int64), np.array(xs, np.int64)

YS, XS = parse_raw()

def expand(axis, n):
    diffs = np.diff(axis) - 1
    diffs[diffs < 0] = 0
    axis[1:] += np.cumsum(diffs) * n
    return axis

def expand_universe(n):
    return sum(
        abs(a - b)
        for axis in [expand(YS.copy(), n), expand(XS.copy(), n)]
        for a, b in combinations(axis, 2)
    )

print(expand_universe(1), expand_universe(999_999)

1

u/daggerdragon Dec 11 '23

The triple-backticks code fence (`​`​`) only works on new.reddit. Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read.

→ More replies (1)

2

u/optimistic-thylacine Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Rust] 🦀

Just a matter of getting the taxicab distance between pairs of points and using a prefix sum array to calculate their row and column distances.

The code below uses a custom 0-based BIT/Fenwick tree. The code linked uses simple prefix sum arrays instead, but was too large to post in the thread.

If you want to play around with the Fenwick tree, you can get it here.

Full solution

fn solution(path: &str, expansion: i64) -> Result<(), Box<dyn Error>> {
    let file   = File::open(path)?;
    let reader = BufReader::new(file);
    let lines  = reader.lines();

    let mut col_dists = Fenwick::from(vec![expansion; 256]);
    let mut row_dists = Fenwick::from(vec![expansion; 256]);
    let mut galaxies  = Vec::new();

    for (i, line) in lines.enumerate() {
        let line = line?;
        for (j, char) in line.chars().enumerate() {
            if char == '#' {
                galaxies.push((i, j));
                row_dists.set(i, 1);
                col_dists.set(j, 1);
            }
        }
    }
    let mut sum = 0;

    for (i, g1) in (1..).zip(&galaxies) {
        for g2 in &galaxies[i..] {
            // Taxi distance.
            sum += row_dists.range_sum2(g1.0.min(g2.0), g2.0.max(g1.0)) +
                   col_dists.range_sum2(g1.1.min(g2.1), g2.1.max(g1.1));
        }
    }
    println!("Sum of shortest paths: {:>12}", sum);
    Ok(())
}

1

u/daggerdragon Dec 11 '23 edited Dec 11 '23

Your code block is too long for the megathreads. Please edit your comment to replace your oversized code with an external link to your code. edit: 👍

→ More replies (2)
→ More replies (2)

2

u/kevinwangg Dec 11 '23 edited Dec 12 '23

[LANGUAGE: PYTHON] [ALLEZ CUISINE!]

leaderboard: 14 / 2

Got second on the leaderboard today! That's by far my best placement so far, and almost certainly will be my best ever.

I kind of had a prophetic vision while starting part 1 and foresaw what the twist in part 2 was going to be. So I just made sure that my part 1 solution worked for part 2 as well, which meant that it took me only 20 seconds after submitting part 1 to submit part 2. It was very exciting, I figured that I was on track to get a good leaderboard position, and I was nearly shaking with adrenaline when I viewed my placement.

I guess I'm skilled at Manhattan distance problems, because my previous best placement (2022, day 15) also involved the metric.

cmap = dict()
rmap = dict()
amd = 0
for r in range(len(inps)):
    if all(inps[r][c] == '.' for c in range(len(inps[r]))):
        amd += 1000000 - 1
    else:
        rmap[r] = r + amd
for c in range(len(inps[0])):
    if all(inps[r][c] == '.' for r in range(len(inps))):
        amd += 1000000 - 1
    else:
        cmap[c] = c + amd
gals = []
for r in range(len(inps)):
    for c in range(len(inps[r])):
        if inps[r][c] == '#':
            gals.append((rmap[r],cmap[c]))

for i in range(len(gals)):
    for j in range(i+1, len(gals)):
        ans += abs(gals[i][0]-gals[j][0]) + abs(gals[i][1]-gals[j][1])

1

u/daggerdragon Dec 11 '23

leaderboard: 14 / 2

Got second on the leaderboard today! That's by far my best placement so far, and almost certainly will be my best ever.

You should add the [ALLEZ CUISINE!] tag to your post because that is indeed Upping the Ante! Good job, chef :D

→ More replies (1)
→ More replies (3)

2

u/damnian Dec 11 '23

[LANGUAGE: C#] [Allez Cuisine!]

GitHub

Today's secret ingredient is a single function that takes lambdas for getting/setting a coordinate and calculates everything in a single pass.

2

u/daggerdragon Dec 11 '23

Today's secret ingredient is a single function that takes lambdas for getting/setting a coordinate and calculates everything in a single pass.

Well done, chef :D

→ More replies (2)

2

u/Eagle_1010 Dec 11 '23

[LANGUAGE: Python] for part1 replace the 1000000 - 1 with 1

input = open("input.txt").read().strip().splitlines()

empty_rows = [r for r, row in enumerate(input) if all(c == '.' for c in row)]
empty_cols = [c for c, col in enumerate(zip(*input)) if all(c == '.' for c in col)]

galaxies = [(i, j) for i in range(len(input)) for j in range(len(input[0])) if input[i][j] == '#']

res = 0
for r, g1 in enumerate(galaxies):
    for l, g2 in enumerate(galaxies[:r]):
        dist = (max(g1[0], g2[0]) - min(g1[0], g2[0])) + (max(g1[1], g2[1]) - min(g1[1], g2[1]))
        for r in empty_rows:
            if min(g1[0], g2[0]) < r < max(g1[0], g2[0]):
                dist += 1000000 - 1
        for c in empty_cols:
            if min(g1[1], g2[1]) < c < max(g1[1], g2[1]):
                dist += 1000000 - 1
        res += dist

print(res)

1

u/daggerdragon Dec 11 '23

The triple-backticks code fence (`​`​`) only works on new.reddit. Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read.

→ More replies (1)

2

u/DarthLasciel Dec 11 '23

[Language: Python]

https://github.com/DarthSpot/aoc2023/blob/main/tasks/task11.py

I know it's not much, but i'm really proud of coming up with a good solution for part 1, that i have a delta of 47 seconds between answering part 1 and 2. Also i used this years AoC to learn python :D

1

u/daggerdragon Dec 11 '23

Also i used this years AoC to learn python :D

Good, good, you've fallen for /u/topaz2078's trap of ~sneakily making people learn new things~ <3

2

u/onrustigescheikundig Dec 12 '23 edited Dec 12 '23

[LANGUAGE: OCaml]

github

For once I correctly predicted the twist for Part 2, and wrote my algorithm for Part 1 to account for a different space expansion factor. The input is processed into a list of coordinates corresponding to galaxies as well as the coordinates of rows and columns that do not contain any galaxies (the "void" axes). The main algorithm then iterates over each axis along each dimension and adds the expansion factor (minus one) to any coordinates of galaxies that are greater than the coordinate of the axis. The galaxies are actually represented as a coord Seq.t (that is, a lazy sequence structure) and the coordinate modifications are performed using a Seq.map operation, so I am not generating a new list for each encountered axis. Although this avoids the generation of new lists for each axis, there still end up being n_axis comparisons per galaxy, which is overall quadratic runtime in the worst case. In practice, there are far fewer void axes than galaxies in the problem input, and anyway the pairwise Manhattan distance calculations at the end are also quadratic.

Also, I see a lot of comments talking about off-by-one errors, so here is some math explaining why you need to subtract 1. The formula for (say) the final xfin coordinate of a galaxy with one void column to its left can be redefined as a sum of distances from some arbitrary origin:

xfin (su) = origin (su) + [left_void_edge (su) - origin (su)] + [right_void_edge (vu) - left_void_edge (vu)] + [x (su) - right_void_edge (su)]

where (su) = screen units and (vu) = void units. Our expansion factor is f (su / vu).

This simplifies to (dropping "_void_edge" for brevity and grouping like terms):

xfin (su) = x (su) + [right (vu) - left (vu)] + [left (su) - right (su)] 
          = x (su) + [right (vu) - left (vu)] - [right (su) - left (su)] 

We know that the width of a void column is 1 screen unit, so

xfin (su) = x (su) + [right (vu) - left (vu)] - 1 (su)

Converting void units to screen units, we get

xfin (su) = x (su) + f (su / vu) * [right (vu) - left (vu)] - 1 (su)
          = x (su) + f * [right (su) - left (su)] - 1 (su)
          = x (su) + f * [ 1 (su) ] - 1 (su)
          = x (su) + f (su) - 1 (su)

1

u/daggerdragon Dec 12 '23 edited Dec 21 '23

Inlined code is intended for short snippets of code only. On old.reddit, your longer one-liner gets cut off when it reaches the edge of the window.

Please edit your post to put all code in a four-spaces code block so it will be horizontally scrollable. edit: 👍

2

u/jbrownkramer Dec 12 '23

[LANGUAGE: Python]

I have a solution that is linear in the size of the grid.

First observation is that you can collapse along the y axis get the x contribution and vice versa.

The one dimensional case: consider the number of paths crossing from index i to index i + 1. It is the number of galaxies at index <= i times the number at index > i. Add these products all up to get the total length (multiplying by the expansion factor for indices with 0 galaxies).

import numpy as np

def parse(input):
    lines = input.split("\n")
    m = len(lines)
    n = len(lines[0])
    
    r = np.zeros((m,n))
    
    for i in range(m):
        for j in range(n):
            if lines[i][j] == "#":
                r[i,j] = 1
                
    return r

def one_d(a,e):
    '''e is the expansion factor'''
    r = 0
    
    past_sum = 0 #The sum of all the things less than or equal to index i
    future_sum = sum(a)#The sum all things greater than index i

    n = len(a)
    for i in range(n):
        v = a[i]
        past_sum += v
        future_sum -= v
        p = past_sum*future_sum #The number of paths spanning [i,i+1]
        if v == 0:
            r += p*e
        else:
            r += p
            
    return r

a = parse(input)
one_d(a.sum(axis=0),2) + one_d(a.sum(axis=1).flatten(),2)

1

u/daggerdragon Dec 12 '23
  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.

2

u/The6thProgrammer Dec 12 '23

[Language: C++]

Code: https://github.com/rlempka/advent-of-code-2023/tree/main/day-11

Part 1: Spent time constructing the "expanded" universe manually and then used the Manhattan distance between each pair of points to find the shortest paths

Part 2: Realized my part 1 was only partly optimal, instead of expanding the universe, I simply calculated Manhattan distance and added the number of empty rows between each pair of points multiplied by 999999 to the Manhattan distance, along with the number of empty columns between each pair of points multiplied by 999999.

That is, each distance calculation looked like this in code (multiplier = 1000000):

long long dist = (manhattanDistance(galaxyPoints[i], galaxyPoints[j]) + (countRowsBetween(emptyRowIndexes, galaxyPoints[i], galaxyPoints[j]) * (multiplier - 1)) + (countColsBetween(emptyColIndexes, galaxyPoints[i], galaxyPoints[j]) * (multiplier - 1)));

1

u/daggerdragon Dec 12 '23

Inlined code is intended for short snippets of code only. On old.reddit, your last line gets cut off when it reaches the edge of the window.

Please edit your post to put the one-liner in a four-spaces code block so it will be horizontally scrollable.

2

u/ianMihura Dec 17 '23 edited Dec 21 '23

[LANGUAGE: go]

Tried brute force for part 2, but no luck. Had to do some refactoring. IMHO the final solution for part 2 ended up being simpler than part 1!

https://github.com/ianmihura/advent23/blob/master/day_11/day_11.go

1

u/daggerdragon Dec 19 '23 edited Dec 21 '23

Your link is borked on old.reddit due to a new.reddit bug with URLs that contain underscores, so please fix it. edit: 👍

19

u/4HbQ Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Python]

Do you ever start mindlessly refactoring, only to end up with code that you can no longer explain?

print(sum(map(lambda g:
    sum(sum(map(i.__ge__, g)) * (i in g or 1000000) * 
        sum(map(i.__lt__, g)) for i in range(max(g))),
    zip(*[(x,y) for y,r in enumerate(open('data.txt'))
                for x,c in enumerate(r) if c=='#']))))
→ More replies (4)

3

u/weeble_wobble_wobble Dec 11 '23

[LANGUAGE: Python]

GitHub (31 lines each, with a focus on readability)

Part 1 expands graphically by inserting rows and columns, part 2 calculates the galaxies' adjusted coordinates

4

u/Weak_Swan7003 Dec 11 '23

I really like this solution! Usually when people optimize for readability they create too much 'fluff' (functions that aren't needed, extensive comment blocks, etc).

Also, I didn't know the trick of writing 1000000 as 1_000_000 to aid readability. It helps a lot!

3

u/weeble_wobble_wobble Dec 11 '23

Thanks for the feedback! I try to adhere to Clean Code principles while keeping with the spirit of these challenges :)

You can find more information about underscores in PEP 515 – Underscores in Numeric Literals. It's great for grouping bits in binary numbers!

2

u/daggerdragon Dec 11 '23

You can find more information about underscores in PEP 515 – Underscores in Numeric Literals.

TIL the same exists in PHP:

As of PHP 7.4.0, integer literals may contain underscores (_) between digits, for better readability of literals.

I appreciate you for saving my eyeballs from ginormous numbers from here on out!

→ More replies (2)

13

u/4HbQ Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Python] Code (6 lines)

Instead of solving the problem for (x,y)-coordinates, my dist(ps) function solves for one direction only. Simply compute dist(xs) and dist(ys) separately, and add the results:

def dist(ps):
    ps = [sum((l, 1)[p in ps] for p in range(p)) for p in ps]
    return sum(abs(a-b) for a in ps for b in ps)//2

for l in 2, 1_000_000: print(sum(map(dist, [xs, ys])))

13

u/kaa-the-wise Dec 11 '23 edited Dec 12 '23

[Language: J]

Well, Python one-liners are simply not short enough, so today I am trying J!

+/,(+/\*(1e6 1{~*)*(-~+/\.))(+/"1,.+/)'#'=];._2(1!:1<'11.in')

Same solution written in Uiua, as a bonus:

/+♭××⊃⊃\+(⊏:1e6_1±)(-:⇌\+⇌.)⍉⊟⊃/+≡/+⊜(=@#)≠@\n. &fras "11.in"

Translation of /u/4HbQ's python solution.

22

u/seligman99 Dec 11 '23

[LANGUAGE: Python] 70 / 23

github

That super rare event happened: My solution for part 1 perfectly set me up for part 2! I never thought I'd see the day!

6

u/kwshi Dec 11 '23

That's clean! Small tip you may already know about: instead of the double for-loop (for i in range(..): for j in range(i+1,..):) to loop over pairs you can also use import itertools; for (ax, ay), (bx, by) in itertools.combinations(stars, 2) if you like one-liners.

→ More replies (1)
→ More replies (3)

10

u/WhiteSparrow Dec 11 '23

[LANGUAGE: Uiua]

Very easy once I understood the task... This solves part 2, for part 1 just change the Scale param.

≡≡(⊗:".#")⊜∘≠@\n.&fras"task11.txt"
Scale ← -1 1000000
⊙:⊙⊙(°⊟⍉) ∩(⊚¬≡/↥) :⍉. :⊚.
÷2/+♭⊠(⌵-). ⍉⊟ ∩(+×Scale ≡(⊃(/+>|⋅∘))¤)

5

u/Laugarhraun Dec 11 '23 edited Dec 11 '23

How are multiple people using Uiua to solve AOC? I know about APL and J, is Uiua a new hype kid on the block? It also seems that BQN is gaining traction...

→ More replies (2)

7

u/Background-Vast487 Dec 11 '23

Looks like the input to day 10 :p

8

u/voidhawk42 Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Dyalog APL]

p←'#'=↑⊃⎕nget'11.txt'1
f←{+\(⍴⍵)⍴⍺⌊1+⍺×~∨⌿⍵}
g←{2÷⍨+/,∘.(+/|⍤-)⍨(,p)/,(⍉⍵f⍉p),¨⍵f p}
g 2   ⍝ part 1
g 1e6 ⍝ part 2

4

u/ultranarcolepsy Dec 11 '23

How about f←{(⍴⍵)⍴+\⍺*~∨⌿⍵}?

→ More replies (1)

8

u/timvisee Dec 11 '23

[Language: Rust]

Day 1 to 11 still under 1 millisecond in total! 🚀

→ More replies (1)

7

u/vu47 Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Kotlin, functional programming]

I'm actually a software engineer in astronomy, so this felt like Monday morning coming early to me.

Thankfully, I predicted (for once) what part 2 might be, and could see that the data was a sparse representation, so it made sense to calculate the empty rows and the empty columns, determine the original coordinates of the galaxies, and then determine their adjusted coordinates by the parameter of how much an empty row or column was worth.

After that, it was easy to just produce all pairs, calculate the difference using the Manhattan metric, and then sum them up to get the answer.

This was one of the more fun problems for me so far, with the exception that my test case for problem 1 kept failing. D'oh. After about half an hour of having no idea what was going wrong, I realized that I carelessly copy-pasted the example input that was already adjusted for the empty rows and columns, so I was getting a number bigger than expected. After I fixed that, the test case passed, and everything was smooth sailing.

Very happy with my solution to this one.

paste

4

u/Valefant Dec 11 '23

I really like your functional coding style. I always try to sprinkle some functional style in my solutions as well but I always switch back to my usual imperative style as I cannot express each construct in my head :D

→ More replies (6)
→ More replies (12)

7

u/leftfish123 Dec 11 '23

[Language: Python]

OK, this most likely is an inefficient solution. I store the galaxies as a list of [x, y], apply offset to their coordinates one by one depending on how many lines/columns with smaller indexes there are, then use itertools.combinations to find pairs and finally calculate Manhattan distances between the pairs. That's a lot of overhead that includes over 100 000 pairs in the combinations iterator, though it works pretty quickly.

But...I managed to think about it early in the day during my commute to work, then after the entire day I sat down, wrote it and had both parts done almost at the same time. As a complete amateur, I am darn proud of my decision to calculate instead of manipulating the 2D array.

→ More replies (1)

7

u/Gprinziv Dec 13 '23

[LANGUAGE: Python 3] This one was nice and easy!

I did part one by actually expanding the array by one at each point and solving, but then I saw part 2 and realized that there was no way I was going to to the math on arrays that big, so instead I kept a list of all the "thresholds" where the graph was empty and if a star crossed those lines, you simply added the required number of extra steps (1 for part 1, 999999 for part 2).

I think itertools saved me a hell of a lot of time here writing up a combinations() function myself. I'm still a bit flu-y and there are probably better ways to do multiple steps of this in one pass, but this was a good one.

import re, itertools

with open("input") as f:
    galaxy = f.read().splitlines()

verticals, horizontals, stars = [], [], []
for i, _ in enumerate(galaxy[0]):
    if all(space[i] == "." for space in galaxy):
        verticals += [i]
for j, line in enumerate(galaxy):
    if all(space == "." for space in line):
        horizontals += [j]
    else:
        stars += [[star.start(), j] for star in re.finditer("#", galaxy[j])]

total = 0
for a, b in itertools.combinations(stars, 2):
    x = sorted([a[0], b[0]])
    y = sorted([a[1], b[1]])
    total += (x[1] - x[0]) + (y[1] - y[0])
    for ver in verticals:
        if ver in range(x[0], x[1]):
            total += 999999
    for hor in horizontals:
        if hor in range(y[0], y[1]):
            total += 999999
print(total)
→ More replies (2)

5

u/DFreiberg Dec 11 '23

[LANGUAGE: Mathematica] Mathematica, 97/289

Actually made it on the leaderboard; it was by one single second, but it counts. Had to completely redo my Part 1 solution upon getting to part 2, but that's par for the course; I have nobody but myself to blame for not seeing "Do what you did before, but do it a million times!" coming.

Setup:

expandUniverse[factor_, array_] :=
 Module[{sp, emptyRows, emptyColumns, oldPos, newPos, subs},
  oldPos = Position[array, "#"];
  sp = SparseArray[# -> 1 & /@ oldPos];
  emptyRows = Flatten[Position[Total /@ sp, 0]];
  emptyColumns = Flatten[Position[Total /@ Transpose[sp], 0]];
  newPos =
   Table[
    pos + (factor - 1)*{
      Count[emptyRows, _?(# < pos[[1]] &)], 
      Count[emptyColumns, _?(# < pos[[2]] &)]},
    {pos, oldPos}];
  subs = Subsets[newPos, {2}];
  Total[ManhattanDistance @@ # & /@ subs]]

Part 1:

expandUniverse[2, input]

Part 2:

expandUniverse[10^6, input]

5

u/YenyaKas Dec 11 '23

[LANGUAGE: Perl] 1193/562

Today's task was quite Perl-friendly. I extracted the galaxy coordinates as array of [ $x, $y ] pairs using pos() on rows, and then computed which coordinates where occupied using

my %x_gxy = map { $_->[0] => 1 } @gxys;
my %y_gxy = map { $_->[1] => 1 } @gxys;

Then I walked X and Y separately, counting the unoccupied rows twice:

for ($x1+1 .. $x2) {
    $steps += $x_gxy{$_} ? 1 : 2;
}

Part 2 was only about using 1_000_000 instead of 2.

Full code for Part 2 here.

All my AoC solutions in Perl

→ More replies (2)

5

u/bucketz76 Dec 11 '23

[Language: Python]

I overthought the hell out of this problem. I originally manually expanded the graph, then ran BFS from every galaxy. Then, for part 2, I changed it to instead encode the row/col expansion as edge weights and then use Dijkstra's algorithm (which worked, but was super slow). Finally, my brain turned on and I realized you can just take the manhattan distance and add in the penalty for each empty row/col in between. Ugh.

paste

5

u/Good_Brain_2087 Dec 11 '23

[LANGUAGE: Javascript]

Part 1 with variable spacing, just change the spacing for part 2

Solution: https://github.com/sanishchirayath1/advent-of-code/blob/master/2023/day11/index.js

5

u/kbielefe Dec 11 '23

[LANGUAGE: Scala] GitHub

Relatively straightforward. I was fortunate enough to not have done the actual expansion in part 1, just made a note of the empty rows and columns. Only hitch was 1000000 times is actually 999999 'extra' spaces, so my first answer was too high.

5

u/pkusensei Dec 11 '23

[Language: Rust]

Not the best solution as it is not fast at all. It also reveals something interesting: in debug mode (cargo test) HashSet is noticeably slower than BTreeSet, but in release mode (cargo test --release) it runs as well, if not slightly better. Code

→ More replies (2)

5

u/chubbc Dec 11 '23

[LANGUAGE: Uiua]

Pad link

Input ← ⊜(=@#)≠@\n. &fras "11.txt"
Gal ← ▽:∩♭ ⊠(□⊂).⇡⧻ .
Wt ← ∩(¤\++1×-1),:⊙(∩(¬/↥)⍉.):
Solve ← ÷2/+♭ × ≡(⊠+∩(⌵-⊙⊡,)⊙:⊐⊃⊢(⊏1)) ⊃(Gal|Wt|¤)

Solve Input 2 
Solve Input 1000000

5

u/[deleted] Dec 11 '23

[Language: Rust]

Easy-peasy compared to day 10. I've done enough of these by now to know that the challenges will usually bite you in the butt with big numbers, so I represented each galaxy as a pair of indices and modified them accordingly. Only thing that bit me was an off-by-one error in Part 2, which I quickly fixed.

Part 1

Part 2

4

u/MarcusTL12 Dec 11 '23

[LANGUAGE: Julia] 385/187 Code

Took me a bit too long to realize that I could just do the manhattan distance + the number of empty columns + rows, and then part 2 was essentially trivial as I just multiply the extra distance by 1000_000 - 1. That -1 lost me a minute...

4

u/jonathan_paulson Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Python 3] 315/239. Solution. Video.

Lots of bugs today :( In part 1:

  • Didn't add a new row/column in the middle of the grid correctly
  • Iteration over rows/columns didn't handle adding new rows/columns correctly (this was tricky to diagnose)
  • Didn't dedupe pairs correctly (misread)

In part 2:

  • Added 1 million to the distance for each empty row/column instead of 999,999 (misread)
→ More replies (1)

4

u/eisemsi2 Dec 11 '23

[Language: C++]

Part 1 : Quite ease. Just added another row/col whenever I found an empty row/col. Then calculated the grid distance b/w all pairs.

Part 2 : I added a different row/col (containing all '!') and considered it to be sort of "space warp". Then after I calculated prefix arrays to access how many such row/col "space warps" are between any 2 points in the grid (space). Then all remains is to use a similar formula used in part 1 to calculate distance.

Link : https://github.com/eisemsi2/Advent-of-Code-2023/tree/master/day11

4

u/sanraith Dec 11 '23

[LANGUAGE: Scala]

Code is available on my github: Day11.scala

I only keep track of the galaxy points of the map and expand everything "backwards", so that I don't have to shift the coordinates of the negative space:

override def part2(ctx: Context): Long =
  val (galaxies, width, height) = parseInput(ctx.input)
  val expanded = expand(galaxies, width, height, 999999)
  expanded.combinations(2).map { case Seq(a, b) => a.manhattan(b) }.sum

def expand(source: Seq[Point], width: Int, height: Int, amount: Long = 1) =
  val wider = (1 until width)
    .filter(x => source.forall(_.x != x))
    .foldLeft(source): (points, x) =>
      points.map(p => if (p.x < x) Point(p.x - amount, p.y) else p)
  (1 until height) // taller
    .filter(y => wider.forall(_.y != y))
    .foldLeft(wider): (points, y) =>
      points.map(p => if (p.y < y) Point(p.x, p.y - amount) else p)

4

u/Hungry_Mix_4263 Dec 11 '23

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day11.hs

Solved using Manhattan distance, and when the path intersects with one of the empty rows, you add factor - 1 to the cost.

4

u/sgxxx Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Python]

10 lines solution for both parts by expanding the coordinates instead. https://github.com/sayan01/advent-of-code-2023/blob/master/11/p.py

TIL about shortcut to transpose matrix using zip(*lines)

4

u/busseroverflow Dec 11 '23

[LANGUAGE: Go]

The code is on Github.

Part 1: 0.18ms, Part 2: 0.18ms

Like many others, I saw part 2's twist coming so I made sure my solution would scale with any expansion factor.

First, I read the input and stored the galaxies as a list of coordinates, each with a row and column integer value.

Next, I needed to identify the rows and columns to expand. I iterated over all galaxies to identify which rows and columns had galaxies. I then computed the minimum and maximum of those values, giving me the range I needed to work with. Within this range, I determined which columns and rows didn't have a galaxy; these are the ones that expand.

To apply expansion, I needed to adjust the coordinates of each galaxy. I decided that expansion would push galaxies to the right and downward only. I shifted each galaxy to the right by the number of empty columns that were to its left. Same thing vertically: each galaxy shifts down by the number of empty columns above it.

Once I had the new coordinates, I computed the distance between each galaxy pair. Since there are no obstacles between galaxies, we can compute the Manhattan distance, which is easy to implement and runs blazingly fast.

With part 1 solved, I needed to adapt my code for part 2. This involved only a small change: instead of adding 1 for each empty row/column, I added 999,999. This involved adding a `factor` variable to my `expand` function, so a 3-line change in total.

The code runs really fast (well under a millisecond). The part that takes the longest to run is computing all the distances between galaxies, because it's O(n²), where n is the number of galaxies. The part that expands the space between galaxies is only O(n).

4

u/MarcoDelmastro Dec 11 '23

[LANGUAGE: Python]

https://github.com/marcodelmastro/AdventOfCode2023/blob/main/Day11.ipynb

(np.insert() directly on original map for Part 1, computing new coordinates for Part 2)

4

u/NickKusters Dec 11 '23 edited Dec 11 '23

[LANGUAGE: C#]

Fun day again! I immediately realised I should NOT actually expand the space and just track how much we need to add.

I wrote these helpers to check how many times we need to pad, calculate the bounding box out of two points to do that check and to calculate the Manhattan Distance between two points.

Basic approach: Map the input to just galaxy coordinates, Find empty cols & rows for the expansion, generate permutations for the Galaxies (wrote that code as an extension a long time ago), calculate bounding box, padding, manhattan distance, and add them all up together.

For part 2, I just needed to introduce a modifier to the padding logic, taking into account that, as part of the map, you already count the space once, so you need to multiply by modifier -1.

Part 1:

var galaxyCombos = Galaxies.GetPermutations(2, false).ToArray();
Console.WriteLine($"We found {Galaxies.Count} galaxies, giving us {galaxyCombos.Length} unique pairs.");
long totalSum = 0L;
foreach(var galaxyPair in galaxyCombos)
{
    var pair = galaxyPair.ToArray();
    var boundingBox = GetBoundingBox(pair[0], pair[1]);
    var padding = GetPadding(boundingBox.topLeft, boundingBox.bottomRight);
    var distance = Distance(pair[0], pair[1]);
    var sum = distance + padding;
    totalSum+= sum;
    //Console.WriteLine($"{pair[0]} -> {pair[1]} = {distance} + {padding} = {sum}");
}
Console.WriteLine($"Part 1: {totalSum}");

Part 2, you just change the padding:

// We need to account for the fact that we already counted the empty space in the map already, so substract one from that modifier.
var padding = GetPadding(boundingBox.topLeft, boundingBox.bottomRight) * (EmptySpaceModifierPart2 - 1);

Parsing Input:

bool test = false;
const char GalaxyMarker = '#';
List<(int row, int col)> Galaxies;
string[] RawMap;
HashSet<int> EmptyRows, EmptyCols;
int RowCount, ColCount;
long EmptySpaceModifierPart2 = 1_000_000;
string[] RawMap = Input.ToLines();
RowCount = RawMap.Length;
ColCount = RawMap[0].Length;
Galaxies = RawMap
    .WithIndexes()
    .SelectMany(row => row.value
        .WithIndexes()
        .Where(col => col.value == GalaxyMarker)
        .Select(col => (row.index, col.index))
    )
    .ToList();
// Map out empty rows & cols for expansion
for (int row = 0; row < RowCount; ++row)
{
    if (RawMap[row].All(x => x == '.')) EmptyRows.Add(row);
}
for (int col = 0; col < ColCount; ++col)
{
    bool allOk = true;
    for (int row = 0; row < RowCount; ++row)
    {
        if (RawMap[row][col] != '.')
        {
            allOk = false;
            break;
        }
    }
    if (allOk) EmptyCols.Add(col);
}

Helpers:

long GetPadding((int row, int col) topLeft, (int row, int col) bottomRight)
    => GetColPadding(topLeft, bottomRight) + GetRowPadding(topLeft, bottomRight);
long GetRowPadding((int row, int col) topLeft, (int row, int col) bottomRight)
    => EmptyRows.Count(x => x >= topLeft.row && x <= bottomRight.row);
long GetColPadding((int row, int col) topLeft, (int row, int col) bottomRight)
    => EmptyCols.Count(x => x >= topLeft.col && x <= bottomRight.col);
long Distance((int row, int col) a, (int row, int col) b) => (Math.Abs(a.row - b.row) + Math.Abs(a.col - b.col));
((int row, int col) topLeft, (int row, int col) bottomRight) GetBoundingBox((int row, int col) a, (int row, int col) b)
    => ((Math.Min(a.row, b.row), Math.Min(a.col, b.col)), (Math.Max(a.row, b.row), Math.Max(a.col, b.col)));

video

→ More replies (5)

4

u/philippe_cholet Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Rust]

My rusty-aoc repo & today' solution here.

Part 2 was obvious from the start, right? That it would expand much more.

Solved in 27mn.

Sadly, this is slowest solver so far: benchmarked to 3.68ms each part ^^. Probably inevitable since there a lot of pairs. Maybe use "bitvec" crate instead of a vector of booleans. I don't want to add a dependency for now. But if the grid was less than 128x128, I would have done with u128 integers.

I did the off-by-one error of 1_000_000 instead of 999_999.

→ More replies (2)

4

u/bulletmark Dec 11 '23

[Language: Python]

import fileinput
from itertools import combinations

data = [[c for c in ln.strip()] for ln in fileinput.input()]

def findempty(data: list[list[str]], n: int) -> list[int]:
    num = []
    count = 0
    for line in data:
        if '#' not in line:
            count += n

        num.append(count)
        count += 1

    return num

def dist(n1: tuple[int, int], n2: tuple[int, int]) -> int:
    return abs(n1[0] - n2[0]) + abs(n1[1] - n2[1])

def calc(data: list[list[str]], n: int) -> int:
    empty_row = findempty(data, n)
    empty_col = findempty([*zip(*data)], n)
    coords = [(erow, ecol) for line, erow in zip(data, empty_row)
              for c, ecol in zip(line, empty_col) if c == '#']
    return sum(dist(n1, n2) for n1, n2 in combinations(coords, 2))

print('P1 =', calc(data, 1))
print('P2 =', calc(data, 1000_000 - 1))

4

u/[deleted] Dec 11 '23 edited Dec 30 '23

[LANGUAGE: Google Sheets]

https://github.com/zdhmd/advent-of-code-gs/blob/main/2023/day11.md

The problem is easy but creating a one formula solution is extremely difficult due to the limitations of the software. I was eventually able to get a one-formula solution for both parts but it's extremely slow. (Takes ~10 minutes to load)

4

u/cbh66 Dec 11 '23

[LANGUAGE: Pickcode]

Of course, my original part 1 solution actually inserted new empty rows and columns to the grid. Hit a couple of off-by-one errors as I refactored to handle any expansion rate, but it was nice that I could test it with my input and an expansion factor of 2 and double check I got the same answer for part 1.

https://app.pickcode.io/project/clq0uo6hhag4yop01pznl6jew

4

u/Synedh Dec 11 '23 edited Dec 11 '23

[Language: Python]

Python shenanigans.

  • Galaxies gathering
  • Finding empty rows/columns
  • Expansion handeling on these galaxies

    • number of empty spaces before index * (expansion ratio - 1)
  • sum of distance
def distance(pos1, pos2):
    return abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1])

universe = open('input').read().splitlines()
empty_lines = [i for i, line in enumerate(universe) if '#' not in line]
empty_cols = [j for j, col in enumerate(zip(*universe)) if '#' not in col]

galaxies = [[i, j] for i, line in enumerate(universe) for j, cell in enumerate(line) if cell == '#']
for galaxy in galaxies:
    galaxy[0] += sum(line < galaxy[0] for line in empty_lines) * (1000000 - 1)
    galaxy[1] += sum(col < galaxy[1] for col in empty_cols) * (1000000 - 1)

print(sum(distance(gal1, gal2) for k, gal1 in enumerate(galaxies) for gal2 in galaxies[k + 1:]))
→ More replies (2)

4

u/nilgoun Dec 11 '23

[Language: Rust]

Well... today was not my brightest.. -_-

At least the final solution is clean..

Code

→ More replies (1)

4

u/Althar93 Dec 11 '23 edited Dec 11 '23

[LANGUAGE: haskell]

Old me would have naively expanded the image and cried when reaching part 2.

Wise(r) me simply extracted the (x,y) coordinates of galaxies and offset their respective x/y components by the number of (empty) rows/columns above/left of their position.

Part 2 was as simple as multiplying offset by 999999.

My solution for part 1 & 2

[EDIT: I need to revisit my pair generation function to only generate unique pairs, in my haste/lazyness I just halved the result]

[EDIT 2: I've addressed the doubling of pairs in my implementation ; unsurprisingly, the execution cost is halved from 38ms to 19ms on my system for both parts]

→ More replies (1)

4

u/Lysander7 Dec 11 '23

[LANGUAGE: rust]

github

Nice, now I have some time to go back and solve part 2 of yesterday's puzzle :^)

→ More replies (2)

4

u/whiplashoo21 Dec 11 '23

[LANGUAGE: Python]

So proud to only need to change one variable for Part 2 to work. github

→ More replies (2)

4

u/malobebote Dec 11 '23 edited Dec 11 '23

[Language: Typescript]

I wouldn't have figured it out if I hadn't eventually gotten the hint that you consider 1_000_000 as a factor of 999_999. Sheesh.

https://pastebin.com/ZthpCbxT

→ More replies (3)

4

u/lucamanzi Dec 11 '23

[LANGUAGE: Haskell]

code

Simple Solution, I felt I had to implement a smart way to store the Universe

It paid off: part2 was trivial

4

u/tridactyla Dec 11 '23

[LANGUAGE: Lua]

I did most of the work with pencil and paper to find a nice small formula for the solution.

Given the number of galaxies n, expansion scale k, sorted x coordinates x1 through xn, sorted y coordinates y1 through yn, the solution can be calculated with a single sum:

[; \sum_{i=1}^{n} (2i-n-1)(x_i + y_i) + (k-1)(i-1)(n-i+1)(max(x_i - x_{i-1} - 1, 0) + max(y_i - y_{i-1} - 1, 0))) ;]

Here's my code:

local xs, ys, y, G = {}, {}, 1, string.byte'#'
for line in io.lines() do
	for x = 1, #line do
		if string.byte(line, x) == G then
			xs[#xs + 1] = x
			ys[#ys + 1] = y
		end
	end
	y = y + 1
end
table.sort(xs)

local ans, off, oldx, oldy = 0, 0, math.huge, math.huge
for i, x in ipairs(xs) do
	local y, n = ys[i], 2*i - #xs - 1
	off = off + (i-1) * (i-n) * (math.max(x-oldx-1, 0) + math.max(y-oldy-1, 0))
	ans = ans + n * (x+y)
	oldx, oldy = x, y
end
print(ans + off, ans + off*999999)

4

u/loquian Dec 12 '23

[LANGUAGE: C++]

github, 12.384 ms (both parts together)

A normal day's work--expanding the universe, finding the distance between every pair of galaxies. You know. The usual stuff.

4

u/i_have_no_biscuits Dec 12 '23

[Language: Python]

Fairly happy with this linear-time solution -

  • Pass through the grid, creating a 'marginal count' of the number of stars in each row and column (effectively a counting sort).
  • In one pass through each 'margin', work out the total pairwise distance + the expansion coefficient (the amount the normal distance increases for each 'stretch' of the empty lines), which is calculated from the number of connections between points which cross each empty line.
  • Part 1 is (normal dist) + 1 * (expansion coeff)
  • Part 2 is (normal dist) + 999,999 * (expansion coeff)

Code is here Average time taken is ~1ms including parsing.

→ More replies (7)

4

u/CutOnBumInBandHere9 Dec 12 '23

[Language: Python3]

Some people say I have a numpy problem. I disagree

def solve(s=1):
    y, x = np.where(data == "#")

    empty_r = [i for i in range(len(data)) if all(data[i] == ".")]
    empty_c = [i for i in range(len(data)) if all(data[:, i] == ".")]
    new_y = y + s * np.array([y > empty_r[i] for i in range(len(empty_r))]).sum(axis=0)
    new_x = x + s * np.array([x > empty_c[i] for i in range(len(empty_c))]).sum(axis=0)
    return (
        abs(new_y - new_y.reshape(-1, 1)) + abs(new_x - new_x.reshape(-1, 1))
    ).sum() // 2

Link

4

u/joshbduncan Dec 13 '23

[LANGUAGE: Python]

Not blazing by any means but pretty straightforward.

from itertools import combinations

data = open("day11.in").read().strip().splitlines()
w, h = len(data[0]), len(data)
map = [[c for c in row] for row in data]
galaxies = set((y, x) for y, l in enumerate(map) for x, c in enumerate(l) if c == "#")
expand_rows = [i for i in range(h) if set(map[i]) == {"."}]
expand_cols = [i for i in range(w) if set(map[r][i] for r in range(h)) == {"."}]

p1 = p2 = 0
p1_exp = 1
p2_exp = 1000000 - 1
for g1, g2 in combinations(galaxies, 2):
    y1, x1, y2, x2 = g1 + g2
    # get distance between galaxies
    p1 += abs(x1 - x2) + abs(y1 - y2)
    p2 += abs(x1 - x2) + abs(y1 - y2)
    # add extra (expanded) rows and cols
    p1 += sum([p1_exp for n in expand_rows if n in range(min(y1, y2), max(y1, y2))])
    p1 += sum([p1_exp for n in expand_cols if n in range(min(x1, x2), max(x1, x2))])
    p2 += sum([p2_exp for n in expand_rows if n in range(min(y1, y2), max(y1, y2))])
    p2 += sum([p2_exp for n in expand_cols if n in range(min(x1, x2), max(x1, x2))])
print(f"Part 1: {p1}")
print(f"Part 2: {p2}")

1

u/dag625 Dec 11 '23

[LANGUAGE: C++] 815/442

Code: AdventOfCode/2023/day11.cpp at master · dag625/AdventOfCode (github.com)

Yesterday I managed to just get into the top 1000, and today I was solidly in. That's a trend I don't expect to continue.

A lot of things just clicked for me. Another grid problem to use my grid utility code on. I realized that by requiring Manhattan distances, I could just add the number of empty rows and columns to the total to calculate the expanded distance. And then when part 2 was just changing the expansion rate I just needed to change my `++dist` to `dist += expansion_factor`. So, this was probably close to the best-case scenario for leaderboard positioning for me.

→ More replies (1)

1

u/m1el Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Python]

Took me too long to find a few copy-paste errors, 746/291

data = list(open('day11.txt').read().splitlines())
cols = set()
rows = set()
gals = []
for (yy, row) in enumerate(data):
    for (xx, ch) in enumerate(row):
        if ch == '#':
            cols.add(xx)
            rows.add(yy)
            gals.append((xx, yy))

for age in (2, 1000000):
    tot = 0
    for ii in range(len(gals)):
        for jj in range(ii + 1, len(gals)):
            x1, y1 = gals[ii]
            x2, y2 = gals[jj]
            
            xs = set(range(min(x1, x2), max(x1, x2)))
            d = len(xs - cols) * age + len(xs & cols)
            ys = set(range(min(y1, y2), max(y1, y2)))
            d += len(ys - rows) * age  + len(ys & rows)
            tot+=d
    print(tot)

1

u/daggerdragon Dec 11 '23 edited Dec 11 '23

Please add the language tag as requested by AutoModerator. edit: 👍

→ More replies (1)

1

u/yfilipov Dec 11 '23 edited Dec 11 '23

1

u/daggerdragon Dec 11 '23 edited Dec 11 '23

Comment temporarily removed because your code is way too long for the megathreads.

  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your comment to put your code in an external link and link that here instead, then I will re-approve your comment. edit: 👍

→ More replies (1)

1

u/AlexAegis Dec 11 '23

[Language: TypeScript]

Part 2 surprised me, because even part 1 is super slow unless done properly. Since I had a fancy graph+path finding implementation at hand I tried with that but it was super slow. Only when I printed the paths it found I realized I could just use the manhattan distances, see how many empty rows/columns it passes through and add them together.

If we interpret the input like this:

export interface GalaxyMap {
	galaxies: Vec2[];
	emptyRows: number[];
	emptyColumns: number[];
}

then:

part1

import { BoundingBox, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { parse } from './parse.js';

export const solver =
	(expansion = 2) =>
	(input: string): number => {
		const { galaxies, emptyRows, emptyColumns } = parse(input);

		return galaxies
			.pairs()
			.map(([a, b]) => {
				const nonExpandedDistance = a.manhattan(b);
				const galaxyBox = BoundingBox.fromVectors([a, b]);
				const emptyRowCount = emptyRows.filter((i) =>
					galaxyBox.vertical.contains(i),
				).length;
				const emptyColumnCount = emptyColumns.filter((i) =>
					galaxyBox.horizontal.contains(i),
				).length;

				return (
					nonExpandedDistance +
					emptyRowCount * (expansion - 1) +
					emptyColumnCount * (expansion - 1)
				);
			})
			.sum();
	};

export const p1 = (input: string): number => solver(2)(input);

await task(p1, packageJson.aoc); // 10228230 ~104.54ms

part2

import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { solver } from './p1.js';

export const p2 = (input: string): number => solver(1_000_000)(input);

await task(p2, packageJson.aoc); // 10228230 ~104.48ms
→ More replies (1)

1

u/vanveenfromardis Dec 11 '23

[LANGUAGE: C#]

GitHub

There's not too much to say about this one, personally I didn't find it too interesting.

2

u/daggerdragon Dec 11 '23 edited Dec 11 '23

There's not too much to say about this one, personally I didn't find it too interesting.

This type of comment does not belong in a Solution Megathread. If you have feedback about the puzzles, create your own post in the main subreddit. edit: retracted, but please don't make a habit out of grinching in the megathreads.

→ More replies (2)

1

u/oupsman Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Golang]

Not much to say here, but that was kinda fun today

https://raw.githubusercontent.com/Oupsman/AOC2023/main/d11/advent_11.go

1

u/daggerdragon Dec 11 '23 edited Dec 11 '23

Your link is borked on old.reddit, so please fix it. edit: 👍

→ More replies (1)

1

u/globalreset Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Ruby] 6825 / 6309

Got a late start on this one, but it was pretty easy. In part 1, I did the expansion of the data array and then just did manhattan distance. But realized I needed something smarter for part 2. So I just created a Set for each empty row and another one for each empty column, then and'ed that with the Set of cols and rows between my galaxies.

github

1

u/daggerdragon Dec 11 '23 edited Dec 11 '23
  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead. edit: 👍

1

u/shouchen Dec 11 '23 edited Dec 11 '23

[LANGUAGE: C++] https://github.com/shouchen/AdventOfCode/blob/master/aoc2023/Day11/Day11.cpp

No need to even store the whole grid. Just parse the input into:

(1) A list of galaxy locations.

(2) An array of bools for which rows have galaxies in them.

(3) An array of bools for which columns have galaxies in them.

Then, go through all combinations pairs of galaxies in (1) and sum up the distances.

To find the distance between two galaxies in the above, the actual path taken doesn't matter as it will still cross the same number of intermediate rows and columns. Just walk across those rows and add 1 if that row contains galaxies (2) or 2 if it doesn't. Then do the same thing walking through columns using (3).

For part two, just change 2 to 1000000.

1

u/daggerdragon Dec 11 '23 edited Dec 11 '23

Your code block is too long for the megathreads. Please edit your comment to replace your oversized code with just that external link to your code. edit: 👍

1

u/TankLivsMatr Dec 11 '23 edited Dec 12 '23

[Language: Go]

Hey look, a day that didn't completely stress me out. (Looking at you Day 10).

This was relatively simple. For part 1, for expanding, we can just use Go's handy dandy append() algorithm to add in the extra line where necessary.

From there I just created a hashmap where the key was the galaxy number, and the value is the galaxy coordinates. When calculating the distances, I just looped through all of the keys and calculated the distances.

Calculating the distances is as simple as Manhattan Distance.
Part 1

For Part 2, instead of adding another row or column of "." I instead added another row or column of "*". Which signifies a certain amount to "jump" by. Rather than adding x amount of rows and lines.

That way, when constructing my hashmap, if it finds a row or a column with "*" then it increases the "Actual Row/Column" and then uses that when storing the hashmap.
Part 2

(Edit: formatting) (Edit 2: removing profanity)

2

u/daggerdragon Dec 11 '23 edited Dec 12 '23

~~kick my [COAL]. ~~

Comment temporarily removed due to naughty language. Keep the megathreads SFW, please. If you edit your comment to take out the naughty language, I'll re-approve the comment. edit: 👍

→ More replies (1)

1

u/ell5e Dec 11 '23 edited Dec 13 '23

[Language: Dart]

2 line diff between part 1 and part 2 :)

https://github.com/ellemouton/aoc_2023/blob/main/eleven/main.dart

1

u/daggerdragon Dec 11 '23

Your link is borked on old.reddit, so please fix it.

→ More replies (1)

1

u/The_Doctor_33 Dec 11 '23

[LANGUAGE: Kotlin]

I just debugged part 2 for ~2h because i used Integer instead of long.

https://github.com/Stenz123/advent-of-code_2023/blob/main/src/main/kotlin/days/day11/Day11.kt

1

u/daggerdragon Dec 11 '23

Your link is borked on old.reddit, so please fix it.

1

u/d9d6ka Dec 11 '23 edited Dec 12 '23

[Language: Python]

Commit

The shortest path is a Manhattan one :) the trick I used is to check whether any empty row or column is between the corresponding coordinates of two galaxies and to add extra values to the distance in non-expanded universe.

UPD: if-statements in the function of exdist can be removed, the're from the solution for Part 1 :)

1

u/daggerdragon Dec 11 '23

Psst: we can see your Markdown.

2

u/d9d6ka Dec 12 '23

Sorry? Did I write smth wrong?

2

u/daggerdragon Dec 12 '23

Err, no, nothing wrong, I just meant that we could see your backtick Markdown on old.reddit because the new.reddit fancypants editor escapes Markdown - you have to switch it to Markdown mode first.

→ More replies (1)

1

u/NigraOvis Dec 11 '23

[python 3]

my awful, but fast python solution

with open("input11.txt") as file:
    input = file.read().splitlines()
    inputtest = """...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....""".split('\n')
    
    columns = {}
    rows = {}
    #expand vertically
    for row in range(len(input)):
        if input[row].count('#') == 0:
            #0 above means expand
            rows[row] = True
        else:
            rows[row] = False

                #input.insert(row,'.'*len(input[row]))
    #expand horizontally
    for col in range(len(input[0])):
        expand = True
        for row in range(len(input)):
            if input[row][col] == "#":
                expand = False
        if expand:
            columns[col] = True
        else:
            columns[col] = False

    answer = 0
    answer2 = 0
    part2_expansion = 1000000
    for y in range(len(input)):
        for x in range(len(input[0])):
            if input[y][x] == "#":
                for yb in range(len(input)):
                    for xb in range(len(input[0])):
                        if input[yb][xb] == "#":
                            if yb>y:
                                for row in range(y,yb):
                                    if rows[row]:
                                        answer+=2
                                        answer2+=part2_expansion
                                    else:
                                        answer += 1
                                        answer2+=1
                            if yb<y:
                                for row in range(yb,y):
                                    if rows[row]:
                                        answer+=2
                                        answer2+=part2_expansion
                                    else:
                                        answer += 1
                                        answer2+=1
                            if xb>x:
                                for col in range(x,xb):
                                    if columns[col]:
                                        answer+=2
                                        answer2+=part2_expansion
                                    else:
                                        answer += 1
                                        answer2+=1
                            if xb<x:
                                for col in range(xb,x):
                                    if columns[col]:
                                        answer+=2
                                        answer2+=part2_expansion
                                    else:
                                        answer += 1
                                        answer2+=1

    print(answer//2)
    print(answer2//2)

1

u/daggerdragon Dec 11 '23

Your code block is too long for the megathreads. Please edit your comment to replace your oversized code with an external link to your code.

Also, please fix your language tag as AutoModerator requested.

→ More replies (1)

1

u/LeoLion32 Dec 11 '23 edited Dec 11 '23

[LANGUAGE: PYTHON]

https://github.com/leolion3/advent-of-code-2023/blob/main/11-cosmic-expansion/solution.py

Ended up rebuilding the 'image' in part 1, then realised it would be easier to just work with coordinates. Itertools for combination generation.

1

u/daggerdragon Dec 11 '23
  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.

→ More replies (1)

1

u/RobertGauld Dec 11 '23 edited Dec 12 '23

[Language: Rust]

I bashed out a solution this morning but was a bit disppointed that it took about half a second (edit, it was actually 200ms). This afternoon some inspiration for an alternative solution hit, now runs in 2ms.

https://topaz.github.io/paste/#XQAAAQCDCQAAAAAAAAA6nMjJFMpQiatRknmsqDuZN+nT4l78dKkl0LLcYiOrVcxPgj+besnL5H9BuVtINBVOAQVjCFPtgZJAtzSWAdEAIxpc5Ci5kZFjws5QV/q/VEHj/iQ3DzHayfpa0RCX7zIzpmioUY0J5FehnAeFBfatcbXagy01wQqdQJE/NBMMEx0ZhDRDaOHebbGJSiZTKMWaGyWB1/xRnRRd1JwnWhQO+lcETDoiNWF+YqVfkXg9F6IqB3g+HpSZLjzLoXWKzTG71GxJ+FhXbqFEGeBlVHz6izUokrDkZliorAnOQrVfik/uNykpphMZbeYlgrZ5Pq4Vh91p1X3i6aYJJ7ykxSPhEiWAU1QQPqz+FlwUyc/FwhdYexUSau3eTbjO1han4HMhkK3MYa4j4Jv5uYoq1f/KyPMUalc1+LfI1xjOVpJjG7MgqOo3LLEsONsLomawnCR/TZN0GCT0fu2ZwLZBO3RWrSprMs5LPiiyXTV59Jm+XkbJEv8ZPHIVeBOORn3UmKVlx4/ywq5WR0OgizYTyZId9uSFuo8LgKsS2WDvFU6nlW7h8eGssimOuRflApfZeBgQSssZXnPvptbTU3Wxf7FCqLBCtT7FBh2uYPoQ+bCzZ5gyr07Y5m8nh2W2XKTWcjKMFFkER92lctuF+EdDPjTw837bgyrRx2lhGr+KOpMmC8TMJfohsZ3cVIeQsBjeNW2MgbGa+2RrNkFq86IWg1am0ymjJRX1dDragr8cP22JT1bC5I5Abv6qfFFswrrhfEsbxH+vka0uWZnu2OV5WsXvJJTsD8a1XgawlWONOMKlu/8yG4833RQCbC3Kcj9b8+z2ILryKabfgvEPZQqJypFzNc90dvrSdSNToxqwrDP7IMMiAib/F+sJgSEgDKVqLK4mwaNy4YHbj5jF0idtQTXEuYFgyRx6bGfGu1iILeH6hl1s8wPBD2lYx1RE4XjvBK171Z3SeoQfiPaP207WG6ka8Oseq8TBfhyeKimPvgU/Dmhki2pVUqVpFvG9dGkjQOAnrRwMbHbiqmwOyldx3kineNRcItFWGyCKDdmWRdhRw0Xc6AcBc34NcDoyeNZpnJSk08beb8LN9RefvYuaF9kE8EZStnQ27pq/d4iyqsECQVt16fcEbw7NUm8ebmZ/Maykz/ykrCQ8y3dj9Pqd8oYUZmZwGiM8bPPf1yVdp1Vu2fAwLzsjDc++Ikow89ReuSRj0d88BGD/4+A6Tw==

1

u/daggerdragon Dec 11 '23

Your code block is too long for the megathreads. Please edit your comment to replace your oversized code with an external link to your code.

3

u/DeadlyRedCube Dec 11 '23

[LANGUAGE: C++20/23] (305/165)

D11.h on GitHub

Maybe the best rank I've ever gotten? And part 2 would have been lower if I hadn't off-by-oned on part 2 and lost a minute (1 + 100000 != 1000000)

First part I almost went and modified the grid but then I realized I could just keep a count of empty rows as I scanned through (And offset stored galaxy Y coordinates by the count of empty rows found).

Then it took a minute before I realized it also said "empty rows and columns" and I had to add a little "Scan from right to left for empty columns and update the galaxy coordinates accordingly" loop

Then for part 2, I just had to change the increment to a += 999999 (Except I did it as += 1000000 first, whoops), so I kinda lucked into a great part 2 setup!

3

u/r_so9 Dec 11 '23 edited Dec 11 '23

[LANGUAGE: F#] 1552/971

Much easier after the Day 10 part 2 slowdown.

Interesting block - a generic way to find gaps in a list, and the "expand" function (factor is the expansion factor, 2 for part 1 and 1,000,000 for part 2).

let gaps filled =
    Set.difference (Set.ofList [ 0 .. List.max filled ]) (Set.ofList filled)
    |> Set.toList
    |> List.sort
let expand factor galaxies =
    let emptyRows = galaxies |> List.map fst |> gaps
    let emptyCols = galaxies |> List.map snd |> gaps
    galaxies
    |> List.map (fun (r, c) ->
        let emptyRowsBefore =
            emptyRows |> Seq.takeWhile (fun emptyR -> emptyR < r) |> Seq.length
        let emptyColsBefore =
            emptyCols |> Seq.takeWhile (fun emptyC -> emptyC < c) |> Seq.length
        r + (factor - 1) * emptyRowsBefore, c + (factor - 1) * emptyColsBefore)

paste

→ More replies (1)

3

u/EffectivePriority986 Dec 11 '23

[LANGUAGE: perl5]

[code] [embarrassing video]

I wasted waaay too much time on accidentally copying the pre-expanded example (oops!) and an extremely silly and shameful implementation of part 1. For part 2, I started over and the new implementation was actually shorter and simpler. After doing extremely badly on the race, I went back and optimized part 2 even more.

3

u/xelf Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Python] pretty python (~8lines)

Instead of expanding the universe, I just move the galaxies based on how many empty rows/cols there were before it.

board     = open(aocinput).read().splitlines()
emptyrows = [index for index,row in enumerate(board) if all(c=='.' for c in row)]
emptycols = [i for i in range(len(board[0])) if all(row[i]=='.' for row in board)]
universe  = [(x,y) for x,row in enumerate(board) for y,cell in enumerate(row) if cell!='.']
manhattan = lambda p: abs(p[0][0]-p[1][0])+abs(p[0][1]-p[1][1])
move      = lambda g,q: (g[0]+sum(r<g[0] for r in emptyrows)*q, g[1]+sum(r<g[1] for r in emptycols)*q)
print('part 1:', sum(map(manhattan,combinations([move(g,2-1) for g in universe],2))))
print('part 2:', sum(map(manhattan,combinations([move(g,1000000-1) for g in universe],2))))

Had a bit of a debug issue because I forgot the (-1) and the number weren't quite right.

3

u/Szeweq Dec 11 '23

[LANGUAGE: Rust]

My code modifies the points before calculating distances. This makes a significant difference in speed (about 2x) and memory allocation since we don't have to reiterate each empty columns and rows. No maps or sets involved.

Code: https://github.com/szeweq/aoc2023/blob/master/src/bin/11.rs

The hero of this code is space_points function.

→ More replies (1)

3

u/stribor14 Dec 11 '23 edited Dec 11 '23

[LANGUAGE: C++]

Only thing I store are coordinates, so no dilation of huge matrices.

Link to code

edit: cleaning up the code

3

u/Greenimba Dec 11 '23

[LANGUAGE: C#]

First time an AoC solution has just worked right off the bat for 1 & 2, with only a minor mod to allow bigger space multipliers and no off-by-one errors 💪

C# code

3

u/ka-splam Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Dyalog APL]

Part 1, expands the map array, takes galaxy coordinates, sums the outer-product distance between all pairs, halves the total:

map←'#'=↑⊃⎕nget 'C:\sc\AdventOfCode\inputs\2023\11-demo.txt' 1
expand←{⍵⌿⍨1+∧/0=⍵}
distance←{+/|⍺-⍵}
2÷⍨+/,∘.distance⍨⍸ ⍉ expand ⍉ expand map

Yes I tried it on part2 hoping Dyalog's bit-packing would Just Magically Work(tm), but no expanding by millions runs out of memory. Instead of expanding the map, this calculates how many expanded-regions each coordinate crosses in X and Y directions and adds that onto their coordinates:

exp_x ← {((↑⍸⍵)[;2]-n)+⍺×n←(⍵∧+\(⊃⍴⍵)⌿↑,⊂∧⌿0=⍵)[⍸⍵]}
exp_y ← {((↑⍸⍵)[;1]-n)+⍺×n←(⍵∧+⍀(≢⍵)/⍪∧/0=⍵)[⍸⍵]}
2÷⍨+/,∘.distance⍨(1e6 exp_x map),⍨¨ 1e6 exp_y map

It's not nice how hard it was to debug, how repetitive yet different expanding x and expanding y are to cope with being . Or how much code it is overall, compared to u/voidhawk42 's here.

→ More replies (1)

3

u/AnxiousMasterpiece23 Dec 11 '23

[Language: JavaScript]

A fun little search problem. I calculated distance gradients for each galaxy to every other galaxy. I'm happy I ignored the statements about physically adding rows and columns and went with a row and column cost on each node from the start. Part two became just adding a variable expansion factor.

https://github.com/ccozad/advent-of-code/blob/master/day-11.js

3

u/ClutchClimber Dec 11 '23 edited Dec 11 '23

[Language: GO]

  1. get rows and columns ids that doesn't have '#'
  2. find all coordinates of '#' but add to their positions the sum of all the 'empty' rows/col before them multiplied by the distortion effect (2, 10, 100, 1000000)
  3. manhattan distance those coordinate but exclude repetitions

Works for both p1 and p2

Github

3

u/4HbQ Dec 11 '23

[LANGUAGE: Python]

Here's my golfing attempt (197 bytes):

print(sum(map(lambda q:(q:=[sum((1e9,1)[p in q]for p in range(p))
for p in q])and sum(abs(a-b)for a in q for b in q),zip(*[(x,y)for
y,r in enumerate(open(0))for x,c in enumerate(r)if c=='#'])))//2)

Basically just a compact version of my normal solution.

→ More replies (3)

3

u/Cyclotheme Dec 11 '23

[LANGUAGE: QuickBASIC]

Github link

3

u/jvdsandt Dec 11 '23

[LANGUAGE: Smalltalk]

GitHub

3

u/No-Monk8319 Dec 11 '23

[LANGUAGE: Rust]

The solution parses the input into coordinates, then uses hash sets to track rows and columns that require expansion. This approach streamlines the expansion process, avoiding the need to manipulate the matrix directly. The calculation of distances between galaxy pairs is optimized by pre-computing the expanded distances, significantly reducing computation time, especially for the massive expansion factor in Part Two.

Runtime:
Part 1: ~100µs
Part 2: ~100µs

https://github.com/NikolaIliev/aoc_rust/blob/master/src/bin/year2023_day11.rs

3

u/gyorokpeter Dec 11 '23

[LANGUAGE: q]

d11:{[mult;x]
    a:raze til[count x],/:'where each"#"=x;
    a:a-\:min a;
    stretch:{[m;x]d:deltas[x[;0]];x[;0]:sums d+(m-1)*0 or d-1;x}[mult];
    a:stretch a;
    b:asc reverse each a;
    b:stretch b;
    (sum sum sum each/:abs b-/:\:b)div 2};
d11p1:{d11[2;x]};
d11p2:{d11[1000000;x]};

3

u/[deleted] Dec 11 '23

[Language: Python]

Keep all the indexes of the expanded rows and columns but don't expand them. Just do a combinations of all stars positions and take the difference in positions, if any special row or column is between the two stars, add n-1 where n is the number of expansions (2 for part 1 and 106 for part2).

Code here.

3

u/[deleted] Dec 11 '23

[Language: Golang] [Code]

Used the right approach for part 1 to make part 2 really easy, but then I got bit hard by an off by one error since I was adding 1M columns and rows instead of 9999999. Aaargh. Runs in about 500 us on my Lenovo Yoga Slim 7.

3

u/weiss_i_net Dec 11 '23

[LANGUAGE: Python]

paste

The actual solution is straight forward, but I had some fun with trying to turn python into haskell afterwards and want to share :D

I turned one of my list comprehensions into a point-free chain of functions and left the other one normal:

empty_row = list(map(all)(map(map(eq(".")))))(data)
empty_col = [ all(elem == "." for elem in col) for col in zip(*data) ]
→ More replies (1)

3

u/DrunkHacker Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Python]

Pairwise manhattan distance calculation is easy and we just add in an expansion factor for any intervening cols/rows.

def parse(filename):
    lines = open(filename).readlines()
    stars = {(x, y) for y, l in enumerate(lines) for x, c in enumerate(l) if c == "#"}
    stars_x, stars_y = {x for x, _ in stars}, {y for _, y in stars}
    return stars, stars_x, stars_y

def md(p1, p2, s_x, s_y, expansion_factor):
    cr = lambda a, b: range(a, b) if a < b else range(b, a)
    return (
        abs(p1[0] - p2[0])
        + abs(p1[1] - p2[1])
        + sum([expansion_factor - 1 for x in cr(p1[0], p2[0]) if x not in s_x])
        + sum([expansion_factor - 1 for y in cr(p1[1], p2[1]) if y not in s_y])
    )

def distance_sum(stars, s_x, s_y, factor):
    return sum(md(a, b, s_x, s_y, factor) for a, b in it.combinations(stars, 2))

stars, stars_x, stars_y = parse("input")
print(distance_sum(stars, stars_x, stars_y, 2))
print(distance_sum(stars, stars_x, stars_y, 1_000_000))

3

u/IronForce_ Dec 11 '23

[LANGUAGE: Python]

Took me forever to figure out that I didn't have to implement some crazy pathfinding algorithm and that I could've just Google'd/ChatGPT'd "how to calculate shortest distance between 2 points on a grid" to save me 4 hours of work. I ended up using the Manhatten distance formula to calculate the distance after failed attempt after failed attempt to get scipy and tcod to work (but at least I learnt how to use numpy to generate 2D arrays :D)

Second part required a bit of wrangling of my code to get it to work, after I naively tried to brute-force (again) generating 1 million rows/columns per empty row/column and crashing VS Code. I ended up stealing taking inspiration from another solution and used this function to generate an expanded coordinate

    def expand_coordinates(self, coordinates: tuple[int, int], multiplier: int):
        empty_columns_before = sum([1 for col in self.empty_columns if col < coordinates[1]])
        empty_rows_before = sum([1 for row in self.empty_rows if row < coordinates[0]])
        return (coordinates[1] + empty_columns_before * (multiplier - 1), 
                coordinates[0] + empty_rows_before * (multiplier - 1))

Part 1 and Part 2

3

u/p88h Dec 11 '23 edited Dec 19 '23

[LANGUAGE: Mojo] vs [LANGUAGE: Python]

https://github.com/p88h/aoc2023/blob/main/day11.mojo

https://github.com/p88h/aoc2023/blob/main/day11.py

Linear scan to find expansions followed by a linear scan to compute all distances.

No combinatorics is necessary here since Manhattan distances are easily summable. For a brief explanation, let's take a star i at \[Xi,Yi\] and compute all distances from that star, to all K stars such that for each j their Xj <= Xi and Yj < = Yi. Or, in space-faring terminology, 'up and to the left'. Each individual distance is then (Xi-Xj+Yi-Yj) for all j from 1 to K. But that can also be written as K\*(Xi+Yi)-SUM(Xj+Yj) over K; and it's reasonably easy to keep that sum up to date as you traverse through the hyperspace. The 'up and to the right' is left as an exercise for the reader (or you can attempt to decipher my obscure scribbles totally readable code linked above). With that, we get the time for each part to around 20 μs in Mojo, and a bit more for its Pythonic friends.

Task             Python      PyPy3       Mojo       parallel    * speedup
Day11 Part1     1.94 ms     0.14 ms    [0.02 ms]    n/a         * 5 - 80
Day11 Part2     1.95 ms     0.14 ms    [0.02 ms]    n/a         * 5 - 80

3

u/Valefant Dec 11 '23

[LANGUAGE: Kotlin]

Initially I used an array to represent and modify the grid for part 1, but this was not feasible for part 2. I rewrote it to track the empty distance and then shifting the coordinates of each galaxy

https://gist.github.com/Valefant/8567597bd9fadf9637baf8785972cf00

3

u/dodo-obob Dec 11 '23 edited Dec 11 '23

[LANGUAGE: rust]

https://github.com/dlesbre/advent-of-code/blob/master/2023/11/puzzle.rs

I do love a puzzle where part 2 is trivial ! I store the galaxies as pairs of coordinates. Expansion is done by computing the set of missing columns/rows, then shifting all coordinates by the number of smaller elements of that set. For part 2, just add a multiplier to that shift (which isn't 1_000_000, it's 999_999, small fact that took WAY too long to debug)...

You could go bit faster by using a balanced ordered set to quickly get the number of missing rows/columns smaller than self in O(log n) time instead of O(n), but the solution is fast enough as is (sub millisecond time).

3

u/Valletta6789 Dec 11 '23

[LANGUAGE: Python]

Mine solution for both parts. Found indices of empty rows and cols, and if a num coordinate is higher than one of the indices, then I update a coordinate by multiplyng num of previous indices by koef.

def get_galaxies(image, koef=1):
    galaxies = {}

    row_dots_indices = [i for i, row in enumerate(image) if all(element == '.' for element in row)]
    col_dots_indices = [i for i, col in enumerate(zip(*image)) if all(element == '.' for element in col)]

    for i, row in enumerate(image):
        for j, col in enumerate(row):
            if col.isdigit():
                i_galaxy, j_galaxy = i, j
                for idx, row_idx in enumerate(row_dots_indices[::-1]):
                    if i_galaxy > row_idx:
                        i_galaxy += (len(row_dots_indices) - idx) * (koef - 1)
                        break
                for idx, col_idx in enumerate(col_dots_indices[::-1]):
                    if j_galaxy > col_idx:
                        j_galaxy += (len(col_dots_indices) - idx) * (koef - 1)
                        break
                galaxies[col] = (i_galaxy, j_galaxy)

    print(galaxies)
    return galaxies


def get_dist(first_coord, second_coord):
    x1, y1 = first_coord
    x2, y2 = second_coord
    return abs(x1 - x2) + abs(y1 - y2)


@timeit
def part1(path, koef=1):
    with open(path, 'r') as f:
        image = f.read().split('\n')
        counter = itertools.count(1)
        image = [list(str(next(counter)) if symbol == '#' else symbol for symbol in list(line)) for line in
                image]

        galaxies = get_galaxies(image, koef)

        total = 0
        for k1, v1 in galaxies.items():
            for k2, v2 in galaxies.items():
                if k1 < k2:
                    total += get_dist(v1, v2)

        return total

https://github.com/Aigul9/AdventOfCode/blob/master/year2023/Day_11/main.py

3

u/After-Leadership2183 Dec 11 '23

[LANGUAGE: Golang]

After a very difficult yesterday's puzzle (part 2), today is a breeze

https://github.com/macos-fuse-t/aoc/tree/main/2023/11

3

u/Diderikdm Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Python]

with open("day11.txt", "r") as file:
    data = file.read().splitlines()
    grid = {(x,y) for x in range(len(data[0])) for y in range(len(data)) if data[y][x] == "#"}
    horizontal = set(range(len(data[0]))) - {x for x, y in grid}
    vertical = set(range(len(data))) - {y for x, y in grid}
    s, s2 = 0, 0
    for galaxy in set(grid):
        grid.remove(galaxy)
        for other_galaxy in grid:
            left_h, right_h = sorted([galaxy[0], other_galaxy[0]])
            left_v, right_v = sorted([galaxy[1], other_galaxy[1]])
            sum_dist = right_h - left_h + right_v - left_v
            extra_h = sum(1 for x in horizontal if left_h < x < right_h)
            extra_v = sum(1 for x in vertical if left_v < x < right_v)
            s += sum_dist + (sum_extra := extra_h + extra_v)
            s2 += sum_dist + sum_extra * 999999
    print(s, s2)

3

u/patrick_iv Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Kotlin]

fun main() {
    day(n = 11) {
        part1 { input ->
            calculateSumOfDistances(input, expandMultiplier = 1)
        }

        part2 { input ->
            calculateSumOfDistances(input, expandMultiplier = 1_000_000 - 1)
        }
    }
}

private fun calculateSumOfDistances(input: Input, expandMultiplier: Int): Long {
    val galaxies = input.lines.flatMapIndexed { y, row ->
        row.mapIndexedNotNull { x, c ->
            if (c == '#') Point(x, y) else null
        }
    }

    val minMaxX = galaxies.minOf(Point::x)..galaxies.maxOf(Point::x)
    val minMaxY = galaxies.minOf(Point::y)..galaxies.maxOf(Point::y)

    val emptyX = minMaxX - galaxies.map(Point::x).toSet()
    val emptyY = minMaxY - galaxies.map(Point::y).toSet()

    val expanded = galaxies.map { galaxy ->
        val offsetX = emptyX.count { it < galaxy.x } * expandMultiplier
        val offsetY = emptyY.count { it < galaxy.y } * expandMultiplier
        Point(galaxy.x + offsetX, galaxy.y + offsetY)
    }

    return expanded
        .flatMap { a -> expanded.map { b -> a.distance(b).toLong() } }
        .sum() / 2
}

private fun Point.distance(other: Point): Int =
    abs(other.x - x) + abs(other.y - y)

3

u/SpaceHonk Dec 11 '23

[Language: Swift] (code)

I initially implemented Part 1 by adding rows and columns to a 2D array of (empty/galaxy) values, and that worked just fine for a growth of 2.

For part 2 I had to completely rewrite this to be based of an array of galaxy coordinates instead, so parts 1 and 2 just pass different "growth" parameters to the same function.

AFACIT there's no suitable "permutations" or "combinations" method in the stdlib so I had to write my own.

→ More replies (4)

3

u/errop_ Dec 11 '23 edited Dec 11 '23

[Language: Python 3]

Not sure if I could make it shorter/more efficient, but I'm quite happy I was lucky enough to set up Part 1 in a way such that I didn't need any hard refactor for Part 2. There's actually no need to re-run the code for Part 1 with different "expansion factor", as the last line of my solution suggests.

Paste

3

u/Radiadorineitor Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Dyalog APL]

p←'#'=↑⊃⎕NGET'11.txt'1
F←{
    x←+\1⌈⍺×~1∊¨↓⍉⍵
    y←+\1⌈⍺×~1∊¨↓⍵
    .5×+/,∘.(+/∘|-)⍨{(y⌷⍨⊣/⍵),x⌷⍨⊢/⍵}¨⍸⍵
}
2 F p ⍝ Part 1
16←⎕PP ⋄ 1e6 F p ⍝ Part 2

3

u/mist_mud Dec 11 '23

[LANGUAGE: Javascript]

Created a 'Galaxy' class to store galaxies, along with a couple of sets to store empty row/cols.

Ran through each galaxy, noting how far it would expand, then expanded the universe :)

Manhattan for distances.

Learnt a little bit about field declarations, static methods, and set creation from an Array in the process, which was nice :)

partOne gist

partTwo gist

3

u/odnoletkov Dec 11 '23 edited Dec 11 '23

[LANGUAGE: jq] github

[inputs] | [
  [map(./"") | transpose[] | add], . | [
    [foreach map(match("#").length // 1E6)[] as $n (0; . + $n)]
    [to_entries[] | .key + (.value | scan("#") | 0)]
  ] | combinations(2) | .[0] - .[1] | fabs
] | add/2
→ More replies (1)

3

u/azzal07 Dec 11 '23

[LANGUAGE: Awk] Using the most advanced vectorised absolute value function: put all the values in $0 and gsub(/-/, "").

END{for(;i++<C;V[i]=e+=!X[i])H[i]=f+=!Y[i];for(y in G){x=G[y]
delete G[y];for(k in G){$0=V[x]-V[a=G[k]]" "H[+y]-H[+k]" "x-a
$4=k-y;gsub(/-/,z);B+=$1+$2;A+=$3+$4}}print A+B"\n"A-B+B*1e6}
{for(C=1;$0;C+=sub(/./,z))if(/^#/)Y[NR]=X[G[NR,C]=C]=1;C+=NR}

This calculates the pairwise distances without expansion (A) and the distance from one expansion (B) in a single pass. The final result is then A + B*t where t is the time (e.i. the number of expansions).

3

u/sergiosgc Dec 11 '23

[LANGUAGE: Rust]

Code

Most of the work is done preparing the input. I read a set of coordinates, then pair each coordinate with (empty_cols_before, empty_rows_before). Then, it's just iterating over all combinations of galaxies and calculating the manhattan distance after expansion for each pair of galaxy coordinates.

For part 2, the only difference is the expansion multiplier. I managed to avoid the obvious off-by-one error trap.

3

u/Tipa16384 Dec 11 '23

[LANGUAGE: Python]

Avoided obvious trap :-)

paste

→ More replies (2)

3

u/tcbrindle Dec 11 '23

[Language: C++]

A nice gentle one today!

While reading in the values, if there was no galaxy found in a particular row then I increment the running y value by the expansion factor. At the same time, I record which columns had galaxies found in them. Once I've read in all the positions (with "expanded" y-coords) I go back and expand the x-coords for each galaxy as appropriate.

After that, calculating the distances is simple, although it does remind me that I need to add a combinations function to my library...

Github

→ More replies (2)

3

u/silt_lover Dec 11 '23 edited Dec 11 '23

[LANGUAGE: rust]

Hard mode rust - #![no_std], no std collections. I allocated 3 slices today: empty rows, columns and galaxy coordinates. Then just an iteration over all pairs of coordinates, checking which empty rows/columns they have between them. Not depending on dynamic memory allocation made me avoid the trap today :^)

github

3

u/msschmitt Dec 11 '23

[Language: Python 3]

Solution with expansion factor, runs in 0.05s

My idea for part 1 was that instead of capturing the galaxies with x,y coordinates from the input, I (step 1) create two lists: one is indexed by x coordinates, other by y. Each lists's elements point to a list of the galaxies that have that x or y coordinate.

This way I could expand the universe by (step 2) just running through each list, and inserting a new empty element at where there's an element with an empty list. The nature of the list means that this automatically adjusts the x or y coordinates that follow that insertion point.

Only then do I run through each list, (step 3) building a new list of galaxies with their x,y coordinates. And then just Manhattan distance (thanks AoC!) to calculate the path length.

For part 2 inserting a million elements wasn't going to scale, but I realized that I didn't actually need to do that at all. I could jump from step 1 to 3, by having step 3 keep a running x or y value as it iterates through the x or y list, incrementing x or y by either 1 or the expansion factor.

3

u/scibuff Dec 11 '23 edited Dec 11 '23

[Language: JavaScript]

const parse = (input, scale) => {
  const lines = input.trim().split(/\r?\n/);
  const rowMap = {};
  const columnExpansions = {};
  const galaxies = [];
  for (let row = 0; row < lines.length; row++) {
    const line = lines[row];
    const matches = [...line.matchAll(/#/g)];
    if (matches.length == 0) { rowMap[row] = true; }
    else {
      for (let match of matches) {
        columnExpansions[match.index] = 0;
        galaxies.push({
          row: row + Object.keys(rowMap).length * (scale - 1),
          col: match.index,
        });
      }
    }
  }
  let counter = 0;
  const numberOfColumns = lines[0].length;
  for (let i = 0; i < numberOfColumns; i++) {
    if (columnExpansions[i] === undefined) { counter += scale - 1; }
    else { columnExpansions[i] = counter; }
  }
  for (let galaxy of galaxies) { galaxy.col += columnExpansions[galaxy.col]; }
  return galaxies;
};
const taxicab = (p1, p2) => {
  return Math.abs(p1.row - p2.row) + Math.abs(p1.col - p2.col);
};
const solvePart1 = (input, scale = 2) => {
  const galaxies = parse(input, scale);
  let sum = 0;
  for (let i = 0; i < galaxies.length - 1; i++) {
    for (let j = i; j < galaxies.length; j++) {
      sum += taxicab(galaxies[i], galaxies[j]);
    }
  }
  return sum;
};
const solvePart2 = (input) => {
  return solvePart1(input, 1_000_000);
};

3

u/[deleted] Dec 11 '23

[LANGUAGE: Rust]

My Day 11 on GitHub

Luckily I didn't actually expand the galaxy in part 1, but simply counted empty rows/columns as 2 when working out the distance. This meant part 2 was as simple as adding a parameter to my two functions - nice!

3

u/Jessseee Dec 11 '23

[LANGUAGE: Python]

I am happy today's puzzle was a little easier again. I was overthinking the solution at first, but quickly came to this (in my opinion) quite simple solution.

from itertools import combinations

import numpy as np

from aoc.helpers import *


def get_expansion(star_map, expansion):
    space_map = np.ones(star_map.shape, dtype=int)
    empty_rows = np.array([i for i, row in enumerate(star_map[:]) if not row.any()])
    empty_cols = np.array([i for i, col in enumerate(star_map.T[:]) if not col.any()])
    space_map[empty_rows] = expansion
    space_map[:, empty_cols] = expansion
    return space_map


def get_stars(star_map, expansion):
    space_map = get_expansion(star_map, expansion)
    stars = [(sum(space_map[:row, col]), sum(space_map[row, :col])) for row, col in np.argwhere(star_map)]
    return stars


def sum_distances(stars):
    distance = np.int64()
    for star1, star2 in combinations(stars, 2):
        distance += manhattan_distance(star1, star2)
    return distance


if __name__ == "__main__":
    star_map = np.array(import_input("\n", lambda row: [x == "#" for x in row]))

    print(
        "Sum of the distance between all stars (small expansion):    ",
        sum_distances(get_stars(star_map, 2)),
    )
    print(
        "Sum of the distance between all stars (large expansion):",
        sum_distances(get_stars(star_map, 100000)),
    )

Also on GitHub

3

u/SwampThingTom Dec 11 '23

[LANGUAGE: Rust]

Blessedly easy after yesterday.

Github

3

u/aexl Dec 11 '23

[LANGUAGE: Julia]

Fun little puzzle today! I think that I have never solved part 2 in such a short time after having solved part 1. I simply counted the empty rows and columns between two galaxies and "moved" one galaxy further apart when calculating the distance. For part 2 I simply had to multiply the number of empty rows and columns by (1000000 - 1), that's it.

Solution on GitHub: https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day11.jl

Repository: https://github.com/goggle/AdventOfCode2023.jl

3

u/bo-tato Dec 11 '23

[LANGUAGE: Common Lisp]

;; 2 for part1
;; 1000000 for part2
(defparameter *expansion-factor* (1- 1000000))

(let* (rows
       cols
       (galaxies (loop for line in (read-file-lines "input.txt")
                       for row from 0
                       finally (setf rows row)
                       append (loop for char across line
                                    for col from 0
                                    finally (setf cols col)
                                    when (char= char #\#)
                                      collect (cons row col))))
       (empty-rows (set-difference (iota rows) (mapcar #'car galaxies)))
       (empty-cols (set-difference (iota cols) (mapcar #'cdr galaxies))))
  (summing
    (map-combinations (lambda-bind (((row1 . col1) (row2 . col2)))
                        (when (> row1 row2) (rotatef row1 row2))
                        (when (> col1 col2) (rotatef col1 col2))
                        (sum (+ (- row2 row1)
                                (- col2 col1)
                                (* *expansion-factor*
                                   (+ (count-if λ(< row1 _ row2) empty-rows)
                                      (count-if λ(< col1 _ col2) empty-cols))))))
                      galaxies
                      :length 2)))

3

u/Gravitar64 Dec 11 '23

[LANGUAGE: Python]

Part 1 & 2, 26 sloc, runtime 47ms

Storing only the coordinates of galaxies, create a list of empty rows/cols by set-subtraction, expand by adding 1/999.999 to x- (insert col) or y- (insert row) coordinates in the reverse list of empty rows/cols. Sum the Manhattan Distances for every itertool.combinations-Pair.

import time
from itertools import combinations


def load(file):
  with open(file) as f:
    return [row.strip() for row in f]


def expand(galaxies, empties, exp):
  for n, row in reversed(empties):
    if row:
      galaxies = [(x, y + exp) if y > n else (x, y) for x, y in galaxies]
    else:
      galaxies = [(x + exp, y) if x > n else (x, y) for x, y in galaxies]
  return galaxies


def distances(galaxies):
  return sum(abs(d1 - d2) for a, b in combinations(galaxies, 2) for d1, d2 in zip(a, b))


def solve(p):
  galaxies = [(x, y) for y, row in enumerate(p) for x, c in enumerate(row) if c == '#']
  cols, rows = [{pos[d] for pos in galaxies} for d in (0,1)]

  empties = [(n,True) for n in (set(range(len(p))) - rows)]
  empties += [(n,False) for n in (set(range(len(p[0]))) - cols)]
  empties.sort()
  
  g1 = expand(galaxies, empties, 1)
  g2 = expand(galaxies, empties, 999_999)
  
  return distances(g1), distances(g2)


time_start = time.perf_counter()
print(f'Part 1 & 2: {solve(load("day11.txt"))}')
print(f'Solved in {time.perf_counter()-time_start:.5f} Sec.')

3

u/remy_porter Dec 11 '23

[LANGUAGE: Python]

Pretty straightforward. I made it up to a few days ago without using NumPy, but now that the seal is broken, I can't be arsed to avoid it.

3

u/xHyroM Dec 11 '23

[LANGUAGE: Python]

part 1
part 2

I used Manhattan distance instead doing BFS (probably everyone did) - https://en.wikipedia.org/wiki/Taxicab_geometry
For part 2, i used bisect

3

u/jstanley0 Dec 11 '23

[Language: Ruby]

I stored the galaxy coordinates as a hash from X coordinate to array of Y coordinates, and I expanded space with the following function:

# Space.galaxies is a hash from X coordinate to array of Y coordinates
def expand_horizontally(factor = 2)
  ret = Space.new
  cols = galaxies.keys.sort
  x0 = cols.shift
  ret.galaxies[x0] = galaxies[x0]
  expansion = 0
  while (x = cols.shift)
    expansion += (x - x0 - 1) * (factor - 1)
    ret.galaxies[x + expansion] = galaxies[x]
    x0 = x
  end
  ret
end

I then transposed the array and repeated for the other axis, i.e.

def expand(factor = 2)
  expand_horizontally(factor).transpose.
    expand_horizontally(factor).transpose
end

For part 2, all I had to do was add the factor argument. My initial answer was too high until I realized I needed to subtract 1 from the scale factor.

Full solution

3

u/mcnadel Dec 11 '23

[LANGUAGE: Python]

Today was a lot of fun! My solution involves two main components:

  1. Saving the indices of the columns/rows to expand and then adjusting the coordinates of the galaxies accordingly. For instance, if a row (let's say, row 2) needs to be expanded, and there's a galaxy on row 3, it will be considered as if it were on row 4. Adapting this for part 2 was straightforward; I simply increased the amount to raise the coordinates from 1 to 999999 (1 million if starting from 0).
  2. Utilizing combinations from itertools to generate all possible pairs of galaxies and then calculating the distance using the Manhattan Distance formula.

I tried to make my code very short while still keeping it somewhat readable. GitHub

3

u/mothibault Dec 11 '23 edited Dec 17 '23

[LANGUAGE: JavaScript]

If, like me, you built a code that doesn't scale well for part 2, that's the hack you're looking for. https://github.com/yolocheezwhiz/adventofcode/blob/main/2023/day11.js

(with tons of inline comments)

→ More replies (2)

3

u/CCC_037 Dec 11 '23

[Language: Rockstar]

Let's start by building a spare universe, shall we?

And then, for part 2, let's build a bigger spare universe.

I would also like to note for the record that both solutions today successfully used a recursive function to add up the distances.

3

u/artesea Dec 11 '23

[LANGUAGE: JavaScript]

Part 1: Over complicated, does several twists on the map, adding in extra .... lines where needed. Then plots where each galaxy [x,y] is, loop through each against any further galaxies. Simple difference between the x and y cords gives the distance. Sum all together.

Part 2: Stopped redrawing the map, instead found the original x,y locations on the input, and also tracked any cols or rows which were full of .... Then when looping through the galaxies check if any of the paths cross an empty row or column and add the expansion value (minus one as it's already been counted).

→ More replies (1)

3

u/wsgac Dec 11 '23

[LANGUAGE: Common Lisp]

Source

I wanted to go with 2D arrays for efficiency, but found it a bit hard to expand them, so I went with lists instead.

In Part 1 I recreate the literal expanded map, locate the galaxies and sum their distances in the NYC metric.

In Part 2 I'm more economical and just locate empty rows and columns, and then adjust galaxy coordinates according to the numbers of preceding row and column "empties".

3

u/vss2sn Dec 11 '23

[LANGUAGE: C++]

Part 1

Part 2

(Each file is a self-contained solution)

3

u/psr Dec 11 '23

[LANGUAGE: Python]

I was quite happy with mine, once I worked out why expansion wasn't working.

from functools import partial
from itertools import combinations, groupby, pairwise

def parse_galaxies(input_):
    for row, line in enumerate(input_):
        for col, c in enumerate(line):
            if c == '#':
                yield complex(col, row)

def expand_galaxies(galaxies, *, scale_direction, grouping_key, expand_amount=2):
    galaxies = sorted(galaxies, key=grouping_key)
    # Hack to avoid losing groups when using pairwise
    grouped = groupby(galaxies, grouping_key)
    grouped = ((k, iter(list(g))) for k, g in grouped)

    shift_by = 0
    for (line1, galaxies1), (line2, galaxies2) in pairwise(grouped):
        yield from galaxies1  # Will already be exhausted except on first galaxies
        missing_lines = line2 - line1 - 1
        shift_by += missing_lines * (expand_amount - 1) * scale_direction
        yield from (g + shift_by for g in galaxies2)

expand_vertically = partial(expand_galaxies, scale_direction=1j, grouping_key=complex.imag.__get__)
expand_horizontally = partial(expand_galaxies, scale_direction=1, grouping_key=complex.real.__get__)

def manahattan_distance(c1, c2):
    difference = c1 - c2
    return int(abs(difference.real) + abs(difference.imag))

def part_1(input_, expand_amount=2):
    galaxies = parse_galaxies(input_)
    galaxies = expand_horizontally(galaxies, expand_amount=expand_amount)
    galaxies = expand_vertically(galaxies, expand_amount=expand_amount)
    galaxy_pairs = combinations(galaxies, 2)
    return sum(manahattan_distance(g1, g2) for g1, g2 in galaxy_pairs)

with open('inputs/day11.txt', 'r', encoding='utf-8') as input_:
    print(f"{part_1(input_)=}")

part_2 = partial(part_1, expand_amount=1_000_000)
with open('inputs/day11.txt', 'r', encoding='utf-8') as input_:
    print(f"{part_2(input_)=}")

3

u/bamless Dec 11 '23

[LANGUAGE: J*]

Today was interesting.
I see pretty much everyone used Manhattan distance which is arguably the optimal solution. Unfortunately I wasn't familiar with it (but now I am wich is an awesome part of reading these posts after having done the problem) and to be honest I didn't give the puzzle much of a tought, so I went with the solution that seemed the most straightforward to me.
I basically used a modified version of Bresenham's algorithm that never does a diagonal step and rasterized a line between each pair. During line rasterization, I correct the number of steps by looking up a precomputed table of expansion factors instead of pre-expanding the input.
The runtime isn't exceptional (~0.5s on my machine) but much faster that I would've expected for this sort of algorithm on an interpreted language.

Part 1 & 2

3

u/rune_kg Dec 11 '23

[LANGUAGE: python]

from itertools import accumulate, combinations

def solve(file_name, spacing):
    rows = list(open(file_name).read().splitlines())
    rng = range(len(rows))
    cols = [[row[i] for row in rows] for i in rng]
    ys = list(accumulate(1 if "#" in y else spacing for y in rows))
    xs = list(accumulate(1 if "#" in x else spacing for x in cols))
    points = [(xs[x], ys[y]) for x in rng for y in rng if rows[y][x] == "#"]

    return sum(abs(x1 - x0) + abs(y1 - y0)
               for (x0, y0), (x1, y1) in combinations(points, 2))


DAY = 11
print(solve(f"inputs/{DAY}i.txt", 2)) # part1
print(solve(f"inputs/{DAY}i.txt", 1000000)) # part2

https://github.com/runekaagaard/aoc-2023/blob/master/day11.py

Super happy with this one. Did the first ten days with Nim and nice to be back to python again :)

→ More replies (2)

3

u/RookBe Dec 11 '23

[Language: Rust] [Language: all] Blogpost explaining my solution in Rust, the explanation can be used for all languages: https://nickymeuleman.netlify.app/garden/aoc2023-day11

3

u/SymmetryManagement Dec 11 '23 edited Dec 12 '23

[LANGUAGE: java]

https://github.com/linl33/adventofcode/blob/year2023/year2023/src/main/java/dev/linl33/adventofcode/year2023/Day11.java

solveVector in the above file implements the solution outlined below.

A vectorized solution with time complexity O(R*C) and O(R+C) extra space, where R is the number of rows in the grid and C is the number of columns in the grid.

Observations

  • x-axis and y-axis pairwise distances can be calculated independently. So the galaxies that share the same x or y coordinate can be calculated together, which reduces the pairwise combinations from n2 /2 (where n is the total number of galaxies) to R*C.
  • Extra space added by empty row/col can be calculated independently from galaxy pairwise distance. The extra space an empty row/col contributes is equal to the number of galaxies on one side of the empty row/col multiplied by the number of galaxies on the other side then multiplied by the size of the extra space of 1 empty row/col. This also means that the answer for part 1 and part 2 can be calculated simultaneously without overhead but I like to keep each part self-contained so this was not done in my implementation.

Benchmark

0.023 ms for each part including IO and parsing.

Edit

Reading another solution made me realize I was very close to a much simpler and more efficient solution. I can just calculate every row/column the way the empty rows and columns are calculated - by counting the galaxies before and after (or on) each row/column. 🤦‍♀️

Now it takes 0.020 ms per part.

→ More replies (2)

3

u/vbe-elvis Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Kotlin]

This morning Part 2 got to me with an off by 1 error, due to not taking the existing line into account. Figured out my mistake quickly in the evening. Here is my solution:

https://pastebin.com/MjCX2UyD

Creating the galaxy maps (repeat the forEach for xPos):

val additionalExpansion = expansion - 1 // Minus the existing empty line
var galaxies: List<Pair<Long, Long>> = galaxymap.mapIndexed { y, line ->
    line.mapIndexed { x, point ->  if (point == '#') x.toLong() to y.toLong() else null }.filterNotNull()
}.flatten()

var shiftY = 0L
galaxymap.indices.forEach { y ->
    if (galaxies.none { it.second == y + shiftY }) {
        val yPos = y + shiftY
        shiftY += additionalExpansion
        galaxies = galaxies.map {
            if (it.second > yPos) (it.first to it.second + additionalExpansion) else it
        }
    }
}
→ More replies (1)

3

u/Ok-Group4131 Dec 11 '23

[LANGUAGE: Python]

part 1&2 - github

I did part one by manually expanding the grid, and then stored the indexes of the empty rows and columns for part 2. My part two solution is quite slow, so any ideas for optimising would be appreciated

3

u/Kfimenepah Dec 11 '23

[LANGUAGE: NodeJs with Typescript]

Day11

Today was one of those sweet days where I managed to solve part 1 in such a way that part 2 was no issue at all. It took me about a minute to finish part 2 and that was only because my initial expansion rate was set to 1M instead of 1M - 1

3

u/Outrageous72 Dec 11 '23

[LANGUAGE: C#] https://github.com/ryanheath/aoc2023/blob/master/Day11.cs

Not too difficult, I just knew the expansion would be bigger in part 2 :) The only gotcha was the wording "10 times bigger", but a simple minus 1 fixed that problem :D

    long Part1(string[] lines) => SumGalaxyDistances(lines, 2);
    long Part2(string[] lines, int grow) => SumGalaxyDistances(lines, grow);

    static long SumGalaxyDistances(string[] lines, int grow)
    {
        var galaxies = ParseGalaxies(lines);
        ExpandGalaxies(galaxies, grow);

        var pairs = galaxies
            .SelectMany((g1, i) => galaxies.Skip(i + 1).Select(g2 => (g1, g2)));

        return pairs.Select(ManhattanDistance).Sum();
    }

    static long ManhattanDistance(((int y, int x) a, (int y, int x) b) p) 
        => Math.Abs(p.a.x - p.b.x) + Math.Abs(p.a.y - p.b.y);

    static void ExpandGalaxies((int y, int x)[] galaxies, int grow)
    {
        grow--;

        // expand x
        var maxX = galaxies.Max(g => g.x);
        for (var x = 0; x < maxX; x++)
            if (galaxies.All(g => g.x != x))
            {
                for (var i = 0; i < galaxies.Length; i++)
                    if (galaxies[i].x > x)
                        galaxies[i] = (galaxies[i].y, galaxies[i].x + grow);
                maxX += grow; x += grow;
            }

        // expand y
        var maxY = galaxies.Max(g => g.y);
        for (var y = 0; y < maxY; y++)
            if (galaxies.All(g => g.y != y))
            {
                for (var i = 0; i < galaxies.Length; i++)
                    if (galaxies[i].y > y)
                        galaxies[i] = (galaxies[i].y + grow, galaxies[i].x);
                maxY += grow; y += grow;
            }
    }

    static (int y, int x)[] ParseGalaxies(string[] lines) 
        => [..lines
                .SelectMany((line, y) => line.Select((c, x) => (c, y, x)))
                .Where(t => t.c == '#')
                .Select(t => (t.y, t.x))];
→ More replies (1)

3

u/rjray Dec 11 '23

[LANGUAGE: Clojure]

GitHub

Not my best code-- I (foolishly) implemented part 1 by actually expanding the matrix representation of the "image". When part 2 was unlocked, that obviously wouldn't have been viable. So what I wrote for part 2 is also what I should have written for part 1. I'll revisit the code later to clean it up and improve it.

3

u/0x2c8 Dec 11 '23

[Language: Python]

https://github.com/alexandru-dinu/advent-of-code/blob/main/2023/11/solve.py

I really like problems where the grid operations can be simplified using numpy.

Representing the grid as a binary numpy array allows for straightforward detection of empty rows and cols via argwhere. The shortest distance b/w 2 points is then given by Manhattan distance plus checking how many empty rows & cols are b/w the 2 points.

3

u/Secure_Pirate9838 Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Rust]

YouTube screencast: https://youtu.be/VOahESJS-vc GitHub: https://github.com/samoylenkodmitry/AdventOfCode_2023/blob/main/src/day11.rs

Today, part 2 was the same as part 1. Copilot was practically useless to teach me how to use PriorityQueue, thankfully Google is still there.

Update after I saw other's solutions: Manhattan...

3

u/Hunky524 Dec 11 '23

[Language: Rust] GitHub

Nothing crazy, my goal with these is to write code that is performant, and readable. Uses itertools to create the tuples, and rayon for parallel distance computation.

→ More replies (1)

3

u/icub3d Dec 11 '23

[LANGUAGE: Rust]

Yay, manhattan distance (apparently also called taxicab geometry) :)

Solution: https://gist.github.com/icub3d/1057d162dbee5fe9f0fb96563b6a20fd

Analysis: https://youtu.be/N3Y5ZSzAvXU

3

u/biggy-smith Dec 11 '23

[LANGUAGE: C++]

Manhattan distance makes its first appearance this year! I knew there would be funny business in part 2, so in part 1 I stored and offseted individual points, then used the Manhattan distance to sum. Changing the expansion value worked for part 2, although I got caught out by setting expansion incorrectly the first time. Fun one.

https://github.com/biggysmith/advent_of_code_2023/blob/master/src/day11/day11.cpp

3

u/oxlade39 Dec 11 '23

[LANGUAGE: Rust]

Code

Had already written manhattan distance and point based coordinate utilities

3

u/Imaginary_Age_4072 Dec 11 '23

[Language: Common Lisp]

Day 11

My solution just figures out where the blank rows/columns are then adds any that are between a galaxy pair to the manhattan distance.

(defun galaxy-distance (a b blanks expansion-factor)
  (flet ((coord-distance (a b blanks)
           (+ (abs (- b a))
              (* (1- expansion-factor) (included-blanks a b blanks)))))
    (reduce #'+ (map 'list #'coord-distance a b blanks))))

I got part 2 wrong for my first answer as I tried to add 1,000,000 lots of the blank lines, when it should have been adding 999,999 since there's already one lot included in the manhattan distance.

3

u/CainKellye Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Rust]

Such a relief after yesterday's (and today's) hell with day 10 part 2!

https://github.com/cainkellye/advent_of_code/blob/main/src/y2023/day11.rs

3

u/yieldtoben Dec 11 '23

[LANGUAGE: PHP]

PHP 8.3.0 paste

Execution time: 0.0486 seconds
Peak memory: 9.9729 MiB

MacBook Pro (16-inch, 2023)
M2 Pro / 16GB unified memory

3

u/aarnens Dec 11 '23 edited Dec 11 '23

[LANGUAGE: Rust] Both parts run in ~560ms, which is really slow considering it's like 5x the other days put together, lol

I know that it's because of my triple for loops, but as of right now I don't know how to get rid of them lol. Might update later.

At least I didn't have to think a lot for part 2

Got both parts to run in ~12ms, while still keeping the triple for loops. Not quite sure why it's so much faster (or rather, why the original was that much slower) but i'm not complaining lol.

improved code: https://pastebin.com/dJ9bE6bp

original paste: https://pastebin.com/UHt9jbjG

3

u/darkgiggs Dec 11 '23

[LANGUAGE: Rust]
Paste
I'm learning Rust this year. I had fun doing row expansion while parsing the input and column expansion in a single pass through the vector of galaxies.
I do both parts at the same time and it runs in roughly 5ms on my laptop.

3

u/xavdid Dec 12 '23

[LANGUAGE: Python]

Step-by-step explanation | full code

Very pleased with today! I got the list of grid points that held galaxies. Then I got all the empty lines by removing any row / col from the respective set of all possible points. Then I iterated all points and increased row and col by the number of empty rows that were smaller than that row/col, which stretched everything nicely. From there, it was taxicab distance.

Part 2 was just turning part 1 into a function and adding a multiplier! The increased distances didn't slow me down at all since they were just bigger numbers. Simple and clean.

3

u/AJMansfield_ Dec 12 '23 edited Dec 12 '23

[LANGUAGE: Fortran]

A thoroughly vectorized solution that runs in 147 μs (274 μs including I/O).

https://github.com/AJMansfield/aoc/blob/master/2023-fortran/src/11/cosmos.f90

3

u/iskypitts Dec 13 '23

[LANGUAGE: Zig]

Part 1 (naive solution where I actually expanded the map) and Part 2

3

u/Robin_270 Dec 15 '23 edited Dec 15 '23

[LANGUAGE: Python]

Not so easy one, but I've managed to solve both parts. It's a combination of brute-force approach for the first part (where it didn't cause any problems) and a smart one for the second part. You can see it in the repo here.

4

u/Sourish17 Dec 11 '23

[LANGUAGE: Python3] 78/166

p1: https://github.com/SourishS17/aoc2023/blob/main/eleven-a.py

p2: https://github.com/SourishS17/aoc2023/blob/main/eleven-b.py

Finally got some more global points :D (totally not spending all my time on AoC when my Cambridge Uni admissions interview is tomorrow...)

My only note today is that you can transpose an array in Python by doing this (check my p1 code to see how I used it)

y=list(zip(*y))

Happy Monday everyone :)

8

u/jeffgreenfan Dec 11 '23

Good luck on your interview!

→ More replies (1)
→ More replies (2)

2

u/piman51277 Dec 11 '23

[LANGUAGE: JavaScript] 76/168

Link to GitHub

Today was fairly simple. Part 1 uses the naive solution while part 2 uses the optimal solution.
The key part was knowing shortest distance between points on an unrestricted grid is just taxicab distance.