Advertisement
Guest User

AdventOfCode/22

a guest
Dec 22nd, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.43 KB | None | 0 0
  1. use tools::parser;
  2. use std::{ fmt, cmp };
  3.  
  4. #[derive(Copy, Clone)]
  5. struct Cube {
  6.     x: (i64, i64),
  7.     y: (i64, i64),
  8.     z: (i64, i64)
  9. }
  10.  
  11. impl fmt::Debug for Cube {
  12.     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  13.         write!(
  14.             f,
  15.             "Cube: x: {}|{}, y: {}|{}, z: {}|{}",
  16.             self.x.0,
  17.             self.x.1,
  18.             self.y.0,
  19.             self.y.1,
  20.             self.z.0,
  21.             self.z.1
  22.         )
  23.     }
  24. }
  25.  
  26. impl Cube {
  27.     fn new(x: (i64, i64), y: (i64, i64), z: (i64, i64)) -> Self {
  28.         Cube { x, y, z }
  29.     }
  30.  
  31.     fn size(&self) -> i64 {
  32.         (self.x.1 - self.x.0)
  33.         * (self.y.1 - self.y.0)
  34.         * (self.z.1 - self.z.0)
  35.     }
  36.  
  37.     fn valid(&self) -> bool {
  38.         self.x.1 > self.x.0 && self.y.1 > self.y.0 && self.z.1 > self.z.0
  39.     }
  40.  
  41.     fn intersects(&self, o: &Self) -> bool {
  42.         self.x.0 < o.x.1 && self.y.0 < o.y.1 && self.z.0 < o.z.1
  43.             && self.x.1 > o.x.0 && self.y.1 > o.y.0 && self.z.1 > o.z.0
  44.     }
  45.  
  46.     fn exclude(&self, o: &Self) -> Vec<Self> {
  47.         if !self.intersects(o) {
  48.             return vec![*self];
  49.         }
  50.         let get = | a: (i64, i64), b: (i64, i64), e | match e {
  51.             0 => (a.0, cmp::max(a.0, b.0)),
  52.             1 => (cmp::max(a.0, b.0), cmp::min(a.1, b.1)),
  53.             2 => (cmp::min(a.1, b.1), a.1),
  54.             _ => (0, 0)
  55.         };
  56.         let mut v = Vec::new();
  57.         for x in 0..=2 {
  58.             for y in 0..=2 {
  59.                 for z in 0..=2 {
  60.                     if x != 1 || y != 1 || z != 1 {
  61.                         v.push(Self::new(
  62.                             get(self.x, o.x, x),
  63.                             get(self.y, o.y, y),
  64.                             get(self.z, o.z, z)
  65.                         ));
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.         v.into_iter().filter(| c | c.valid()).collect()
  71.     }
  72. }
  73.  
  74. #[derive(Debug)]
  75. struct Command {
  76.     on: bool,
  77.     cube: Cube
  78. }
  79.  
  80. impl Command {
  81.     fn new(s: &str) -> Self {
  82.         let mut sp = s.split(" ").map(| s | s.to_string());
  83.         let on = sp.next().unwrap() == "on";
  84.         let coord: Vec<Vec<i64>> = sp.next().unwrap().split(",").map(| s | s[2..].to_string().split("..").map(| s | s.parse::<i64>().unwrap()).collect()).collect();
  85.         let vec_to_tuple = | v: &Vec<i64> | (v[0], v[1] + 1);
  86.         Command {
  87.             on,
  88.             cube: Cube::new(
  89.                 vec_to_tuple(&coord[0]),
  90.                 vec_to_tuple(&coord[1]),
  91.                 vec_to_tuple(&coord[2])
  92.             )
  93.         }
  94.     }
  95. }
  96.  
  97. fn main() {
  98.     let cmds: Vec<Command> = parser::string("./src/22.input").lines().map(| s | Command::new(s)).collect();
  99.     let mut v: Vec<Cube> = Vec::new();
  100.  
  101.     for cmd in cmds {
  102.         let mut n = Vec::new();
  103.         for c in v {
  104.             n.append(&mut c.exclude(&cmd.cube));
  105.         }
  106.         if cmd.on {
  107.             n.push(cmd.cube);
  108.         }
  109.         v = n;
  110.     }
  111.     println!("{:#?}", v.into_iter().map(| c | c.size()).sum::<i64>());
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement