/ day14 / src / main.rs
main.rs
 1  use std::str;
 2  
 3  fn main() {
 4      let mut bots = str::from_utf8(include_bytes!("input"))
 5          .unwrap()
 6          .lines()
 7          .map(|l| {
 8              l.split("p=")
 9                  .last()
10                  .unwrap()
11                  .split(" v=")
12                  .map(|s| {
13                      s.split(",")
14                          .map(|n| n.parse::<i16>().unwrap())
15                          .collect::<Vec<i16>>()
16                  })
17                  .map(|v| (v[0], v[1]))
18                  .collect::<Vec<(i16, i16)>>()
19          })
20          .map(|v| (v[0], v[1]))
21          .collect::<Vec<((i16, i16), (i16, i16))>>();
22      let mut bc = bots.clone();
23      for _ in 0..100 {
24          step(&mut bc);
25      }
26      let mut q1 = 0;
27      let mut q2 = 0;
28      let mut q3 = 0;
29      let mut q4 = 0;
30      for bot in bc {
31          if bot.0 .0 < 50 {
32              if bot.0 .1 < 51 {
33                  q1 += 1;
34              } else if bot.0 .1 > 51 {
35                  q2 += 1;
36              }
37          } else if bot.0 .0 > 50 {
38              if bot.0 .1 < 51 {
39                  q3 += 1;
40              } else if bot.0 .1 > 51 {
41                  q4 += 1;
42              }
43          }
44      }
45      println!("Part 1 : {} {q1}*{q2}*{q3}*{q4}", q1 * q2 * q3 * q4);
46      let mut seconds = 0;
47      loop {
48          seconds += 1;
49          step(&mut bots);
50          let mut sum = 0;
51          for i in 40..50 {
52              for bot in &bots {
53                  if bot.0 == (35, i) {
54                      sum += 1;
55                  }
56              }
57          }
58          if sum >= 10 {
59              break;
60          }
61      }
62      println!("Part 2 : {seconds}");
63  }
64  
65  fn step(bots: &mut Vec<((i16, i16), (i16, i16))>) {
66      for bot in bots {
67          let new_pos = (
68              (bot.0 .0 + bot.1 .0).rem_euclid(101),
69              (bot.0 .1 + bot.1 .1).rem_euclid(103),
70          );
71          *bot = (new_pos, bot.1);
72      }
73  }