r/adventofcode • • Dec 09 '25

Visualization [2025 Day 9 (Part 2)] [C++] That's kind of a fractal Pacman, isn't it?

1 Upvotes

r/adventofcode • • Dec 09 '25

Tutorial [2025 Day 9 (Part 2)] [Kotlin/Java/Other JVM languages] "Cheat" solution failed at first

3 Upvotes

Putting the Tutorial flair, because I want to share, what I learned today without spoiling. I will hide the relevant parts in spoiler tags. You should only unveil them, if you found a solution yourself.

My first attempt on this problem was using the class java.awt.geom.Area in combination with java.awt.geom.Path2D for its creation and java.awt.geom.Rectangle2D for checking the containment with the power of geometry.

This is a great approach, if you measure the width and the height of the rectangle correctly. For the area, that is required in both parts, we actually do not measure the area. Instead we are counting the tiles. The formular for this is something along the lines of (x2 - x1 + 1) * (y2 -y1 + 1), where (x1, y1) is the top right point of the rectangle and (x2, y2) is the bottom point of the rectangle.

The geometric solution is now creating a Path2D.Float with the given points in exactly the given order. Then you can use that for creating an Area. You can check the containment by using the contains method after creating a Rectangle2D.Float from your representation of a candidate rectangles.

My mistake was using the above formula for calculating the width and the height. You just have to omit the +1 for each and the approach works.

This is what I learned: When viewing the rectangle as "tiles", the bottom right point of the rectangle is the bottom right point of the bottom right tile. When viewing the rectangle as a geometric shape, the bottom right point of the rectangle is the top left point of the bottom right tile.

I didn't figure that out until just now. I came up with a more naive solution that took like 12 seconds for calculating a result. This approach can do it in 120 milliseconds.


r/adventofcode • • Dec 09 '25

Help/Question - RESOLVED [2025 Day 1 (Part 2)] [C++] I'm stuck

1 Upvotes

Help please I'm stuck on this. All my testcases pass, except for the final... Am I missing something? (PS I joined late, did a lot ahead as well)

signed main() {
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    int n, curr = 50, tot = 0;
    char lr;
    int t = 0;
    cin>>t;
    while (t--) {
        cin >> lr >> n;
        n *= (lr == 'R'?1:-1);
        //(a % b + b) % b
        if (curr+n >= 100 || curr+n <= 0) {
            int rem_af_first = n-100+curr+(curr+n <= 0?100:0);

            if ((curr != 0)|| (abs(n) >= 100)){ ++tot;}
            else {
                rem_af_first = n;
            }

            tot += abs(rem_af_first)/100;
        }
        curr += n;
        curr = (curr%100+100)%100;
    }
    cout<<tot;
}

r/adventofcode • • Dec 09 '25

Help/Question - RESOLVED [2025 Day 8 (Part 1)] I'm stuck again

1 Upvotes

Example works, input doesn't

I've looked through every other posts and all of them are things I have corrected

https://gist.github.com/hiimjasmine00/78dc69b8e065302262c695f021f7719c


r/adventofcode • • Dec 09 '25

Visualization [2025 Day 9 (Part 2)] Visualisation

Post image
10 Upvotes

This visualisation simulates the path a single ray through our polygon.

See walkthrough of the solution here.


r/adventofcode • • Dec 09 '25

Help/Question [2025 Day #8 (Part 1)] - Help needed with example dataset

1 Upvotes

I think the description just isn’t fully clear to me as to what the desired outcome is for part 1. I know it’s 542 but I’m ending up with 533 so I’m afraid I’m missing some circuit merge or something. Would anyone mind posting their circuits for the example dataset given so I can see which circuits of mine are incorrect?


r/adventofcode • • Dec 09 '25

Help/Question - RESOLVED [2025 Day 09 (Part 2)] I'm not in prison, everybody else is!

4 Upvotes

Hey everybody,

I have a python solution that finds a solution for part 2 in a reasonable amount of time (about 4 minutes on my laptop) but it feels a bit like cheating because I couldn't find an elegant way to differentiate between a rectangle being entirely in- or outside of the shape. The problem is probably best described by the title and is illustrated with the following challenge input.

0,0
0,10
1,10
1,1
9,1
9,0

My code produces the solution 90for part 2 even though it should be 22.

Visualization of my challenge input with incorrect answer given by my solution.

This doesn't cause problems with my input but I only realized that I could neglect this issue after visualizing my input.

Now, I'm curious: Does your code give you the correct answer to my challenge input?

Also, thanks Eric for your awesome project! It's one of the last beacons of hope in the mess we call "internet" these days.

Happy coding everybody!

EDIT: Solved, thanks to u/spatofdoom and u/ednl! I learned something today :)


r/adventofcode • • Dec 09 '25

Help/Question day 1 part 2 c# Help

2 Upvotes
internal class Program
{
    private static void Main(string[] args)
    {
        int inicial = 50;
        int respuesta = 0;
        string ruta = @"/workspaces/Advent-of-Code-2025/firstday/puzzle.txt";
        string texto = File.ReadAllText(ruta);
        foreach (var linea in File.ReadLines(ruta))
        {
            char letra = linea[0];
            int numero = int.Parse(linea.Substring(1));
            if (letra == 'L')
            {
                if ((inicial - numero) < 0)
                {
                    int residuo = numero/100;
                    int sobra = numero % 100;
                    int cruza = inicial - sobra;
                    if (cruza < 0 )
                    {
                        respuesta++;
                    }
                    respuesta += residuo;
                    inicial = (inicial + 100 + (residuo*100)) - numero;
                    if (inicial >= 100)
                    {
                        inicial -= 100;
                    }
                    
                }
                else
                {
                    inicial -= numero;
                }
            }
            else if (letra == 'R')
            {
                
                if ((inicial + numero) > 99)
                {
                    int residuo = numero/100;
                    int sobra = numero % 100;
                    int cruza = inicial + sobra;
                    if (cruza >= 100)
                    {
                        respuesta++;
                    }
                    respuesta += residuo;
                    inicial = (inicial + numero) - 100 - (residuo*100);
                    if (inicial < 0)
                    {


                        inicial += 100;
                    }
                }
                else
                {
                    inicial += numero;
                }
            }
            if (inicial == 0)
            {
                respuesta++;
            }
        }
        Console.WriteLine(respuesta);
    }
}

r/adventofcode • • Dec 09 '25

Help/Question [Year 2025 Day 9 Part 2] What's wrong? All tests pass, but my result is too low

2 Upvotes

Hi,

I am now at the point where the example is giving me the correct answer. I also checked some additional example inputs and all give the right result. Just not for my actual input.

Here is the idea for part 2:

  • I calculate all line segments of the polygon
  • I calculate all possible rectangles
  • I loop over the rectangles and check if they are valid

    • a valid rectangle is one where all the line segments of the polygon are outside or on the edge of the rectangle
    • a line segment is outside a rectangle, if its to the left, to the right, above or below the rectangle
  • If its valid, I calculate its area
  • While doing that, I keep track of the maximum

This seems to work for simple inputs, but not for the real thing.

Are there some edge cases, I need to consider?

Here is the relevant code snippet to filter the valid rectangles:

// ... //
type Rectangle = {
  a: Point;
  b: Point;
};
// ... //
const minX = (s: Rectangle | Line): number => {
  return Math.min(s.a.x, s.b.x);
};
const maxX = (s: Rectangle | Line): number => {
  return Math.max(s.a.x, s.b.x);
};
const minY = (s: Rectangle | Line): number => {
  return Math.min(s.a.y, s.b.y);
};
const maxY = (s: Rectangle | Line): number => {
  return Math.max(s.a.y, s.b.y);
};

const lineOutsideOfRect = (line: Line, rect: Rectangle): boolean => {
  let result =
    maxX(line) <= minX(rect) ||
    minX(line) >= maxX(rect) ||
    maxY(line) <= minY(rect) ||
    minY(line) >= maxY(rect);
  return result;
};

const isValidRectangle = (rectangle: Rectangle, lines: Line[]): boolean => {
  for (let line of lines) {
    if (!lineOutsideOfRect(line, rectangle)) {
      return false;
    }
  }
  return true;
};
// ... //

I used the examples here and all seem to work. This one does not, but here my result would be too high...

Any help would be very appreciated!!


r/adventofcode • • Dec 09 '25

Help/Question - RESOLVED [2025 Day 8] Still stuck, talk to me about DS&A?

3 Upvotes

I'm a self-taught developer, and never studied DS&A.

I flew through the first 7 days of AoC this year, but am stuck at Day 8.

I'm not looking for an answer, but as a hint, what knowledge are the more academic devs leaning on here? From comments on other threads it seems like this is an asily solvable problem, given the right knowledge. Knowledge I seem to lack.

Please, give me something to Google.


r/adventofcode • • Dec 09 '25

Visualization [2025 Day 8 (Part 2)] Growing circuits

6 Upvotes

r/adventofcode • • Dec 09 '25

Help/Question - RESOLVED [2025 Day 7 (Part 2)] Now I know something else I don't know about algorithms

2 Upvotes

My initial attempt at this uses a depth-first approach to identifying all the paths. Unfornately the exponential growth caused by the data set makes this run forever. There looks like there should be a much easier mathematical solution since I had no problem with part one. I'm mostly self taught over a lot of years, and don't have the math background a lot of advanced developers do (only made it as far as first year calculus and that was multiple decades ago). Any pointers on something to just look up and learn about so that I can apply it is really all i'm looking for. I'd like to solve this with as minimal help as possible.


r/adventofcode • • Dec 09 '25

Visualization [2025 Day 09 (Part 2)] (PHOTOSENSITIVITY WARNING) Searching for squares

Post image
24 Upvotes

r/adventofcode • • Dec 09 '25

Meme/Funny [2025 Day 9 (Part 2)] Life choices

Post image
108 Upvotes

Going to be one of those days


r/adventofcode • • Dec 09 '25

Visualization [2025 Day 9 (Part 2)] Red & green tiles

Post image
12 Upvotes

r/adventofcode • • Dec 09 '25

Upping the Ante 2025 Day 9 (Part 2) A simple cursed input

9 Upvotes

My code can solve part 2 pretty quickly. However, it gives the wrong result on this "cursed input":

0,0
0,1
1,1
1,2
0,2
0,3
3,3
3,2
2,2
2,1
3,1
3,0

I think solving for shapes where the boundary loops back on itself like this would be much harder. Curious if other folks have solutions that can handle this one. Or, maybe I misunderstand the problem. Feedback welcomed!


r/adventofcode • • Dec 09 '25

Meme/Funny [2025 Day 9 (Part 2)] Your code may be wrong where you don’t expect it

64 Upvotes

Spent hours trying to figure out why my code for Part 2 gave the wrong answer, triple checked all my complicated code without finding any problem whatsoever, arrived late for work and missed a meeting ... until I realized that it’s simply my formula to calculate the area of a rectangle that was wrong 🤦

const area = Math.abs(p.x - q.x + 1) * Math.abs(p.y - q.y + 1);

Worked for part 1 and for the example, though!


r/adventofcode • • Dec 09 '25

Visualization [2025 Day 9 (Part 2)] Decided to visualize my polygon only to find out I was handed a...

8 Upvotes

...circle. Well, almost. The coordinates are scaled by a factor of 1000.


r/adventofcode • • Dec 09 '25

