use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; use std::cmp::min; use std::cmp::max; #[derive(Clone, PartialEq)] enum Cell { Void, Sand, Wall, } fn main() { let file_path = String::from("../aoc_14a/input"); println!("In file {}", file_path); let mut data: Vec> = vec![vec![Cell::Void; 1000]; 1000]; let mut min_x = 100000; let mut max_x = 0; let mut min_y = 100000; let mut max_y = 0; if let Ok(lines) = read_lines(file_path) { // Consumes the iterator, returns an (Optional) String for (i, line) in lines.enumerate() { if let Ok(ip) = line { let items: Vec<&str> = ip.split(" -> ").collect(); let mut x = items[1].split(",").collect::>()[0].parse::().unwrap(); let mut y = items[1].split(",").collect::>()[1].parse::().unwrap(); let mut prev_x = items[0].split(",").collect::>()[0].parse::().unwrap(); let mut prev_y = items[0].split(",").collect::>()[1].parse::().unwrap(); min_x = min(min_x, prev_x); max_x = max(max_x, prev_x); min_y = min(min_y, prev_y); max_y = max(max_y, prev_y); for v in items.iter().skip(2) { match max(x, prev_x) - min(x, prev_x) { 0 => { println!("draw vertical"); for i in min(y, prev_y)..max(y, prev_y) + 1 { data[x][i] = Cell::Wall; } }, _ => { println!("draw horizontal"); for i in min(x, prev_x)..max(x, prev_x) + 1 { data[i][y] = Cell::Wall; } } } prev_x = x; prev_y = y; x = v.split(",").collect::>()[0].parse::().unwrap(); y = v.split(",").collect::>()[1].parse::().unwrap(); min_x = min(min_x, x); max_x = max(max_x, x); min_y = min(min_y, y); max_y = max(max_y, y); println!("{}", v); } match max(x, prev_x) - min(x, prev_x) { 0 => { println!("draw vertical"); for i in min(y, prev_y)..max(y, prev_y) + 1 { data[x][i] = Cell::Wall; } }, _ => { println!("draw horizontal"); for i in min(x, prev_x)..max(x, prev_x) + 1 { data[i][y] = Cell::Wall; } } } println!("{:#?}", items); } } } for i in 0..1000 { data[i][max_y + 2] = Cell::Wall; } let mut sand_count = 0; 'outer: loop { let mut x = 500; let mut y = 0; loop { if data[500][0] == Cell::Sand { break 'outer; } if data[x][y + 1] == Cell::Void { y += 1; } else if data[x - 1][y + 1] == Cell::Void { x -= 1; y += 1; } else if data[x + 1][y + 1] == Cell::Void { x += 1; y += 1; } else { sand_count += 1; data[x][y] = Cell::Sand; break; } } } if min_y > 1 { min_y -= 2; } if min_x > 1 { min_x -= 2; } let dx = max_x - min_x; for i in 0..max_y + 4 { for j in min_x - dx..max_x + dx { match data[j][i] { Cell::Void => print!("."), Cell::Wall => print!("#"), Cell::Sand => print!("o"), } } println!(""); } println!("{} {} {} {}", min_x, max_x, min_y, max_y); println!("{}", sand_count); } fn read_lines

(filename: P) -> io::Result>> where P: AsRef, { let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) }