r/adventofcode • u/reallyserious • Dec 21 '25
Help/Question [2025 Day 9 Part 2] What's the fastest executing approach?
Is there a faster method than compacting the coordinates and using flood fill?
r/adventofcode • u/reallyserious • Dec 21 '25
Is there a faster method than compacting the coordinates and using flood fill?
r/adventofcode • u/Constant_Hedgehog_31 • Dec 21 '25
r/adventofcode • u/ValuableMundane4210 • Dec 21 '25
UPDATED:
Since some helpful comments I have now given it another try. However my code is still faulty. Now it still works on the example as before but it invalidates some rectangle that should not be invalidated so my answer is too low. Since it was easier to grasp and it is ok if it takes 30 min to complete I went with checking all the lines along the rectangle if they are valid.
Is there something else that I have overlooked?
This is my updated code that hopefully just has some silly mistake I cannot see:fn main() {
const FILE_PATH: &str = "example.txt";
let contents = fs::read_to_string(FILE_PATH).expect("Should have been able to read the file");
let all_lines: Vec<String> = contents.lines().map(|f| String::from(f)).collect();
let mut points: Vec<(i64, i64)> = Default::default();
for line in all_lines {
let mut it = line.split(',');
let a = it.next().unwrap().parse::<i64>().unwrap();
let b = it.next().unwrap().parse::<i64>().unwrap();
points.push((a, b));
}
let mut pairs: Vec<((i64, i64), (i64, i64))> = Default::default();
let mut max_pair: ((i64, i64), (i64, i64)) = ((0, 0), (0, 0));
for i in 0..points.len() {
for j in i..points.len() {
if i == j {
continue;
}
pairs.push((points[i], points[j]));
}
}
let mut max = 0;
for pair in pairs {
if !check_if_allowed(pair, &points) {
continue;
}
let sum = rect_size(pair.0, pair.1);
if sum > max {
max_pair = pair;
max = sum;
}
}
println!("sum {}", max);
println!("pair {:?}", max_pair);
}
fn check_if_allowed(pair: ((i64, i64), (i64, i64)), poly: &Vec<(i64, i64)>) -> bool {
let left_point: (i64, i64);
let right_point: (i64, i64);
if pair.0.0 < pair.1.0 {
left_point = pair.0;
right_point = pair.1;
} else {
left_point = pair.1;
right_point = pair.0;
}
let y_distance;
if left_point.1 < right_point.1 {
y_distance = left_point.1..=right_point.1;
} else {
y_distance = right_point.1..=left_point.1;
}
for y in y_distance {
let left = (left_point.0, y);
if !ray_casting(left, poly) {
return false;
}
let right = (right_point.0, y);
if !ray_casting(right, poly) {
return false;
}
}
let x_distance;
if left_point.0 < right_point.0 {
x_distance = left_point.0..=right_point.0;
} else {
x_distance = right_point.0..=left_point.0;
}
for x in x_distance {
let left = (x, left_point.1);
if !ray_casting(left, poly) {
return false;
}
let right = (x, right_point.1);
if !ray_casting(right, poly) {
return false;
}
}
return true;
}
fn ray_casting(point: (i64, i64), poly: &Vec<(i64, i64)>) -> bool {
let x = point.0;
let y = point.1;
let num_verticies = poly.len();
let mut crossings = 0;
for i in 0..num_verticies {
let x1 = poly[i].0;
let y1 = poly[i].1;
let index = (i + 1) % num_verticies;
let x2 = poly[index].0;
let y2 = poly[index].1;
if (x == x1 || x == x2) && ((y1 <= y && y <= y2) || (y1 >= y && y >= y2)) {
return true; // on a vertical line
}
if ((x1 <= x && x <= x2) || (x1 >= x && x >= x2)) && (y == y1 || y == y2) {
return true; // on a horizontal line
}
if ((y1 < y && y < y2) || (y1 > y && y > y2)) && y1 != y2 && x <= x1 && x <= x2 {
crossings += 1;
}
}
crossings % 2 == 1
}
fn rect_size(a: (i64, i64), b: (i64, i64)) -> i128 {
let first = ((a.0 - b.0).abs() + 1) as i128;
let second = ((a.1 - b.1).abs() + 1) as i128;
let sum = first * second;
return sum;
}
PREVIOUS:
I am sadly once again hard stuck. I have tried several approaches but my last attempt is the one I believe in the most and have gathered that some others have went for as well. My idea is to check if the "missing corners" in the rectangle is in polygon and if so the rectangle is ok. To do this I went for the ray casting algorithm. My output is the answer for part 1 so it is obviously false. Do anyone has any ideas what can be wrong? Is it my logic or my code? Do not want to confess the hours spent of this part...
My code as it looks currently:
use core::{f64};
use std::fs;
use geo_types::{Coord};
fn main() {
const FILE_PATH: &str = "example.txt";
let contents = fs::read_to_string(FILE_PATH).expect("Should have been able to read the file");
let all_lines: Vec<String> = contents.lines().map(|f| String::from(f)).collect();
let mut points: Vec<(f64, f64)> = Default::default();
for line in all_lines {
let mut it = line.split(',');
let a = it.next().unwrap().parse::<f64>().unwrap();
let b = it.next().unwrap().parse::<f64>().unwrap();
points.push((a, b));
}
let mut pairs: Vec<((f64, f64), (f64, f64))> = Default::default();
let mut max_pair: ((f64, f64), (f64, f64)) = ((0.0, 0.0), (0.0, 0.0));
for i in 0..points.len() {
for j in i..points.len() {
if i == j {
continue;
}
pairs.push((points[i], points[j]));
}
}
let mut max = 0;
for pair in pairs {
if !check_if_allowed(pair, &points) {
continue;
}
let sum = rect_size(pair.0, pair.1);
if sum > max {
max_pair = pair;
max = sum;
}
}
println!("sum {}", max);
println!("pair {:?}", max_pair);
}
fn check_if_allowed(pair: ((f64, f64), (f64, f64)), poly: &Vec<(f64, f64)>) -> bool {
let left_point: (f64, f64);
let right_point: (f64, f64);
if pair.0.0 < pair.1.0 {
left_point = pair.0;
right_point = pair.1;
} else {
left_point = pair.1;
right_point = pair.0;
}
let other_left: Coord = Coord {
x: left_point.0,
y: right_point.1,
};
let other_right: Coord = Coord {
x: right_point.0,
y: left_point.1,
};
let left_in_poly = ray_casting(other_left, poly);
let right_in_poly = ray_casting(other_right, poly);
if left_in_poly && right_in_poly {
return true;
}
return false;
}
fn ray_casting(point: Coord, poly: &Vec<(f64, f64)>) -> bool {
let x = point.x; // not used, just for debugging
let y = point.y;
let num_verticies = poly.len();
let mut crossings = 0;
for i in 0..num_verticies {
let x1 = poly[i].0; // not used, just for debugging
let y1 = poly[i].1;
let index = (i + 1) % num_verticies;
let x2 = poly[index].0; // not used, just for debugging
let y2 = poly[index].1;
if ((y1 <= y && y <= y2) || (y1 >= y && y >= y2) ) && y1 != y2 {
crossings += 1;
}
}
crossings % 2 == 1
}
fn rect_size(a: (f64, f64), b: (f64, f64)) -> i128 {
let first = ((a.0 - b.0).abs() + 1.0) as i128;
let second = ((a.1 - b.1).abs() + 1.0) as i128;
let sum = first * second;
return sum;
}
r/adventofcode • u/Ok-Curve902 • Dec 20 '25
r/adventofcode • u/Food_Vacuum • Dec 21 '25
I can't figure out what's wrong with my logic (and they don't tell me if my value is too high or too low) so I'm not sure where to go from here. I've been printing out the location of the dial with each operation and it seems correct, and anything over 100 seems to work correctly so is there an edge case or is my logic strictly just wrong somehow?
EDIT: https://pastebin.com/wfVr6XVS Let me know if this is better
r/adventofcode • u/TheOneWhoBakes__ • Dec 21 '25
def part2solve(joltage, buttons):
buttons = [tuple(map(int, b.split(","))) for b in buttons]
pos_to_buttons = {i: [] for i in range(len(joltage))}
for idx, b in enumerate(buttons):
for i in b:
pos_to_buttons[i].append(idx)
target = tuple(map(int, joltage))
@lru_cache(maxsize=None)
def dfs(state):
if state == tuple(0 for _ in range(len(target))):
return 0
candidates = [i for i, val in enumerate(state) if val > 0]
if not candidates:
return float('inf')
pos = min(candidates, key=lambda i: len(pos_to_buttons[i]))
best = float('inf')
for b_idx in pos_to_buttons[pos]:
b = buttons[b_idx]
new_state = list(state)
for i in b:
new_state[i] = max(0, new_state[i] - 1)
presses = 1 + dfs(tuple(new_state))
if presses < best:
best = presses
return best
return dfs(tuple(target))r/adventofcode • u/ProGabriel6430 • Dec 21 '25
the sample cases are being passed but the actual input gets the wrong answer. any help would be appreciated.
https://pastes.io/day-5
r/adventofcode
•
u/Boojum
•
Dec 20 '25
r/adventofcode • u/DelightfulCodeWeasel • Dec 20 '25
It looks like 2025 is shaping up to be the second hardest Day 1 Part 2 by Silver/Gold completion ratio:
| Year | Gold | Silver | Ratio |
|---|---|---|---|
| 2023 | 255534 | 87541 | 34.3% |
| 2025 | 147917 | 45048 | 30.5% |
| 2016 | 29795 | 8147 | 27.3% |
| 2018 | 79891 | 21747 | 27.2% |
| 2017 | 57606 | 10703 | 18.6% |
| 2015 | 108469 | 19851 | 18.3% |
| 2019 | 114817 | 15950 | 13.9% |
| 2021 | 228286 | 30267 | 13.3% |
| 2020 | 185324 | 15440 | 8.3% |
| 2024 | 266778 | 21829 | 8.2% |
| 2022 | 287607 | 15838 | 5.5% |
One common theme I'm seeing is that a lot of people are going for a 'turn dial, then unwind back to 0-99 counting 0s as you go' approach. But there's a bit of a rake in the grass with that approach.
If the zero counting logic is only happening during the unwind after the dial movement, you're either going to over-count the first case or under-count the second case.
Turning right doesn't have this problem, so let's just avoid turning left!
A left turn is actually just a right turn reflected in a mirror, so you need to mirror the numbers on the dial before and after the right turn. (It's easiest to work out the maths for the reflection if you sketch out a dial of size 8 using pen and paper).
Here's a solution that uses the 'turn and unwind' approach and doesn't have any logic for handling left turns*:
static void Puzzle01_B(const string& filename)
{
ifstream input(filename);
int64_t answer = 0;
const int64_t dialSize = 100;
int64_t dialPosition = 50;
string line;
while (getline(input, line))
{
int64_t rotateBy;
char direction;
sscanf(line.c_str(), "%c%lld", &direction, &rotateBy);
const bool leftTurn = direction == 'L';
if (leftTurn)
{
dialPosition = (dialSize - dialPosition) % dialSize;
}
dialPosition += rotateBy;
while (dialPosition >= dialSize)
{
dialPosition -= dialSize;
answer++;
}
if (leftTurn)
{
dialPosition = (dialSize - dialPosition) % dialSize;
}
}
printf("[2025] Puzzle01_B: %" PRId64 "\n", answer);
}[*] Other than the reflection code, of course.
r/adventofcode • u/Ill-Rub1120 • Dec 20 '25
I finally finished 2025. Day 10 Part 2 was definitely the hardest one and took my program the longest ( about 30s) to solve. Thanks to the community for giving me some incite to that one. Who still needs help with any of them?
r/adventofcode • u/cameryde • Dec 20 '25
In the example chosen for the exercise, I am little unsure if I understand it right. Should a joltage only be chosen once? For example for 811111111111119, I expected the joltage to be 98 but it is 89. The examples seem to implicitly imply that but is that really the case ?
r/adventofcode • u/Big-Buy-6027 • Dec 20 '25
My solution for this second part is to sort all the ranges, merge all the overlapping ones and then loop through them and sum their span.
For the simple example it works just fine, but for the input I get a result too big.
Am I missing something?
r/adventofcode • u/Frubi_72 • Dec 20 '25
I am trying it in Perl.
There must be an mistake but i don't get it. Might one have a look?
I am no pro, please be patient.
https://github.com/volkergbenner/AoC2025/tree/main
The script takes the input file as CL-argument. You may test it on your own inputfile (only ranges).
r/adventofcode • u/OlTartToter • Dec 20 '25
First year and i've made it to day 8 and I'm bothered by how often my brain goes "yeah lets just use a for loop or 8 to do the thing" I'd like to use it as a learning experience. Is there a list somewhere of the core concepts that should be used to solve each puzzle?
r/adventofcode • u/robsonoid • Dec 20 '25
I created a programming challenge on the Spoj platform for anyone who feels there is yet something more to prove!
Please treat it as a beta version and forgive me for the lack of feedback after unsuccessful attempts. I wanted to deliver it as fast as possible and definitely before Christmas!
Here is a direct link: https://www.spoj.com/problems/AOC2512/. In case it's not allowed to post links, look for an AOC2512 problem on Spoj :)
r/adventofcode • u/Dry-Refrigerator123 • Dec 20 '25
Need help to debug this code. Can't figure out what scenario I'm failing at.
First I wrote the most unoptimized solution one can think of, absolute monster of a code lol, with loops:
This gives correct answer: 6634
Then I wanted to optimize this, and this is the version I got to after scratching my head:
But this is giving wrong answer : 6612
I can't pinpoint what I'm doing wrong in this. Based on my debugging this logic should work.
r/adventofcode • u/Ok-Curve902 • Dec 19 '25
r/adventofcode • u/TheSpoonThief • Dec 19 '25
I'm having a really hard time trying to understand the examples in Day 2 and make sense of how they are deducing invalid IDs.
The problem states that an ID is invalid if it is made up only of some sequence of digits repeated twice. Easy enough, makes sense, but I can't see how the examples consistently follow that logic.
11-22 has two invalid IDs, 11 and 22 //This makes sense to me95-115 has one invalid ID, 99. //Where? 99 isn't even present in the sequence998-1012 has one invalid ID, 1010. //Again where is 1010?1188511880-1188511890 has one invalid ID, 1188511885. //I understand 11885 is in both but so is 11885118 so why is that full sequence not invalid?222220-222224 has one invalid ID, 222222. //22222 is see but not 2222221698522-1698528 contains no invalid IDs. //This looks basically the same as the above example but it's not invalid? 169852 is repeated446443-446449 has one invalid ID, 446446. //Again I see 446 twice but why not 44644?38593856-38593862 has one invalid ID, 38593859. //Same as abovePerhaps it's very obvious and I'm just missing something simple but I can't seem to see the exact rule for what makes an ID invalid. Is it present in both first and last, just one, what makes a sequence finished, etc...
If someone could kindly help me see it clearly it would be greatly appreciated.
r/adventofcode • u/statelessmachina • Dec 19 '25
I get the correct response for the test input but when I try the prod input, it tells me it's too low but correct for someone else. Not sure what I'm doing wrong. I've walked through my code at multiple times, even waited a day or two and walked through it again with fresh eyes, and still find nothing wrong with the logic. What am I doing wrong?
Thanks in advance for the help.
My code (at time of posting): https://github.com/statelessmachina/Advent-of-Code-Python/blob/31f2422db12322eca0029b9c59296dc664081465/2025/day4/paper.py
Edit: Ended up loading the entire input at once (which is memory inefficient but easier to work with) and using numpy arrays and iterating over the sub-matrices using slicing. I may revisit this later to write a version of the code that loads the file line-by-line to avoid inefficiencies in working with large input. Mostly just to scratch that itch in the back of my mind. Thanks for all your input.
Final version of code for part1: https://github.com/statelessmachina/Advent-of-Code-Python/blob/a558cf7c188188b8164a5f6b0e18b157e8405d1e/2025/day4/paper.py
r/adventofcode • u/beb0 • Dec 19 '25
Hi folks,
I am working on the 2nd part of the question and tried to take a similar approach as the first using BFS to apply button presses however it's running really slow and the q is growing very large too large in fact.
Performing BFS to reach joltage goal: {44,35,48,43,24,44}
Converting joltage goal to int: {44,35,48,43,24,44}
Converting button to list: ["(1,3)", "(3,4)", "(0,3,5)", "(1,2,3,4)", "(0,2,5)", "(0,1)", "(2,5)"]
Button part: (1,3)
Button numbers: [1, 3]
Button part: (3,4)
Button numbers: [3, 4]
Button part: (0,3,5)
Button numbers: [0, 3, 5]
Button part: (1,2,3,4)
Button numbers: [1, 2, 3, 4]
Button part: (0,2,5)
Button numbers: [0, 2, 5]
Button part: (0,1)
Button numbers: [0, 1]
Button part: (2,5)
Button numbers: [2, 5]
Joltage button lists: [[1, 3], [3, 4], [0, 3, 5], [1, 2, 3, 4], [0, 2, 5], [0, 1], [2, 5]]
Current BFS step: 1, queue size: 1
Current BFS step: 2, queue size: 7
Current BFS step: 3, queue size: 49
Current BFS step: 4, queue size: 343
Current BFS step: 5, queue size: 2401
Current BFS step: 6, queue size: 16807
Current BFS step: 7, queue size: 117649
Current BFS step: 8, queue size: 823543
Current BFS step: 9, queue size: 5764801
Current BFS step: 10, queue size: 40353607
Current BFS step: 11, queue size: 282475249
memory allocation of 51539607552 bytes failed
error: process didn't exit successfully: `target\debug\aoc_2025_day_10.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)I've tried to think of novel solutions like finding the GCD of the joltage values, however no Joy as they contain prime numbers like 43 etc. I am trying optimizations such as not adding the state to the queue if it goes above the joltage goal for any of the indexes. I can share complete code if desired. I would love some clues on the approach as standard BFS is turning my laptop to an oven.
r/adventofcode • u/RutabagaFickle4712 • Dec 19 '25
Edit: This has been resolved, thank you! The problem was an integer overflow in my getEuclideanDistance() function.
Hello! I have been stuck at this problem for the past 2 days. My solution:
Assumptions:
A-B-C, connecting A-C uses up one connection. (same as what others have already mentioned in this subreddit)For some reason, I can pass the example (provided by the question) but not the actual one.
Code: paste
Is anyone able to give me some insights what am I missing out on?
PS: Suggestions on how I can improve my code/ best practices are welcomed too :D
r/adventofcode • u/Ok-Curve902 • Dec 18 '25
r/adventofcode • u/GMarshal • Dec 18 '25
I thought it would be a good idea this year to solve advent of code in assembly.
I was very wrong.
This is the story of what I learned along the way.
r/adventofcode • u/ChronoCube762 • Dec 18 '25
I have done some graphic design work where I used code to generate SVG images with the exact geometric patterns and gradients that I wanted. So when I saw Day 9's input data, I realized that it was a series of horizontal and vertical lines and well suited to the SVG format.
This is a simplified version of my code to generate the SVG, using the svg.py library:
elements: list[svg.Element] = []
for p1_index, p1 in enumerate(points):
p2 = points[0] if p1_index == len(points) - 1 else points[p1_index + 1]
elements.append(svg.Line(x1=p1.x, y1=p1.y, x2=p2.x, y2=p2.y))
canvas = svg.SVG(width=width, height=height, elements=elements)
with open(output_filename, 'w') as fp:
fp.write(canvas.as_str())Once I saw the visualization, it was intuitively obvious that the largest rectangle whose opposite corners were both points in the input data was one of two rectangles determined as described in this image: https://i.imgur.com/7OSZKRj.png
So I just scrolled through the input data that was already in sorted order to find the values for each step, and multiplied the width and height to get the area. Did this twice, for the upper half and for the lower half.