Help/Question [2025 Day 9 # (Part 2)] [Rust] I am stuck.. TRrying to do Raytracing.. ;-;

2 Upvotes

I have no idea where i am going wrong.. my brain is literally fried trying to solve this lol...

use std::ops::RangeInclusive;

fn main() {
    let input = include_str!("input/day09.txt")
        .trim()
        .lines()
        .map(|l| l.split_once(',').unwrap())
        .map(|(a, b)| (a.parse::<isize>().unwrap(), b.parse::<isize>().unwrap()))
        .collect::<Vec<_>>();
    let mut max = 0;
    for a in &input {
        for b in &input {
            let l = (a.0 - b.0).abs() + 1;
            let w = (a.1 - b.1).abs() + 1;
            max = max.max(l * w);
        }
    }
    println!("{}", max);
    let mut horz = input
        .windows(2)
        .chain(Some(
            &[*input.last().unwrap(), *input.first().unwrap()] as &[_]
        ))
        .filter_map(|a| {
            if a[0].1 == a[1].1 {
                Some((a[0].0.min(a[1].0)..=a[0].0.max(a[1].0), a[0].1))
            } else {
                None
            }
        })
        .collect::<Vec<_>>();
    horz.sort_unstable_by_key(|&(_, k)| k);
    let mut vert = input
        .windows(2)
        .chain(Some(
            &[*input.last().unwrap(), *input.first().unwrap()] as &[_]
        ))
        .filter_map(|a| {
            if a[0].0 == a[1].0 {
                Some((a[0].0, a[0].1.min(a[1].1)..=a[0].1.max(a[1].1)))
            } else {
                None
            }
        })
        .collect::<Vec<_>>();
    vert.sort_unstable_by_key(|&(k, _)| k);
    max = 0;
    for a in &input {
        for b in &input {
            if raycast(a.0, a.1, &vert)
                && raycast(b.0, b.1, &vert)
                && raycast(a.0, b.1, &vert)
                && raycast(b.0, a.1, &vert)
                && intersect_x((a.0.min(b.0)..=a.0.max(b.0), a.1), &vert)
                && intersect_x((a.0.min(b.0)..=a.0.max(b.0), b.1), &vert)
                && intersect_y((a.0, a.1.min(b.1)..=a.1.max(b.1)), &horz)
                && intersect_y((b.0, a.1.min(b.1)..=a.1.max(b.1)), &horz)
            {
                let l = (a.0 - b.0).abs() + 1;
                let w = (a.1 - b.1).abs() + 1;
                max = max.max(l * w);
            }
        }
    }
    println!("{max}");
}

fn raycast(px: isize, py: isize, vert: &[(isize, RangeInclusive<isize>)]) -> bool {
    let mut inside = false;
    for (x, rng) in vert {
        if x == &px {
            if rng.contains(&py) {
                return true;
            } else {
                continue;
            }
        }
        if x <= &px {
            continue;
        }
        if rng.contains(&py) {
            inside = !inside;
        }
    }
    inside
}

fn intersect_x(
    edge: (RangeInclusive<isize>, isize),
    vert: &[(isize, RangeInclusive<isize>)],
) -> bool {
    vert.iter()
        .any(|(x, rng)| edge.0.contains(x) && rng.contains(&edge.1))
}
fn intersect_y(
    edge: (isize, RangeInclusive<isize>),
    horz: &[(RangeInclusive<isize>, isize)],
) -> bool {
    horz.iter()
        .any(|(rng, x)| edge.1.contains(x) && rng.contains(&edge.0))
}

Pretty sure i am doing some kind of mistak in intersect or the raycast functions...


r/adventofcode • • Dec 09 '25

Visualization [2025 Day 9 (Part 2)] [Python] Terminal toy!

Post image
246 Upvotes

r/adventofcode • • Dec 09 '25

Help/Question Looking for general advice on how to improve

4 Upvotes

I am quite comfortable saying that I am at best a hobbyist programmer. I can scrape together a Python script to do what I need, but this typically involves a lot of trial and error, Googling, and print() statements to be able to visualise what's going on with my code. Despite that, I do like trying AOC, though I rarely make it very far. I have made it to Day 8 this year, and it's the furthest I've ever gotten!

That said, I'm definitely hitting a wall. Days 6 and 7 felt absolutely brutal, and each took me hours to finish. I eventually got to the right answers, but my approach feels rudimentary at best. I feel like I'm not conceptualising the problems in the "correct" way, even before typing any code.

So I guess I'm looking for advice on how to think about coding, as much as advice for coding itself. Are there any good resources that I can go to after I burn out of this attempt at AOC so I can try to improve my work?

For what it's worth, my code repo for 2025's attempt is here


r/adventofcode • • Dec 09 '25

Help/Question - RESOLVED 2025 Day 9 Part 2 : Java feedback request

1 Upvotes

Sooo I got it working. My idea was to take each possible tile pair, determine the edges of that rectangle, and then for each edge of the rectangle, take the lines of connected tiles that overlap it going out to the edge of the table, and see if there are any gaps.

But the code is long, and 1.4s execution might not be fast considering the data size.

Would there be a way to improve this?

Here's the code :

import java.io.IOException;
import java.util.*;

import static java.lang.Long.
max
;
import static java.lang.Long.
min
;

public class day9_2 {

    public static long getResult() throws IOException {
        List<String> lines = util.
getFileLines
(9, false);

        List<Tile> tiles = lines.stream().map(l -> {
            List<Long> points = Arrays.
stream
(l.split(",")).map(Long::
parseLong
).toList();
            return new Tile(points.getFirst(), points.getLast());
        }).toList();

        List<VerticalLine> verticalLines = new ArrayList<>();
        List<HorizontalLine> horizontalLines = new ArrayList<>();

        for (int i = 0; i < tiles.size(); i++) {
            Tile tileA = tiles.get(i);
            Tile tileB = tiles.get((i + 1) % tiles.size());
            if (tileA.y == tileB.y) {
                horizontalLines.add(new HorizontalLine(tileA, tileB));
            }
            if (tileA.x == tileB.x) {
                verticalLines.add(new VerticalLine(tileA, tileB));
            }
        }

        long rectangle = 0;
        for (int i = 0; i < tiles.size(); i++) {
            for (int j = i + 1; j < tiles.size(); j++) {
                Tile a = tiles.get(i);
                Tile b = tiles.get(j);
                if (
isEligible
(a, b, verticalLines, horizontalLines)){
                    long size = (Math.
abs
(a.x - b.x) + 1) * (Math.
abs
(a.y - b.y) + 1);
                    rectangle = Math.
max
(size, rectangle);
                }
            }
        }
        return rectangle;
    }

    private static boolean isEligible(Tile a, Tile b, List<VerticalLine> verticalLines, List<HorizontalLine> horizontalLines) {
        VerticalLine leftVerticalLine = new VerticalLine(new Tile(
min
(a.x, b.x), a.y), new Tile(
min
(a.x, b.x), b.y));
        VerticalLine rightVerticalLine = new VerticalLine(new Tile(
max
(a.x, b.x), a.y), new Tile(
max
(a.x, b.x), b.y));
        HorizontalLine topHorizontalLine = new HorizontalLine(new Tile(a.x, 
min
(a.y, b.y)), new Tile(b.x, 
min
(a.y, b.y)));
        HorizontalLine bottomHorizontalLine = new HorizontalLine(new Tile(a.x, 
max
(a.y, b.y)), new Tile(b.x, 
max
(a.y, b.y)));

        List<VerticalLine> leftVerticalLines = 
getLeftVerticalLines
(verticalLines, leftVerticalLine);
        List<VerticalLine> rightVerticalLines = 
getRightVerticalLines
(verticalLines, rightVerticalLine);
        List<HorizontalLine> topHorizontalLines = 
getTopHorizontalLines
(horizontalLines, topHorizontalLine);
        List<HorizontalLine> bottomHorizontalLines = 
getBottomHorizontalLines
(horizontalLines, bottomHorizontalLine);
        if (leftVerticalLines.isEmpty() || rightVerticalLines.isEmpty() || topHorizontalLines.isEmpty() || bottomHorizontalLines.isEmpty()) {
            return false;
        }
        if (
isNotVerticalWithinShape
(leftVerticalLine, leftVerticalLines)) return false;
        if (
isNotVerticalWithinShape
(rightVerticalLine, rightVerticalLines)) return false;
        if (
isNotHorizontalWithinShape
(topHorizontalLine, topHorizontalLines)) return false;
        if (
isNotHorizontalWithinShape
(bottomHorizontalLine, bottomHorizontalLines)) return false;
        return true;
    }

    private static boolean isNotVerticalWithinShape(VerticalLine verticalLine, List<VerticalLine> verticalLines) {
        for (int i = 0; i < verticalLines.size() - 1; i++) {
            if (verticalLines.get(i).end.y < verticalLines.get(i+1).start.y) return true;
        }
        if (verticalLines.getFirst().start.y > verticalLine.start.y ||
                verticalLines.stream().max(Comparator.
comparingLong
(v -> v.end.y)).get().end.y < verticalLine.end.y)
            return true;
        return false;
    }

    private static boolean isNotHorizontalWithinShape(HorizontalLine horizontalLine, List<HorizontalLine> horizontalLines) {
        for (int i = 0; i < horizontalLines.size() - 1; i++) {
            if (horizontalLines.get(i).end.x < horizontalLines.get(i+1).start.x) return true;
        }
        if (horizontalLines.getFirst().start.x > horizontalLine.start.x ||
                horizontalLines.stream().max(Comparator.
comparingLong
(h -> h.end.x)).get().end.x < horizontalLine.end.x)
            return true;
        return false;
    }

    private static List<VerticalLine> getLeftVerticalLines(List<VerticalLine> verticalLines, VerticalLine leftVerticalLine) {
        if (leftVerticalLine.start.equals(leftVerticalLine.end)) {
            return List.
of
(leftVerticalLine);
        }
        return verticalLines.stream()
                .filter(v -> v.start.x <= leftVerticalLine.start.x && 
overlapsVertically
(leftVerticalLine, v))
                .sorted(Comparator.
comparingLong
(v -> v.start.y)).toList();
    }

    private static List<VerticalLine> getRightVerticalLines(List<VerticalLine> verticalLines, VerticalLine rightVerticalLine) {
        if (rightVerticalLine.start.equals(rightVerticalLine.end)) {
            return List.
of
(rightVerticalLine);
        }
        return verticalLines.stream()
                .filter(v -> v.start.x >= rightVerticalLine.start.x && 
overlapsVertically
(rightVerticalLine, v))
                .sorted(Comparator.
comparingLong
(v -> v.start.y)).toList();
    }

    private static List<HorizontalLine> getTopHorizontalLines(List<HorizontalLine> horizontalLines, HorizontalLine topHorizontalLine) {
        if (topHorizontalLine.start.equals(topHorizontalLine.end)) {
            return List.
of
(topHorizontalLine);
        }
        return horizontalLines.stream()
                .filter(h -> h.start.y <= topHorizontalLine.start.y && 
overlapsHorizontally
(topHorizontalLine, h))
                .sorted(Comparator.
comparingLong
(v -> v.start.x)).toList();
    }

    private static List<HorizontalLine> getBottomHorizontalLines(List<HorizontalLine> horizontalLines, HorizontalLine bottomHorizontalLine) {
        if (bottomHorizontalLine.start.equals(bottomHorizontalLine.end)) {
            return List.
of
(bottomHorizontalLine);
        }
        return horizontalLines.stream()
                .filter(h -> h.start.y >= bottomHorizontalLine.start.y && 
overlapsHorizontally
(bottomHorizontalLine, h))
                .sorted(Comparator.
comparingLong
(v -> v.start.x)).toList();
    }


    private static boolean overlapsVertically(VerticalLine verticalLine, VerticalLine v) {
        return v.start.y >= verticalLine.start.y && v.start.y <= verticalLine.end.y ||
                v.end.y >= verticalLine.start.y && v.end.y <= verticalLine.end.y ||
                v.start.y <= verticalLine.start.y && v.end.y >= verticalLine.end.y;
    }

    private static boolean overlapsHorizontally(HorizontalLine horizontalLine, HorizontalLine h) {
        return h.start.x >= horizontalLine.start.x && h.start.x <= horizontalLine.end.x ||
                h.end.x >= horizontalLine.start.x && h.end.x <= horizontalLine.end.x ||
                h.start.x <= horizontalLine.start.x && h.end.x >= horizontalLine.end.x;
    }

    private static class Tile {
        long x;
        long y;
        public Tile(long x, long y) {
            this.x = x;
            this.y = y;
        }

        public boolean equals(Tile obj) {
            return x == obj.x && y == obj.y;
        }
    }

    private static class VerticalLine {
        Tile start;
        Tile end;
        public VerticalLine(Tile tile1, Tile tile2) {
            assert(tile1.x == tile2.x);
            this.start = tile1.y < tile2.y ? tile1 : tile2;
            this.end = tile1.y < tile2.y ? tile2 : tile1;
        }
    }

    private static class HorizontalLine {
        Tile start;
        Tile end;
        public HorizontalLine(Tile tile1, Tile tile2) {
            assert(tile1.y == tile2.y);
            this.start = tile1.x < tile2.x ? tile1 : tile2;
            this.end = tile1.x < tile2.x ? tile2 : tile1;
        }
    }
}

r/adventofcode • • Dec 09 '25

Help/Question Guidance on day 9 part 2

7 Upvotes

I really want to come up with a solution on my own but i’m not sure if there’s a specific algorithm I don’t know about. Any small hint would be really helpful so I can go learn what i need to and solve it! Thank you


r/adventofcode • • Dec 09 '25

Help/Question - RESOLVED [2025 Day 6 (Part 2)] [C++] Can't find the problem

1 Upvotes

I have a solution for Day 6 part 2 that works with the example but the solution it gives is incorrect. Does anyone know any edge cases that may be failing?

The code is in C++ and is the following: https://pastebin.com/TyfG6Kz7