use std::env::args; use std::fs::File; use std::io::BufRead; use std::io::BufReader; fn split_once<'a>(s: &'a str, sep: &str) -> (&'a str, &'a str) { let i = s.find(sep).unwrap(); let left = &s[..i]; let right = &s[i+sep.len()..]; return (left, right); } fn get_data() -> (Vec<(usize, usize)>, Vec<(bool, usize)>){ let path = args().nth(1).unwrap(); let file = File::open(path).unwrap(); let mut state = false; let mut points = vec![]; let mut folds = vec![]; for line in BufReader::new(file).lines() { let l = line.unwrap(); if l == "" { state = true; } else if state { // println!("{} {}", l[11..12], l[13..]); folds.push(( l.chars().nth(11).unwrap() == 'y', l[13..].parse().unwrap(), )); } else { let (x, y) = split_once(&l, ","); points.push(( x.parse().unwrap(), y.parse().unwrap(), )); } } return (points, folds); } fn main() { let (mut points, folds) = get_data(); let (is_y, v) = folds[0]; points = points .into_iter() .map(|(x, y)| { if is_y && y > v { (x, 2 * v - y) } else if !is_y && x > v { (2 * v - x, y) } else { (x, y) } }) .collect(); points.sort_unstable(); points.dedup(); println!("{}", points.len()); }