Advertisement
nairby

Day 13 Code

Dec 13th, 2021 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.20 KB | None | 0 0
  1. use std::env;
  2. use std::io::{self};
  3. use std::collections::HashSet;
  4.  
  5. extern crate regex;
  6. use regex::Regex;
  7.  
  8. use point2d::point2d::Point2D;
  9.  
  10. enum Fold {
  11.     AlongX(i64),
  12.     AlongY(i64)
  13. }
  14. impl From<&str> for Fold {
  15.     fn from(s: &str) -> Self {
  16.         let re = Regex::new(r"fold along ([xy])=(\d+)").unwrap();
  17.         let matches = re.captures(s).unwrap();
  18.         match &matches[1] {
  19.             "x" => Self::AlongX(matches[2].parse().unwrap()),
  20.             "y" => Self::AlongY(matches[2].parse().unwrap()),
  21.             other => panic!("Unknown fold type: {}", other),
  22.         }
  23.     }
  24. }
  25.  
  26. fn map_extents(map: &HashSet<Point2D>) -> (i64,i64,i64,i64) {
  27.     let xmin = &map.iter().map(|&pt| pt.x).min().unwrap();
  28.     let ymin = &map.iter().map(|&pt| pt.y).min().unwrap();
  29.     let xmax = &map.iter().map(|&pt| pt.x).max().unwrap();
  30.     let ymax = &map.iter().map(|&pt| pt.y).max().unwrap();
  31.     (*xmin,*ymin,*xmax,*ymax)
  32. }
  33.  
  34. fn solve(input: &str) -> io::Result<()> {
  35.     // Input
  36.     let input_str = std::fs::read_to_string(input).unwrap();
  37.     let input_str = input_str.trim();
  38.     let input: Vec<_> = input_str.split("\n\n").collect();
  39.  
  40.     let initial_points: Vec<_> = input[0].split("\n").collect();
  41.     let folds: Vec<_> = input[1].split("\n").map(Fold::from).collect();
  42.  
  43.     // Build initial image
  44.     let mut image: HashSet<Point2D> = HashSet::new();
  45.     let re = Regex::new(r"(\d+),(\d+)").unwrap();
  46.     for point in initial_points {
  47.         let matches = re.captures(&point).unwrap();
  48.         let (x,y) = (matches[1].parse().unwrap(),matches[2].parse().unwrap());
  49.         image.insert(Point2D{x: x, y: y});
  50.     }
  51.  
  52.     // Process folds
  53.     let (_,_,mut xmax,mut ymax) = map_extents(&image);
  54.     for (i,fold) in folds.iter().enumerate() {
  55.  
  56.         // Part 1
  57.         if i == 1 {
  58.             let part1 = image.iter().filter(|&pt| pt.x <= xmax && pt.y <= ymax).count();
  59.             println!("Part 1: {}", part1); // 743
  60.         }
  61.  
  62.         match &fold {
  63.             Fold::AlongX(cut) => {
  64.                 for y in 0..=ymax {
  65.                     for x in *cut..=xmax {
  66.                         let pt = Point2D { x: x, y: y };
  67.                         let newpt = Point2D { x: 2*cut-x, y };
  68.                         if image.contains(&pt) { image.insert(newpt); }
  69.                     }
  70.                 }
  71.                 xmax = *cut;
  72.             },
  73.             Fold::AlongY(cut) => {
  74.                 for y in *cut..=ymax {
  75.                     for x in 0..=xmax {
  76.                         let pt = Point2D { x: x, y: y };
  77.                         let newpt = Point2D { x: x, y: 2*cut-y };
  78.                         if image.contains(&pt) { image.insert(newpt); }
  79.                     }
  80.                 }
  81.                 ymax = *cut;
  82.             },
  83.         }
  84.     }
  85.  
  86.     println!("Part 2:"); // RCPLAKHL
  87.     for y in 0..=ymax {
  88.         for x in 0..=xmax {
  89.             match image.contains(&Point2D { x: x, y: y }) {
  90.                 true => { print!("█"); },
  91.                 _    => { print!(" "); },
  92.             }
  93.         }
  94.         println!();
  95.     }
  96.  
  97.     Ok(())
  98. }
  99.  
  100. fn main() {
  101.     let args: Vec<String> = env::args().collect();
  102.     let filename = &args[1];
  103.     solve(&filename).unwrap();
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement