Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use rayon::prelude::*;
- use regex::Regex;
- use std::collections::HashMap;
- fn main() {
- const INPUT: &str = include_str!("input.txt");
- let grid_serial: i64 = INPUT.parse().unwrap();
- let cells: HashMap<(i64, i64), i64> =
- // Populate the fuel cells
- (1..301i64).into_par_iter().flat_map(|x| {
- (1..301i64).into_par_iter().map(move |y| {
- let cell_rack_id = x + 10;
- let mut power_level = cell_rack_id * y;
- power_level += grid_serial;
- power_level *= cell_rack_id;
- if power_level < 100 {
- power_level = 0;
- } else {
- // FIXME: proper math algorithm. this is dumb
- power_level = power_level
- .to_string()
- .chars()
- .rev()
- .nth(2)
- .unwrap()
- .to_string()
- .parse()
- .unwrap();
- }
- power_level -= 5;
- ((x, y), power_level)
- })
- }).collect();
- // Sum all 3x3's
- let sum_cells: HashMap<(i64, i64), i64> =
- // main grid x
- (1i64..299i64).into_par_iter().flat_map(|grid_x| {
- // main grid y
- (1i64..299i64).into_par_iter().map(move |grid_y| {
- // hashmap's struct tuple
- (
- // coordinates tuple for the 3x3 square's top-left
- (grid_x , grid_y),
- // offsets for calculating sum of 3x3
- (0i64..3i64).into_par_iter().flat_map(|offset_x| {
- (0i64..3i64).into_par_iter().map(|offset_y| {
- // get the value at this coord+offset
- cells.get(&(grid_x + offset_x, grid_y + offset_y )).unwrap()
- })
- })
- .sum::<i64>()
- )
- })
- })
- .collect();
- // Find best
- }
Add Comment
Please, Sign In to add comment