Compare commits

...

7 Commits

Author SHA1 Message Date
ae4332c7d3 clean up: autoformat 2024-01-26 12:42:23 +01:00
1525ddfa95 clean up: formatting 2024-01-26 12:36:24 +01:00
db747ef3fe calculate manhattan distance of pairs and sum 2024-01-25 17:48:12 +01:00
d0f0a31d14 prepare expanding 2024-01-25 17:47:16 +01:00
c06faa97ff extract '#' as galaxies 2024-01-25 17:45:13 +01:00
83925dfd48 initialize day 11 2024-01-25 17:43:20 +01:00
61c576afd4 match by regex instead of splitting 2024-01-25 17:38:56 +01:00
6 changed files with 258 additions and 79 deletions

View File

@ -4,13 +4,12 @@ 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)
.expect(&format!("Unable to read input for '{name}'.")); .expect(&format!("Unable to read input for '{name}'."));
contents contents
} }
@ -23,6 +22,6 @@ pub fn read_stdin() -> String {
io::stdin() io::stdin()
.read_line(&mut input) .read_line(&mut input)
.expect("Failed to read line!"); .expect("Failed to read line!");
input input
} }

View File

@ -8,23 +8,22 @@ pub fn split_lines(input: &str) -> Vec<String> {
.collect() .collect()
} }
pub fn split_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");
let matches: Vec<String> = regex
let split: Vec<String> = regex .find_iter(entry)
.split(entry) .map(|m| m.as_str().to_string())
.map(|s| s.to_string())
.collect(); .collect();
split matches
} }
pub fn split_inputs(input: Vec<String>, re: &str) -> Vec<Vec<String>> { pub fn match_inputs(input: Vec<String>, re: &str) -> Vec<Vec<String>> {
let mut result = Vec::new(); let mut result = Vec::new();
for entry in input { for entry in input {
let split = split_input(&entry, re); let split = match_input(&entry, re);
result.push(split); result.push(split);
} }
@ -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, split_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,85 +18,48 @@ 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()
); );
} }
} }
} }
fn extra_diff(seq: Vec<i32>) -> Vec<Vec<i32>> { fn extra_diff(seq: Vec<i32>) -> Vec<Vec<i32>> {
let mut structure = vec![seq]; let mut structure = vec![seq];
let mut index = 0; let mut index = 0;
loop { loop {
let current = &structure[index]; let current = &structure[index];
let diff = seq_diff(current.to_vec()); let diff = seq_diff(current.to_vec());
if diff.iter().all(|&n| n == 0) { if diff.iter().all(|&n| n == 0) {
break structure; break structure;
} }
@ -119,9 +82,9 @@ fn seq_diff(seq: Vec<i32>) -> Vec<i32> {
fn setup() -> Vec<Vec<i32>> { fn setup() -> Vec<Vec<i32>> {
let name = "day09"; let name = "day09";
println!("Executing module '{name}' entrypoint . . . "); println!("Executing module '{name}' entrypoint . . . ");
let content = read_file(name); let content = read_file(name);
let lines = split_lines(&content); let lines = split_lines(&content);
let input = split_inputs(lines, &r"\s+"); let input = match_inputs(lines, &r"\s*-?\d+\s*");
convert_num(input) convert_num(input)
} }

140
src/day11/input.txt Normal file
View File

@ -0,0 +1,140 @@
..#............................#....................................................................................#..........#........#...
..........#.........................................#.......................................................................................
...................#..........................................................................#.............................................
.....................................................................................#.............................................#........
.......#.......#.............#..........#......#...................#.....#..................................................................
................................................................................#..................#..............#.........................
....................................#.......................................................................................................
...........#...................................................................................#......................................#.....
.......................................................................................................................#.........#..........
.....................#.................................................#...................#.................#..............#...............
....#...........................................#...............#...........................................................................
.........................#.......................................................................#..........................................
...................................#......................#.......................................................#.......................#.
..................#...........................................................#.......................#.................#...........#.......
..........#........................................................#..........................................#.............................
.............................................#.........#.....#..............................................................................
.......................#.....................................................................#..............................#..........#....
.............................#.......................................................#...........................#..........................
..#...........#.......................#.....................................#...............................................................
...........................................................#.............................................#..................................
......#..........................................................#......#........................................................#..........
................................#.........#...........#....................................#........#...................#..................#
...........................#..................................................#..............................#..............................
..............................................#.................................................#...........................................
.#.................................................................................................................#........................
.......................................................................#....................................................................
...............#.............................................................................................................#..........#...
.................................#.............................#............................................#...............................
.....#.................#...................#................................................................................................
....................................................#...................................................#............#......................
...........#.....#...................................................................#............................................#.........
..............................................#...........................................................................#.................
...#.........................#.....................................................................#........................................
.........................................................#...................................#........................................#.....
.........#....................................................#........#................#.....................#.............................
........................#..........................................................................................#........................
..............#...................................#............................#.............................................#.....#........
.................................#................................................................#.........................................
..................#........................#...............#................................#.............#.............................#...
..........#........................................................#............................................................#...........
..#.....................................................................................#.............................#.....................
..........................#..........#..................................#......................................#...........#................
................................................................#...........................................................................
................#................................#..........................#...............................................................
...........................................#..................................................#.....#.......#...............................
..........#............................................#.....................................................................#..............
.......................#....................................#...........................................................#..............#....
.....#.........................#........#..............................................#.................#........................#.........
.................#....................................................#............................................#........................
....................................#...............#................................................#......................................
.........................#........................................#................#.......#...................#............................
....................................................................................................................................#.......
...........#...................................#...........................#......................#.........................#..............#
..................................#......#..................................................................................................
......#....................#.........................#..................................................#...................................
......................#...................................#......#.....#.................................................#..................
..............#..............................................................................#.................#.....................#......
............................................................................................................................................
#.............................#................#....................................................................#....................#..
............................................................................................................................................
........#...........................................#.......................................................................................
...................................................................#..........#..................#..........................................
.....................#............#........#.............................................#............#................#....................
................#............................................................................................................#..............
...............................................................................................................#...................#........
........................#......#..........................#.....#......#................................................................#...
................................................#..........................................#.............#..................................
......................................#..............................................................................#......................
..#.................................................................#.........#.............................................................
..........#.....#...............................................................................................#...........#...............
.................................#....................................................................#..............................#......
.....................................................#........................................#........................#....................
..........................................#..................#............#.................................................................
......................#...............................................................#..................#.........#............#...........
......#........................................................................#............................................................
...................................#...................................#..................................................#.................
...................................................#.........................................#.................#............................
...........................#...................................#.....................................................................#......
........................................#...................................................................................................
................#......................................#........................#.........#.....#...........................................
...........#......................#.....................................................................#...................................
....#....................#...................#...................#................................................#...........#...........#.
............................................................................................................................................
............................................................................................................................................
.....................................#....................................#.................................................................
.......................#..................#........#............................#................#.............#............................
...............#...............................................#......................#..............................................#......
............................................................................................................................................
.................................#...........#.....................#....................................................#...................
............................................................................................................................................
..#........#..........#...............#..................................................................#..........#......................#
............................................................#............................#..................................................
.................................................................#..........#.................................#.................#...........
.............................#......................#.......................................................................................
....#..................................................................................................................................#....
...................#...............#..................................................#....................#................................
.........................................................#......................................#...................#..............#........
.....................................................................#...................................................#..................
........................#...................................................................#..................#............................
...#.....#...............................#....................#.............................................................................
................#...............#...........................................................................................................
.....................#........................................................#.....................................................#.......
..............................................#.......#................#...............................................#...................#
.........................................................................................#...............#...................#..............
.#...........................#.......#......................................................................................................
.............................................................................................................#........................#.....
.................................#..........................................................................................................
...........#........................................................................................#.............................#.........
................#...............................................#............................#...................#..........................
...#............................................................................#...........................................................
....................................................#.......#.......#......#...........................................................#....
.........................#..................................................................................................................
....................#...............#.........................................................................................#.............
.........................................................#....................#......................#......................................
....#........................#...............#........................#...............#.....................................................
..........#......#....................................................................................................................#.....
..............................................................................................#...................#.........................
.......................................................#...................................................#.................#..............
..........................#....................#............................................................................................
........................................#......................#.........#...............#.......#......................#.........#.....#...
...............................#....................................#.......................................................................
........#...........#.............................#.........................................................................................
...#...........#...................................................................#..................#............#........................
...........................#..............................#...................................#.............................................
...................................#.............................#..........................................#...............................
..............................................#........................#........#..........................................#................
.......................................................................................#.............................#..............#.......
.........................................#..................................................................................................
.....#............#......................................#.....#............................#...............................................
............................#...............................................................................................................
.......................#.........#........................................#..............................#...............................#..
..#...........#.......................................#.........................#...........................................................
.......................................#.........................#..........................................................................
............................................................#...............................................................................
.......#......................#........................................#................................................#..............#....
..............................................................................................................#.............................
...............................................#.....................................................#......................................
......................#..................................................................#......#.................................#.......#.
..#.......#................#......#..................................#.......#.....................................#........................
................#...............................................#..................#....................#..................#................

73
src/day11/mod.rs Normal file
View File

@ -0,0 +1,73 @@
use crate::common::{match_inputs, read_file, split_lines};
#[allow(dead_code)]
pub fn part_two() {}
#[allow(dead_code)]
pub fn part_one() {
let input = setup();
let mut galaxies = extract(input);
expand(&mut galaxies);
let distances = sum_dist(galaxies);
println!("The sum of all distances is: '{distances}'.");
}
fn sum_dist(galaxies: Vec<Galaxy>) -> usize {
let mut distances = 0;
for (i, a) in galaxies.iter().enumerate() {
for b in galaxies.iter().skip(i) {
if b.compare(a) {
continue;
}
distances += a.distance(b);
}
}
distances
}
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;
}
galaxies.push(Galaxy { x, y });
}
}
galaxies
}
fn expand(galaxies: &mut Vec<Galaxy>) {}
struct Galaxy {
x: usize,
y: usize,
}
impl Galaxy {
fn distance(&self, other: &Galaxy) -> usize {
let dx = (other.x as isize - self.x as isize).abs();
let dy = (other.y as isize - self.y as isize).abs();
(dx + dy) as usize
}
fn compare(&self, other: &Galaxy) -> bool {
self.x == other.x && self.y == other.y
}
}
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".")
}

View File

@ -1,14 +1,19 @@
use std::time::Instant; use std::time::Instant;
mod common; mod common;
mod day09; mod day09;
mod day11;
fn main() { fn main() {
println!("Executing main entrypoint . . . "); println!("Executing main entrypoint . . . ");
let now = Instant::now(); let now = Instant::now();
// day09::part_one(); // day09::part_one();
day09::part_two(); // day09::part_two();
day11::part_one();
// day11::part_two();
let elapsed = now.elapsed(); let elapsed = now.elapsed();
println!("Execution in {:?}.", elapsed); println!("Execution in {:?}.", elapsed);