Day 11
This commit is contained in:
parent
dd5792817e
commit
676cf34ea2
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -88,3 +88,11 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "d10t2"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "d11t1"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "d11t2"
|
||||
version = "0.1.0"
|
||||
|
|
|
@ -23,4 +23,6 @@ members = [
|
|||
"d09t2",
|
||||
"d10t1",
|
||||
"d10t2",
|
||||
"d11t1",
|
||||
"d11t2",
|
||||
]
|
||||
|
|
8
d11t1/Cargo.toml
Normal file
8
d11t1/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "d11t1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
10
d11t1/demo_input.txt
Normal file
10
d11t1/demo_input.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
..........
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
..........
|
||||
.......#..
|
||||
#...#.....
|
70
d11t1/src/main.rs
Normal file
70
d11t1/src/main.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
fn main() {
|
||||
let input = read_to_string("input.txt").unwrap();
|
||||
println!("d11t1: {}", d11t1(&input));
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Galaxy {
|
||||
col: usize,
|
||||
row: usize,
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn d11t1(input: &str) -> usize {
|
||||
let lines = input.lines();
|
||||
let width = lines.clone().next().unwrap().len();
|
||||
let mut expanded_lines = Vec::<String>::new();
|
||||
for line in lines {
|
||||
if line.chars().all(|c| c == '.') {
|
||||
expanded_lines.push(".".repeat(width));
|
||||
}
|
||||
expanded_lines.push(line.to_string());
|
||||
}
|
||||
let mut fields = vec![String::new(); expanded_lines.len()];
|
||||
let expanded_lines = expanded_lines
|
||||
.into_iter()
|
||||
.map(|l| l.chars().collect::<Vec<char>>())
|
||||
.collect::<Vec<_>>();
|
||||
for col in 0..width {
|
||||
if expanded_lines.iter().all(|row| row[col] == '.') {
|
||||
for (row_idx, row) in expanded_lines.iter().enumerate() {
|
||||
fields[row_idx].extend(['.', row[col]]);
|
||||
}
|
||||
} else {
|
||||
for (row_idx, row) in expanded_lines.iter().enumerate() {
|
||||
fields[row_idx].extend([row[col]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
let galaxies = fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(row, line)| {
|
||||
line.chars()
|
||||
.enumerate()
|
||||
.filter(|&(_col, c)| c == '#')
|
||||
.map(move |(col, _)| Galaxy { col, row })
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let mut res = 0;
|
||||
for (idx, galaxy) in galaxies.clone().into_iter().enumerate() {
|
||||
res += galaxies
|
||||
.iter()
|
||||
.skip(idx + 1)
|
||||
.map(|g| g.col.abs_diff(galaxy.col) + g.row.abs_diff(galaxy.row))
|
||||
.sum::<usize>();
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_d11t1() {
|
||||
let input = include_str!("../demo_input.txt");
|
||||
assert_eq!(d11t1(input), 374);
|
||||
}
|
||||
}
|
8
d11t2/Cargo.toml
Normal file
8
d11t2/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "d11t2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
10
d11t2/demo_input.txt
Normal file
10
d11t2/demo_input.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
..........
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
..........
|
||||
.......#..
|
||||
#...#.....
|
75
d11t2/src/main.rs
Normal file
75
d11t2/src/main.rs
Normal file
|
@ -0,0 +1,75 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
fn main() {
|
||||
let input = read_to_string("input.txt").unwrap();
|
||||
println!("d11t2: {}", d11t2(&input, 1_000_000));
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Galaxy {
|
||||
col: usize,
|
||||
row: usize,
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn d11t2(input: &str, factor: usize) -> usize {
|
||||
let lines = input.lines();
|
||||
let width = lines.clone().next().unwrap().len();
|
||||
|
||||
let fields = lines
|
||||
.into_iter()
|
||||
.map(|l| l.chars().collect::<Vec<char>>())
|
||||
.collect::<Vec<_>>();
|
||||
let empty_rows = fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, row)| row.iter().all(|c| *c == '.'))
|
||||
.map(|(idx, _)| idx)
|
||||
.collect::<Vec<_>>();
|
||||
let empty_cols = (0..width)
|
||||
.filter(|&col| fields.iter().all(|row| row[col] == '.'))
|
||||
.collect::<Vec<_>>();
|
||||
let galaxies = fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(row, line)| {
|
||||
line.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_col, c)| *c == '#')
|
||||
.map(move |(col, _)| Galaxy { col, row })
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let get_distances = |(a, b): (Galaxy, Galaxy)| -> usize {
|
||||
a.col.abs_diff(b.col)
|
||||
+ a.row.abs_diff(b.row)
|
||||
+ (((a.col.min(b.col))..=(a.col.max(b.col)))
|
||||
.filter(|col| empty_cols.contains(col))
|
||||
.count()
|
||||
+ ((a.row.min(b.row))..=(a.row.max(b.row)))
|
||||
.filter(|row| empty_rows.contains(row))
|
||||
.count())
|
||||
* (factor - 1)
|
||||
};
|
||||
galaxies
|
||||
.clone()
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.flat_map(|(idx, galaxy)| galaxies.iter().skip(idx + 1).map(move |g| (galaxy, *g)))
|
||||
.map(get_distances)
|
||||
.sum::<usize>()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_d11t2_10() {
|
||||
let input = include_str!("../demo_input.txt");
|
||||
assert_eq!(d11t2(input, 10), 1030);
|
||||
}
|
||||
#[test]
|
||||
fn test_d11t2_100() {
|
||||
let input = include_str!("../demo_input.txt");
|
||||
assert_eq!(d11t2(input, 100), 8410);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue