Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::iter::repeat;
- use std::cmp::max;
- const GRID_SIZE: i32 = 300;
- #[derive(Debug, Clone)]
- struct Coord {
- x: i32,
- y: i32,
- square_lvls: Vec<i32>,
- }
- impl Coord {
- fn new(x: i32, y: i32) -> Self {
- Coord {
- x,
- y,
- square_lvls: Vec::with_capacity((GRID_SIZE - max(x, y) + 1) as usize),
- }
- }
- fn calc_square_lvls(&mut self, lvls: &[i32], serial: i32) {
- assert!(self.square_lvls.is_empty());
- let mut pow = 0;
- for sz in 1..=(GRID_SIZE - max(self.x, self.y) + 1) {
- for d in 0..sz {
- //println!("x {} y {} sz {} d {}", self.x, self.y, sz, d);
- pow += lvls[(self.x - 1 + sz - 1 + (self.y - 1 + d) * sz) as usize];
- pow += lvls[(self.x - 1 + d + (self.y - 1 + sz - 1) * sz) as usize];
- }
- self.square_lvls.push(pow);
- }
- }
- }
- fn power(x: i32, y: i32, serial: i32) -> i32 {
- let rack_id: i32 = x + 10;
- (rack_id * y + serial) * rack_id / 100 % 10 - 5
- }
- fn square_power(x: i32, y: i32, serial: i32, size: i32) -> i32 {
- let mut pow = 0;
- for x in x..=x + size {
- for y in y..=y + size {
- pow += power(x, y, serial);
- }
- }
- pow
- }
- fn square_power_v2(pows: &[i32], x: i32, y: i32, serial: i32, size: i32) -> i32 {
- let mut pow = 0;
- for y in y..y + size {
- for x in x..x + size {
- pow += pows[((x - 1) + (y - 1) * 300) as usize];
- }
- }
- pow
- }
- fn main() {
- const SERIAL: i32 = 8141;
- let (mut coords, lvls): (Vec<Coord>, Vec<i32>) = (1..=300)
- .flat_map(|y| repeat(y).zip(1..=300))
- .map(|(y, x)| (Coord::new(x, y), power(x, y, SERIAL)))
- .unzip();
- for c in &mut coords {
- c.calc_square_lvls(&lvls, SERIAL);
- }
- /* Part 2 v3 */
- //let coords = vec![Coord { square_lvls: Vec::new() }; 300 * 300];
- let max = (1..=300)
- .flat_map(|x| repeat(x).zip(1..=300));
- /* Part 1 v2 */
- //let max = (1..=300 - 2)
- // .flat_map(|x| repeat(x).zip(1..=300 - 2))
- // .max_by_key(|(x, y)| square_power_v2(&lvls, *x, *y, SERIAL, 3))
- // .expect("no maximum found");
- //println!("Part 1: {},{}", max.0, max.1);
- /* Part 2 v2 */
- //let max = (1..=300)
- // .flat_map(|sz| repeat(sz).zip(1..300 - sz + 1))
- // .flat_map(|(sz, x)| repeat((sz, x)).zip(1..300 - sz + 1))
- // .map(|((sz, x), y)| (sz, (x, y)))
- // .max_by_key(|(sz, (x, y))| square_power_v2(&lvls, *x, *y, SERIAL, *sz))
- // .expect("no maximum found");
- //println!("Part 2: {},{},{}", (max.1).0, (max.1).1, max.0);
- /* Part 1 v1 */
- //let max = (1..=300 - 2)
- // .flat_map(|x| repeat(x).zip(1..=300 - 2))
- // .max_by_key(|(x, y)| square_power(*x, *y, SERIAL, 3))
- // .expect("no maximum found");
- //println!("Part 1: {},{}", max.0, max.1);
- /* Part 2 v1 */
- //let max = (1..=300)
- // .flat_map(|sz| repeat(sz).zip(1..300 - sz + 1))
- // .flat_map(|(sz, x)| repeat((sz, x)).zip(1..300 - sz + 1))
- // .map(|((sz, x), y)| (sz, (x, y)))
- // .max_by_key(|(sz, (x, y))| square_power(*x, *y, SERIAL, *sz))
- // .expect("no maximum found");
- //println!("Part 2: {},{},{}", (max.1).0, (max.1).1, max.0);
- assert_eq!(power(3, 5, 8), 4);
- assert_eq!(power(122, 79, 57), -5);
- assert_eq!(power(217, 196, 39), 0);
- assert_eq!(power(101, 153, 71), 4);
- }
Add Comment
Please, Sign In to add comment