The first part was not too bad, mostly just making sure my code lined up with the algorithm provided and then checking all the squares.

The second part was super duper slow. I think you could probably cache the calculations or use previous calculations of smaller size to speed up calculating the bigger sizes. However, I'm sleepy, and I noticed that the example problems both had maximum totals at relatively low numbers (12 and 16), so I just checked squares with sizes up to 30 in the hopes that I'd get lucky. I did, since my answer was in that range! So, high five for optimization-by-laziness!

I'm also really really proud of my sum-calculating code inside the nested loop. It feels very Rust-like, making use of iterators, take, skip, and flat_map. I did a little wiggle when I finally got it to compile. :)

/// Day 11: Chronal Charge/// /// Figure out the power contained in power cells/// A Grid of powercells with variable power levelspubstructGrid{cells:[[i32;300];300],serial:isize,}implGrid{pubfnnew(serial:isize)->Self{letmutgrid=Self{serial,cells:[[0;300];300]};grid.generate_values();grid}/// Each cell has a location/serial number-based checksum that/// determines its power level. Calculates all cells.fngenerate_values(&mutself){for(i,row)inself.cells.iter_mut().enumerate(){for(j,value)inrow.iter_mut().enumerate(){*value=Grid::power_level(self.serial,(i+1)asi32,(j+1)asi32);}}}/// The checksum power level calculation on a cell basis./// /// Depends on the grid's serial number and on the location of the cellfnpower_level(serial:isize,x:i32,y:i32)->i32{letmutresult=(xasisize+10)*(yasisize)+serial;result*=xasisize+10;result=(result/100)%10;(result-5)asi32}/// Finds the cell that is at the top left of the 3x3 with the highest/// total in the grid.pubfnbest_cell(&self)->(usize,usize){letmutmax_value=0;letmutmax_location=(0,0);foriin0..298{forjin0..298{letvalue=self.cells.iter().skip(i).take(3).flat_map(|row|row.iter().skip(j).take(3)).sum();ifvalue>max_value{max_value=value;max_location=(i+1,j+1);}}}max_location}/// Finds the cell that is at the top left of the N x N grid with/// the highest total power level, where N can be any size between/// 1 and 300.pubfnbest_cell_sized(&self)->(usize,usize,usize){letmutmax_value=0;letmutmax_location=(0,0,0);forsizein1..=30{println!("Doing iteration: {}",size);foriin0..=(300-size){forjin0..=(300-size){letvalue=self.cells.iter().skip(i).take(size).flat_map(|row|row.iter().skip(j).take(size)).sum();ifvalue>max_value{max_value=value;max_location=(i+1,j+1,size);}}}}max_location}}

## re: AoC Day 11: Chronal Charge VIEW POST

FULL DISCUSSIONFinished just in time for the next day!

The first part was not too bad, mostly just making sure my code lined up with the algorithm provided and then checking all the squares.

The second part was super duper slow. I think you could probably cache the calculations or use previous calculations of smaller size to speed up calculating the bigger sizes. However, I'm sleepy, and I noticed that the example problems both had maximum totals at relatively low numbers (12 and 16), so I just checked squares with sizes up to 30 in the hopes that I'd get lucky. I did, since my answer was in that range! So, high five for optimization-by-laziness!

I'm also really

reallyproud of my sum-calculating code inside the nested loop. It feels very Rust-like, making use of iterators, take, skip, and flat_map. I did a little wiggle when I finally got it to compile. :)