Guest User

Untitled

a guest
Dec 11th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. use rayon::prelude::*;
  2. use regex::Regex;
  3. use std::collections::HashMap;
  4.  
  5. fn main() {
  6. const INPUT: &str = include_str!("input.txt");
  7. let grid_serial: i64 = INPUT.parse().unwrap();
  8. let cells: HashMap<(i64, i64), i64> =
  9. // Populate the fuel cells
  10. (1..301i64).into_par_iter().flat_map(|x| {
  11. (1..301i64).into_par_iter().map(move |y| {
  12. let cell_rack_id = x + 10;
  13. let mut power_level = cell_rack_id * y;
  14. power_level += grid_serial;
  15. power_level *= cell_rack_id;
  16. if power_level < 100 {
  17. power_level = 0;
  18. } else {
  19. // FIXME: proper math algorithm. this is dumb
  20. power_level = power_level
  21. .to_string()
  22. .chars()
  23. .rev()
  24. .nth(2)
  25. .unwrap()
  26. .to_string()
  27. .parse()
  28. .unwrap();
  29. }
  30. power_level -= 5;
  31.  
  32. ((x, y), power_level)
  33. })
  34. }).collect();
  35.  
  36. // Sum all 3x3's
  37. let sum_cells: HashMap<(i64, i64), i64> =
  38. // main grid x
  39. (1i64..299i64).into_par_iter().flat_map(|grid_x| {
  40. // main grid y
  41. (1i64..299i64).into_par_iter().map(move |grid_y| {
  42. // hashmap's struct tuple
  43. (
  44. // coordinates tuple for the 3x3 square's top-left
  45. (grid_x , grid_y),
  46. // offsets for calculating sum of 3x3
  47. (0i64..3i64).into_par_iter().flat_map(|offset_x| {
  48. (0i64..3i64).into_par_iter().map(|offset_y| {
  49. // get the value at this coord+offset
  50. cells.get(&(grid_x + offset_x, grid_y + offset_y )).unwrap()
  51. })
  52. })
  53. .sum::<i64>()
  54. )
  55. })
  56. })
  57. .collect();
  58.  
  59. // Find best
  60. }
Add Comment
Please, Sign In to add comment