Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.14 KB | None | 0 0
  1. use std::io::{self, Read};
  2.  
  3. use regex::Regex;
  4.  
  5. fn read_stdin() -> String{
  6.     let mut buffer = String::new();
  7.     io::stdin().read_to_string(&mut buffer).expect("did not recieve anything from stdin");
  8.     return buffer;
  9. }
  10.  
  11.  
  12. #[derive(Debug, Clone, Hash, PartialEq, Eq)]
  13. struct Coord{
  14.     x: i64,
  15.     y: i64,
  16.     z: i64
  17. }
  18.  
  19. #[derive(Debug, Clone, PartialEq, Eq, Hash)]
  20. struct CBody{
  21.     pos: Coord,
  22.     velocity: Coord
  23. }
  24.  
  25. impl CBody{
  26.     fn new(nx: i64, ny: i64, nz: i64) -> CBody{
  27.         CBody{
  28.             pos: Coord{x: nx, y: ny, z: nz},
  29.             velocity: Coord{x: 0, y: 0, z: 0}
  30.         }
  31.     }
  32.     fn update(&mut self){
  33.         self.pos.x += self.velocity.x;
  34.         self.pos.y += self.velocity.y;
  35.         self.pos.z += self.velocity.z;
  36.     }
  37.     fn total_energy(&self) -> i64{
  38.         return self.pos.sum_of_absolute_coords() * self.velocity.sum_of_absolute_coords();
  39.     }
  40. }
  41.  
  42.  
  43.  
  44. fn update_velocities(planets: &mut Vec<CBody>){
  45.  
  46.     for i in 0..(planets.len()){
  47.         for j in (i + 1)..(planets.len()){
  48.             if planets[i].pos.x < planets[j].pos.x{
  49.                 planets[i].velocity.x += 1;          
  50.                 planets[j].velocity.x -= 1;          
  51.             }else if planets[j].pos.x < planets[i].pos.x{
  52.                 planets[j].velocity.x += 1;          
  53.                 planets[i].velocity.x -= 1;          
  54.             }
  55.             if planets[i].pos.y < planets[j].pos.y{
  56.                 planets[i].velocity.y += 1;          
  57.                 planets[j].velocity.y -= 1;          
  58.             }else if planets[j].pos.y < planets[i].pos.y{
  59.                 planets[j].velocity.y += 1;          
  60.                 planets[i].velocity.y -= 1;          
  61.             }
  62.             if planets[i].pos.z < planets[j].pos.z{
  63.                 planets[i].velocity.z += 1;          
  64.                 planets[j].velocity.z -= 1;          
  65.             }else if planets[j].pos.z < planets[i].pos.z{
  66.                 planets[j].velocity.z += 1;          
  67.                 planets[i].velocity.z -= 1;          
  68.             }
  69.         }
  70.     }
  71.  
  72. }
  73.  
  74. fn move_planets(planets: &mut Vec<CBody>){
  75.     for i in 0..(planets.len()){
  76.         planets[i].update();
  77.     }
  78. }
  79.  
  80.  
  81. fn simulate_until(mut planets: Vec<CBody>, count: usize) -> Vec<CBody> {
  82.     let mut steps: usize = 0;
  83.  
  84.     loop{
  85.         update_velocities(&mut planets);
  86.         move_planets(&mut planets);
  87.         steps += 1;
  88.  
  89.         if steps % (count / 10) == 0{
  90.             println!("{}", steps);
  91.         }
  92.  
  93.         if steps == count{
  94.             break;
  95.         }
  96.  
  97.     }
  98.     return planets;
  99. }
  100.  
  101. fn main(){
  102.     let pattern = Regex::new(r"<x=(-?\d+), y=(-?\d+), z=(-?\d+)>").unwrap();
  103.  
  104.     let io_input: String = read_stdin();
  105.  
  106.     let planets: Vec<CBody> =
  107.         pattern
  108.         .captures_iter(&io_input)
  109.         .map(|g|
  110.             CBody::new(
  111.                 g[1].parse().unwrap(),
  112.                 g[2].parse().unwrap(),
  113.                 g[3].parse().unwrap()
  114.             )
  115.         ).collect();
  116.    
  117.    
  118.     //let after_sim = simulate(planets);
  119.     let after_sim = simulate_until(planets, 1000000000);
  120.    
  121.     println!("{:?}", after_sim);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement