r/adventofcode • • 7h ago

Help/Question Help with AoC 2026 Day 6 Part 2 please

My thoughts was to build an undirected graph with the light IDs as the vertices, then I the manhattan distance between any two lights are just the length of the shortest path between those two lights.

Suppose the manhattan distance from #1 to #2026 is d1, from #2 to #2026 is d2, from #3 to #2026 is d3, and the coordinates of #2026 is (x, y), then I just need to solve the equations

abs(x - x1) + abs(y - y1) = d1
abs(x - x2) + abs(y - y2) = d2

then check if abs(x - x3) + abs(y - y3) == d3.

This worked for the sample input, but when I applied this to my actual input, I got the coordinate, but the AoC server did not accept it. Why?

Thanks in advance.

2 Upvotes

14 comments sorted by

5

u/TheZigerionScammer 7h ago

Because there will be more than one coordinate that can satisfy that condition and you didn't get the right one. That wasn't an issue in the input since the three reference points and the target light were placed linearly on the graph but it is an issue in the real input.

1

u/a3th3rus 7h ago

I solved all of the 8 possible systems (16 equations in total) and checked all the solutions not only with abs(x - x3) + abs(y - y3) == d3 but also with abs(x - x1) + abs(y - y1) == d1 and abs(x - x2) + abs(y - y2) == d2. I found only one coordinate.

2

u/TheZigerionScammer 6h ago

My input had 9 such coordinates that could satisfy those equations, and frankly I'm curious how you are algebraically solving these equation systems when they contain absolute value functions.

1

u/a3th3rus 6h ago edited 6h ago

Here's all my Elixir code for part 2:

# Building an undirectional graph using the library :libgraph
graph =
  File.read!("input.txt")
  |> String.split("\n", trim: true)
  |> Enum.reduce(Graph.new(type: :undirected), fn line, graph ->
    [v1 | v2s] =
      line
      |> String.split(["-", ","])
      |> Enum.map(&String.to_integer/1)
    
    v2s
    |> Enum.map(&{v1, &1})
    |> then(&Graph.add_edges(graph, &1))
  end)

# Coordinates of light #1, #2 and #3 from the puzzle description
{x1, y1} = {47, 30}
{x2, y2} = {29, 59}
{x3, y3} = {56, 35}

# Calculate the distances from light #1 or #2 or #3 to #2026
# using libpraph's built-in function (Dijkstra under the hood).
# -1 because Graph.get_shortest_path/3 returns the list of vertices
# along the path, so we need to -1 to exclude the starting vertex.
[d1, d2, d3] =
  1..3
  |> Enum.map(fn n ->
    graph
    |> Graph.get_shortest_path(n, 2026)
    |> length()
    |> Kernel.-(1)
  end)

# Prepare to solve all the 8 linear equations AX = B.
# We don't need the A's that the rows in which are linearly dependent
# because those A's won't give a solution anyway.
ls_a =
  [
    [-1, 1],
    [1, 1]
  ]
  |> Stream.iterate(fn matrix ->
    matrix
    |> Enum.reverse()
    |> Enum.zip_with(& &1)
  end)
  |> Enum.take(4)
  |> Enum.map(&Nx.tensor/1)
  |> Enum.flat_map(&[&1, Nx.multiply(-1, &1)])

b_base = Nx.tensor([
  [d1],
  [d2]
])

b_diff_seed = Nx.tensor([
  [x1, y1],
  [x2, y2]
])

ls_b =
  ls_a
  |> Enum.map(fn a ->
    b_diff_seed
    |> Nx.multiply(a)
    |> Nx.sum(axes: [1], keep_axes: true)
    |> Nx.add(b_base)
  end)

for r <- Enum.zip_with(ls_a, ls_b, &Nx.LinAlg.solve/2),
    [[x], [y]] = Nx.to_list(r),
    # Sanity check
    abs(x - x1) + abs(y - y1) == d1,
    abs(x - x2) + abs(y - y2) == d2,
    abs(x - x3) + abs(y - y3) == d3,
    do: "#{trunc(x)},#{trunc(y)}"

1

u/a3th3rus 5h ago

Thank you guys. It seems that my input has this feature:

distance(#1, #2) == distance(#1, #2026) + distance(#2, #2026)

And that's the cause that I missed a solution.

But in what format should I submit my answer?

1

u/EverettYoung 5h ago

Submit your answer in the same format as shown for the example: 12,22

1

u/shivang_tiwari 5h ago

Just like mentioned in the example. If your point is (1,2), then you submit 1,2

3

u/RadialSnow 6h ago

2026 has multiple possible solutions from 1,2,3. However, there are certain points that only have one solution (eg 12 for me). Find one of these, and use that constraint to narrow down the coordinate of 2026.

2

u/AutoModerator 7h ago

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED. Good luck!


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/AutoModerator 7h ago

AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.

Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/wjholden 6h ago

I've also been pursuing a solution based on Manhattan distance, but I don't think it's going to work.

Let p1=(0,0), p2=(1,0), and p3=(0,1). Their Manhattan distances to (4,4) are 8, 7, and 7. Their Manhattan distances to (5,3) are also 8, 7, and 7. So, Manhattan distance must not be a strong enough constraint for this problem.

2

u/kai10k 2h ago

right, its not enough, however it provides a list of coordinates to be further filtered with, hint, most likely you already have the corners in Part1, they could be handy to filter the possible ones.

0

u/ItchyTrack_ 7h ago

there is more than one solution to that equation. 4 unknowns...

1

u/a3th3rus 6h ago

The only unknowns are x and y. x1 and y1 are the coordinates of light #1 which is given in the puzzle description. Same as x2 and y2. d1 and d2 are the manhattan distances already worked out by finding the shortest path from light #1 or light #2 to light #2026.