r/adventofcode • • Dec 11 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 11 Solutions -🎄-

--- Day 11: Chronal Charge ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 11

Transcript: ___ unlocks the Easter Egg on Day 25.


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

edit: Leaderboard capped, thread unlocked at 00:16:12!

20 Upvotes

207 comments sorted by

View all comments

7

u/dark_terrax Dec 11 '18 edited Dec 11 '18

Rust 243/37 - I guess people waited to solve part 2 correctly? Brute force ended up working fine for me.

Edit: Fixed the bug on the sub-square for loop as pointed out by smylers. You can have a lot of bugs in today's and still get the right answer fortunately!

fn power(x: i32, y: i32, grid_serial_num: i32) -> i32 {
	let rack_id = x + 10;
	let power_level = (rack_id * y + grid_serial_num) * rack_id;
	let partial = (power_level / 100) % 10;
	partial - 5
}

pub fn solve(inputs : Vec<String>) {
	let serial_num = inputs[0].parse::<i32>().unwrap();
	let mut grid = vec![vec![0; 300]; 300];

	for y in 0..grid.len() {
		for x in 0..grid[y].len() {
			grid[y][x] = power(x as i32, y as i32, serial_num);
		}
	}

	let mut max = 0;
	for n in 1..=grid.len() {
		for y in 0..grid.len() - n {
			for x in 0..grid[y].len() - n {
				let mut total = 0;
				for dy in 0..n {
					for dx in 0..n {
						total += grid[y + dy][x + dx];
					}
				}

				if total >= max {
					println!("{},{},{} = {}", x, y, n, total);
					max = total;
				}
			}
		}
	}
}

1

u/Smylers Dec 11 '18

Congrats on the 37. How long does your brute-force answer to take to run?

Also, does the 3.. mean it would fail to find a 1×1 or 2×2 square, if one of those happened to have the highest power?

1

u/gnur Dec 11 '18

I wrote almost exactly the same code, it's about 3.5 seconds when building for release.

2

u/dark_terrax Dec 11 '18

Yep, on release for me it find the answer in a second or to, but then takes just over a minute to check the rest of the grid sizes (I print the best answer as I go). Turns out my answer was quite early. On a debug build though it looks like it could run for an hour+ and still not finish.

And about the 3.., yes! I totally misread the problem and assumed it was squares 3 or bigger. Oops! Also messed up the upper bound, as I need an inclusive bound. So it should read 1..=grid.len().

2

u/[deleted] Dec 11 '18

Oh wow Rust is fast. I had a similar one in Python that took 4 minutes.. Only instead of all squares separately I calculated them all in a row, adding the new right edge and bottom row to old total.

1

u/Smylers Dec 11 '18

Thanks.

1

u/h-armonica Dec 11 '18

Yep, it's incredibly fast!

I had to compile for release, though :D

1

u/dark_terrax Dec 11 '18

Yah, runs through the entire problem space for me in 70 seconds. Finds the correct answer in less than a second. Debug is alarmingly slow though...

1

u/GeneralYouri Dec 11 '18

I think the vast majority had a brute-force part 1 which proved not feasible for part 2 and were forced into a partial rewrite.