This commit is contained in:
2021-12-14 14:23:34 +01:00
parent dfe8f1dfe8
commit 10bd7c4997
8 changed files with 1255 additions and 0 deletions

112
december_13/src/main.rs Normal file
View File

@@ -0,0 +1,112 @@
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
fn main(){
let file = File::open("./input.txt").expect("Read failed");
let reader = BufReader::new(file);
let mut line_vec = Vec::new();
for line in reader.lines() {
line_vec.push(line.unwrap());
}
let mut coordinates: Vec<(i32, i32)> = Vec::new();
let mut folds: Vec<(char, i32)> = Vec::new();
let mut processing_coordinates = true;
// Get coordinates
for line in &line_vec {
let _t = line.trim();
if line.trim().len() < 2 {
processing_coordinates = false;
} else if processing_coordinates {
let split_string = line.split(",").collect::<Vec<&str>>();
let x: i32 = split_string[0].to_owned().parse().unwrap();
let y: i32 = split_string[1].to_owned().parse().unwrap();
coordinates.push( (x, y) );
} else {
let split_string = line.split(" ").collect::<Vec<&str>>();
let split_coords = split_string[2].split("=").collect::<Vec<&str>>();
let split_direction: char = split_coords[0].to_owned().parse().unwrap();
let split_cord: i32 = split_coords[1].to_owned().parse().unwrap();
folds.push((split_direction, split_cord));
}
}
println!("There are {} lights", coordinates.len());
for fold in folds {
let line = fold.1;
let mut new_cords = Vec::new();
if fold.0 == 'x' {
for cord in &coordinates {
let mut x = cord.0;
let mut y = cord.1;
if x > line {
let diff = x - line;
x = line - diff;
}
new_cords.push((x,y));
}
} else {
for cord in &coordinates {
let mut x = cord.0;
let mut y = cord.1;
if y > line {
let diff = y - line;
y = line - diff;
}
new_cords.push((x,y));
}
}
coordinates = new_cords;
coordinates.sort_unstable();
coordinates.dedup();
println!("There are {} lights", coordinates.len());
}
let mut width = 0;
let mut height = 0;
for cord in &coordinates {
if cord.0 + 1 > width {
width = cord.0 + 1;
}
if cord.1 + 1 > height {
height = cord.1 + 1;
}
}
println!("Grid is {} high and {} wide", height, width);
for y in 0..=height {
for x in 0..=width {
let mut exists = false;
for cord in &coordinates {
if cord.0 == x && cord.1 == y {
exists = true;
}
}
if exists {
print!("X");
} else {
print!(" ");
}
}
println!("");
}
}