Guest User

Untitled

a guest
Dec 10th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #[macro_use] extern crate scan_fmt;
  2.  
  3. use std::io::BufRead;
  4.  
  5. #[derive(Debug)]
  6. struct Star {
  7. position: (i64, i64),
  8. velocity: (i64, i64)
  9. }
  10.  
  11. fn integrate_step(stars: &mut [Star]) {
  12. for star in stars {
  13. star.position.0 += star.velocity.0;
  14. star.position.1 += star.velocity.1;
  15. }
  16. }
  17.  
  18. fn undo_step(stars: &mut [Star]) {
  19. for star in stars {
  20. star.position.0 -= star.velocity.0;
  21. star.position.1 -= star.velocity.1;
  22. }
  23. }
  24.  
  25. /// Returns (minx, miny), (maxx, maxy)
  26. fn bounding_box(stars: &[Star]) -> ((i64, i64), (i64, i64)){
  27. let minx = stars.iter().map(|s| s.position.0).min().unwrap();
  28. let miny = stars.iter().map(|s| s.position.1).min().unwrap();
  29. let maxx = stars.iter().map(|s| s.position.0).max().unwrap();
  30. let maxy = stars.iter().map(|s| s.position.1).max().unwrap();
  31.  
  32. ((minx, miny), (maxx, maxy))
  33. }
  34.  
  35. fn area(bounding_box: ((i64, i64), (i64, i64))) -> i64 {
  36. (bounding_box.0 .0 - bounding_box.1 .0) * (bounding_box.0 .1 - bounding_box.1 .1)
  37. }
  38.  
  39. fn print_stars(stars: &[Star]) {
  40. let ((min_x, min_y), (max_x, max_y)) = bounding_box(stars);
  41.  
  42. // Fill a buffer with the thing to print
  43. // buf[y][x] gives whether to print or not at (x, y). True means print.
  44. let mut buf = vec![vec![false; (max_x - min_x + 1) as usize]; (max_y - min_y + 1) as usize];
  45. for star in stars {
  46. buf[(star.position.1 - min_y) as usize][(star.position.0 - min_x) as usize] = true;
  47. }
  48.  
  49. for line in buf {
  50. for val in line {
  51. if val {
  52. print!("#");
  53. } else {
  54. print!(" ");
  55. }
  56. }
  57. println!("");
  58. }
  59. }
  60.  
  61. fn main() {
  62. let mut stars: Vec<Star> = std::io::stdin()
  63. .lock()
  64. .lines()
  65. .map(|line| {
  66. let (x, y, vx, vy) = scan_fmt!(
  67. &line.expect("Couldn't read input"),
  68. "position=<{}, {}> velocity=<{}, {}>",
  69. i64, i64, i64, i64);
  70. Star {
  71. position: (x.unwrap(), y.unwrap()),
  72. velocity: (vx.unwrap(), vy.unwrap())
  73. }
  74. }).collect();
  75.  
  76. // Integrate until we hit minimum bounding box
  77. let mut previous_area = area(bounding_box(&stars));
  78. let mut number_seconds = 0;
  79. while previous_area >= area(bounding_box(&stars)) {
  80. previous_area = area(bounding_box(&stars));
  81. integrate_step(&mut stars);
  82. number_seconds += 1;
  83. }
  84.  
  85. // Undo the last step to get to the minimum bounding box
  86. undo_step(&mut stars);
  87. number_seconds -= 1;
  88.  
  89. println!("Message in the sky:");
  90. print_stars(&stars);
  91. println!("Waited {} seconds", number_seconds);
  92. }
Add Comment
Please, Sign In to add comment