clean up: autoformat

This commit is contained in:
Baipyrus 2024-01-26 12:42:23 +01:00
parent 1525ddfa95
commit ae4332c7d3
5 changed files with 43 additions and 82 deletions

View File

@ -4,8 +4,7 @@ use std::path::Path;
pub fn read_file(name: &str) -> String { pub fn read_file(name: &str) -> String {
let path = Path::new("src").join(name).join("input.txt"); let path = Path::new("src").join(name).join("input.txt");
let mut file = File::open(&path) let mut file = File::open(&path).expect(&format!("Unable to open input for '{name}'."));
.expect(&format!("Unable to open input for '{name}'."));
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents) file.read_to_string(&mut contents)

View File

@ -9,8 +9,7 @@ pub fn split_lines(input: &str) -> Vec<String> {
} }
pub fn match_input(entry: &str, re: &str) -> Vec<String> { pub fn match_input(entry: &str, re: &str) -> Vec<String> {
let regex = Regex::new(re) let regex = Regex::new(re).expect("Invalid regex provided!");
.expect("Invalid regex provided!");
let matches: Vec<String> = regex let matches: Vec<String> = regex
.find_iter(entry) .find_iter(entry)
@ -35,13 +34,13 @@ pub fn convert_num(input: Vec<Vec<String>>) -> Vec<Vec<i32>> {
input input
.into_iter() .into_iter()
.map(|a| { .map(|a| {
a a.into_iter()
.into_iter()
.map(|b| { .map(|b| {
b b.trim()
.trim() .parse::<i32>()
.parse::<i32>()
.expect("Invalid numeric value in input!") .expect("Invalid numeric value in input!")
}).collect() })
}).collect() .collect()
})
.collect()
} }

View File

@ -1,4 +1,4 @@
use crate::common::{convert_num, read_file, match_inputs, split_lines}; use crate::common::{convert_num, match_inputs, read_file, split_lines};
#[allow(dead_code)] #[allow(dead_code)]
pub fn part_two() { pub fn part_two() {
@ -18,72 +18,35 @@ fn predict(forward: bool) {
let current = s.to_vec(); let current = s.to_vec();
let mut next = extra_diff(current); let mut next = extra_diff(current);
expand(&mut next, forward); expand(&mut next, forward);
let history = next let history = next.first().unwrap();
.first()
.unwrap();
if forward { if forward {
history history.last().unwrap().clone()
.last()
.unwrap()
.clone()
} else { } else {
history history.first().unwrap().clone()
.first()
.unwrap()
.clone()
} }
}) })
.collect(); .collect();
let sum: i32 = predictions let sum: i32 = predictions.iter().sum();
.iter()
.sum();
println!("The sum of all prediction values is: '{sum}'."); println!("The sum of all prediction values is: '{sum}'.");
} }
fn expand(strct: &mut Vec<Vec<i32>>, forward: bool) { fn expand(strct: &mut Vec<Vec<i32>>, forward: bool) {
let last = strct let last = strct.last_mut().unwrap();
.last_mut() last.push(*last.last().unwrap());
.unwrap();
last.push(*last
.last()
.unwrap()
);
for i in (0..strct.len() - 1).rev() { for i in (0..strct.len() - 1).rev() {
let last = strct let last = strct.get(i + 1).unwrap().clone();
.get(i + 1) let current = strct.get_mut(i).unwrap();
.unwrap()
.clone();
let current = strct
.get_mut(i)
.unwrap();
if forward { if forward {
current.push( current.push(current.last().unwrap().clone() + last.last().unwrap().clone());
current
.last()
.unwrap()
.clone() +
last
.last()
.unwrap()
.clone()
);
} else { } else {
current.insert( current.insert(
0, 0,
current current.first().unwrap().clone() - last.first().unwrap().clone(),
.first()
.unwrap()
.clone() -
last
.first()
.unwrap()
.clone()
); );
} }
} }

View File

@ -1,9 +1,7 @@
use crate::common::{read_file, match_inputs, split_lines}; use crate::common::{match_inputs, read_file, split_lines};
#[allow(dead_code)] #[allow(dead_code)]
pub fn part_two() { pub fn part_two() {}
}
#[allow(dead_code)] #[allow(dead_code)]
pub fn part_one() { pub fn part_one() {
@ -21,7 +19,9 @@ fn sum_dist(galaxies: Vec<Galaxy>) -> usize {
for (i, a) in galaxies.iter().enumerate() { for (i, a) in galaxies.iter().enumerate() {
for b in galaxies.iter().skip(i) { for b in galaxies.iter().skip(i) {
if b.compare(a) { continue; } if b.compare(a) {
continue;
}
distances += a.distance(b); distances += a.distance(b);
} }
} }
@ -34,7 +34,9 @@ fn extract(symbols: Vec<Vec<String>>) -> Vec<Galaxy> {
for (y, line) in symbols.iter().enumerate() { for (y, line) in symbols.iter().enumerate() {
for (x, symbol) in line.iter().enumerate() { for (x, symbol) in line.iter().enumerate() {
if symbol.as_str() == "." { continue; } if symbol.as_str() == "." {
continue;
}
galaxies.push(Galaxy { x, y }); galaxies.push(Galaxy { x, y });
} }
} }
@ -42,9 +44,7 @@ fn extract(symbols: Vec<Vec<String>>) -> Vec<Galaxy> {
galaxies galaxies
} }
fn expand(galaxies: &mut Vec<Galaxy>) { fn expand(galaxies: &mut Vec<Galaxy>) {}
}
struct Galaxy { struct Galaxy {
x: usize, x: usize,