mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-12-25 19:41:45 +00:00
clean up: autoformat
This commit is contained in:
parent
1525ddfa95
commit
ae4332c7d3
@ -4,13 +4,12 @@ use std::path::Path;
|
||||
|
||||
pub fn read_file(name: &str) -> String {
|
||||
let path = Path::new("src").join(name).join("input.txt");
|
||||
let mut file = File::open(&path)
|
||||
.expect(&format!("Unable to open input for '{name}'."));
|
||||
|
||||
let mut file = File::open(&path).expect(&format!("Unable to open input for '{name}'."));
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)
|
||||
.expect(&format!("Unable to read input for '{name}'."));
|
||||
|
||||
|
||||
contents
|
||||
}
|
||||
|
||||
@ -23,6 +22,6 @@ pub fn read_stdin() -> String {
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("Failed to read line!");
|
||||
|
||||
|
||||
input
|
||||
}
|
||||
|
@ -9,14 +9,13 @@ pub fn split_lines(input: &str) -> Vec<String> {
|
||||
}
|
||||
|
||||
pub fn match_input(entry: &str, re: &str) -> Vec<String> {
|
||||
let regex = Regex::new(re)
|
||||
.expect("Invalid regex provided!");
|
||||
|
||||
let regex = Regex::new(re).expect("Invalid regex provided!");
|
||||
|
||||
let matches: Vec<String> = regex
|
||||
.find_iter(entry)
|
||||
.map(|m| m.as_str().to_string())
|
||||
.collect();
|
||||
|
||||
|
||||
matches
|
||||
}
|
||||
|
||||
@ -35,13 +34,13 @@ pub fn convert_num(input: Vec<Vec<String>>) -> Vec<Vec<i32>> {
|
||||
input
|
||||
.into_iter()
|
||||
.map(|a| {
|
||||
a
|
||||
.into_iter()
|
||||
a.into_iter()
|
||||
.map(|b| {
|
||||
b
|
||||
.trim()
|
||||
.parse::<i32>()
|
||||
b.trim()
|
||||
.parse::<i32>()
|
||||
.expect("Invalid numeric value in input!")
|
||||
}).collect()
|
||||
}).collect()
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
@ -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)]
|
||||
pub fn part_two() {
|
||||
@ -18,85 +18,48 @@ fn predict(forward: bool) {
|
||||
let current = s.to_vec();
|
||||
let mut next = extra_diff(current);
|
||||
expand(&mut next, forward);
|
||||
let history = next
|
||||
.first()
|
||||
.unwrap();
|
||||
|
||||
let history = next.first().unwrap();
|
||||
|
||||
if forward {
|
||||
history
|
||||
.last()
|
||||
.unwrap()
|
||||
.clone()
|
||||
history.last().unwrap().clone()
|
||||
} else {
|
||||
history
|
||||
.first()
|
||||
.unwrap()
|
||||
.clone()
|
||||
history.first().unwrap().clone()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let sum: i32 = predictions
|
||||
.iter()
|
||||
.sum();
|
||||
|
||||
let sum: i32 = predictions.iter().sum();
|
||||
|
||||
println!("The sum of all prediction values is: '{sum}'.");
|
||||
}
|
||||
|
||||
fn expand(strct: &mut Vec<Vec<i32>>, forward: bool) {
|
||||
let last = strct
|
||||
.last_mut()
|
||||
.unwrap();
|
||||
last.push(*last
|
||||
.last()
|
||||
.unwrap()
|
||||
);
|
||||
let last = strct.last_mut().unwrap();
|
||||
last.push(*last.last().unwrap());
|
||||
|
||||
for i in (0..strct.len() - 1).rev() {
|
||||
let last = strct
|
||||
.get(i + 1)
|
||||
.unwrap()
|
||||
.clone();
|
||||
let current = strct
|
||||
.get_mut(i)
|
||||
.unwrap();
|
||||
|
||||
let last = strct.get(i + 1).unwrap().clone();
|
||||
let current = strct.get_mut(i).unwrap();
|
||||
|
||||
if forward {
|
||||
current.push(
|
||||
current
|
||||
.last()
|
||||
.unwrap()
|
||||
.clone() +
|
||||
last
|
||||
.last()
|
||||
.unwrap()
|
||||
.clone()
|
||||
);
|
||||
current.push(current.last().unwrap().clone() + last.last().unwrap().clone());
|
||||
} else {
|
||||
current.insert(
|
||||
0,
|
||||
current
|
||||
.first()
|
||||
.unwrap()
|
||||
.clone() -
|
||||
last
|
||||
.first()
|
||||
.unwrap()
|
||||
.clone()
|
||||
current.first().unwrap().clone() - last.first().unwrap().clone(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn extra_diff(seq: Vec<i32>) -> Vec<Vec<i32>> {
|
||||
let mut structure = vec![seq];
|
||||
let mut structure = vec![seq];
|
||||
let mut index = 0;
|
||||
|
||||
loop {
|
||||
let current = &structure[index];
|
||||
let diff = seq_diff(current.to_vec());
|
||||
|
||||
|
||||
if diff.iter().all(|&n| n == 0) {
|
||||
break structure;
|
||||
}
|
||||
@ -119,7 +82,7 @@ fn seq_diff(seq: Vec<i32>) -> Vec<i32> {
|
||||
fn setup() -> Vec<Vec<i32>> {
|
||||
let name = "day09";
|
||||
println!("Executing module '{name}' entrypoint . . . ");
|
||||
|
||||
|
||||
let content = read_file(name);
|
||||
let lines = split_lines(&content);
|
||||
let input = match_inputs(lines, &r"\s*-?\d+\s*");
|
||||
|
@ -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)]
|
||||
pub fn part_two() {
|
||||
|
||||
}
|
||||
pub fn part_two() {}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn part_one() {
|
||||
@ -21,7 +19,9 @@ fn sum_dist(galaxies: Vec<Galaxy>) -> usize {
|
||||
|
||||
for (i, a) in galaxies.iter().enumerate() {
|
||||
for b in galaxies.iter().skip(i) {
|
||||
if b.compare(a) { continue; }
|
||||
if b.compare(a) {
|
||||
continue;
|
||||
}
|
||||
distances += a.distance(b);
|
||||
}
|
||||
}
|
||||
@ -31,10 +31,12 @@ fn sum_dist(galaxies: Vec<Galaxy>) -> usize {
|
||||
|
||||
fn extract(symbols: Vec<Vec<String>>) -> Vec<Galaxy> {
|
||||
let mut galaxies = Vec::new();
|
||||
|
||||
|
||||
for (y, line) in symbols.iter().enumerate() {
|
||||
for (x, symbol) in line.iter().enumerate() {
|
||||
if symbol.as_str() == "." { continue; }
|
||||
if symbol.as_str() == "." {
|
||||
continue;
|
||||
}
|
||||
galaxies.push(Galaxy { x, y });
|
||||
}
|
||||
}
|
||||
@ -42,9 +44,7 @@ fn extract(symbols: Vec<Vec<String>>) -> Vec<Galaxy> {
|
||||
galaxies
|
||||
}
|
||||
|
||||
fn expand(galaxies: &mut Vec<Galaxy>) {
|
||||
|
||||
}
|
||||
fn expand(galaxies: &mut Vec<Galaxy>) {}
|
||||
|
||||
struct Galaxy {
|
||||
x: usize,
|
||||
@ -66,7 +66,7 @@ impl Galaxy {
|
||||
fn setup() -> Vec<Vec<String>> {
|
||||
let name = "day11";
|
||||
println!("Executing module '{name}' entrypoint . . . ");
|
||||
|
||||
|
||||
let content = read_file(name);
|
||||
let lines = split_lines(&content);
|
||||
match_inputs(lines, &r".")
|
||||
|
@ -8,10 +8,10 @@ mod day11;
|
||||
fn main() {
|
||||
println!("Executing main entrypoint . . . ");
|
||||
let now = Instant::now();
|
||||
|
||||
|
||||
// day09::part_one();
|
||||
// day09::part_two();
|
||||
|
||||
|
||||
day11::part_one();
|
||||
// day11::part_two();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user