Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::iter::repeat;
- fn power_level(x: i32, y: i32, serial: i32) -> i32 {
- let rack_id: i32 = x + 10;
- (rack_id * y + serial) * rack_id / 100 % 10 - 5
- }
- fn main() {
- const SERIAL: i32 = 8141;
- let max = (1..=300-2)
- .flat_map(|x| repeat(x).zip(1..=300 - 2))
- .max_by_key(|(x, y)| {
- let mut pow = 0;
- for x in *x..=*x + 2 {
- for y in *y..=*y + 2 {
- pow += power_level(x, y, SERIAL);
- }
- }
- pow
- })
- .expect("no maximum found");
- println!("Part 1: {},{}", max.0, max.1);
- assert_eq!(power_level(3, 5, 8), 4);
- assert_eq!(power_level(122, 79, 57), -5);
- assert_eq!(power_level(217, 196, 39), 0);
- assert_eq!(power_level(101, 153, 71), 4);
- }
Add Comment
Please, Sign In to add comment