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 (points, folds) = get_data(); let mut width = 0; let mut height = 0; for (is_y, v) in folds.iter() { if *is_y { height = *v; } else { width = *v; } } let mut map = vec![false; width * height]; for (mut x, mut y) in points { for (is_y, v) in folds.iter() { if *is_y && y > *v { y = 2 * *v - y; } else if !*is_y && x > *v { x = 2 * *v - x; } } map[y * width + x] = true; } for y in 0..height { for x in 0..width { if map[y * width + x] { print!("X"); } else { print!("."); } } print!("\n"); } }