mirror of
https://github.com/kristoferssolo/Advent-of-Code.git
synced 2025-12-31 05:32:31 +00:00
Finish day-08 part-1
This commit is contained in:
parent
6cac42e2df
commit
43c57b58df
31
2025/Cargo.lock
generated
31
2025/Cargo.lock
generated
@ -205,6 +205,22 @@ dependencies = [
|
|||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day-08"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"divan",
|
||||||
|
"glam",
|
||||||
|
"itertools",
|
||||||
|
"miette",
|
||||||
|
"nom",
|
||||||
|
"rstest",
|
||||||
|
"test-log",
|
||||||
|
"thiserror",
|
||||||
|
"tracing",
|
||||||
|
"tracing-subscriber",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "divan"
|
name = "divan"
|
||||||
version = "0.1.21"
|
version = "0.1.21"
|
||||||
@ -301,6 +317,15 @@ version = "0.32.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
|
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "glam"
|
||||||
|
version = "0.30.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bd47b05dddf0005d850e5644cae7f2b14ac3df487979dbfff3b56f20b1a6ae46"
|
||||||
|
dependencies = [
|
||||||
|
"mint",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "glob"
|
name = "glob"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
@ -416,6 +441,12 @@ dependencies = [
|
|||||||
"adler2",
|
"adler2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mint"
|
||||||
|
version = "0.5.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e53debba6bda7a793e5f99b8dacf19e626084f525f7829104ba9898f367d85ff"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nom"
|
name = "nom"
|
||||||
version = "8.0.0"
|
version = "8.0.0"
|
||||||
|
|||||||
@ -8,6 +8,7 @@ members = [
|
|||||||
"day-05",
|
"day-05",
|
||||||
"day-06",
|
"day-06",
|
||||||
"day-07",
|
"day-07",
|
||||||
|
"day-08",
|
||||||
]
|
]
|
||||||
default-members = ["day-*"]
|
default-members = ["day-*"]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|||||||
28
2025/day-08/Cargo.toml
Normal file
28
2025/day-08/Cargo.toml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
[package]
|
||||||
|
name = "day-08"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
itertools.workspace = true
|
||||||
|
nom.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
tracing-subscriber.workspace = true
|
||||||
|
miette.workspace = true
|
||||||
|
thiserror.workspace = true
|
||||||
|
glam = { workspace = true, features = ["mint"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
divan.workspace = true
|
||||||
|
rstest.workspace = true
|
||||||
|
test-log.workspace = true
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "day-08-bench"
|
||||||
|
path = "benches/benchmarks.rs"
|
||||||
|
harness = false
|
||||||
|
|
||||||
|
[lints]
|
||||||
|
workspace = true
|
||||||
21
2025/day-08/benches/benchmarks.rs
Normal file
21
2025/day-08/benches/benchmarks.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
use day_08::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
divan::main();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[divan::bench]
|
||||||
|
fn part1() {
|
||||||
|
part1::process(divan::black_box(include_str!(
|
||||||
|
"../input1.txt",
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[divan::bench]
|
||||||
|
fn part2() {
|
||||||
|
part2::process(divan::black_box(include_str!(
|
||||||
|
"../input2.txt",
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
12
2025/day-08/src/bin/part1.rs
Normal file
12
2025/day-08/src/bin/part1.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use day_08::part1::process;
|
||||||
|
use miette::{Context, Result};
|
||||||
|
|
||||||
|
#[tracing::instrument]
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
let file = include_str!("../../input1.txt");
|
||||||
|
let result = process(file, 1000).context("process part 1")?;
|
||||||
|
println!("{result}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
12
2025/day-08/src/bin/part2.rs
Normal file
12
2025/day-08/src/bin/part2.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use day_08::part2::process;
|
||||||
|
use miette::{Context, Result};
|
||||||
|
|
||||||
|
#[tracing::instrument]
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
let file = include_str!("../../input2.txt");
|
||||||
|
let result = process(file).context("process part 2")?;
|
||||||
|
println!("{result}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
2
2025/day-08/src/lib.rs
Normal file
2
2025/day-08/src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod part1;
|
||||||
|
pub mod part2;
|
||||||
142
2025/day-08/src/part1.rs
Normal file
142
2025/day-08/src/part1.rs
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
use glam::Vec3;
|
||||||
|
use miette::miette;
|
||||||
|
use std::{collections::HashSet, str::FromStr};
|
||||||
|
|
||||||
|
fn vec3_from_str(s: &str) -> Result<Vec3, String> {
|
||||||
|
let coords = s
|
||||||
|
.trim()
|
||||||
|
.split(',')
|
||||||
|
.map(|x| {
|
||||||
|
x.parse::<f32>()
|
||||||
|
.map_err(|_| "Invalid coordinate value".to_string())
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
if coords.len() == 3 {
|
||||||
|
let vec = Vec3::new(coords[0], coords[1], coords[2]);
|
||||||
|
return Ok(vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
Err("Expected exactly 3 coordinates".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct UnionFind {
|
||||||
|
parent: Vec<usize>,
|
||||||
|
size: Vec<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UnionFind {
|
||||||
|
fn new(n: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
parent: (0..n).collect(),
|
||||||
|
size: vec![1; n],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find(&mut self, x: usize) -> usize {
|
||||||
|
if self.parent[x] != x {
|
||||||
|
self.parent[x] = self.find(self.parent[x]);
|
||||||
|
}
|
||||||
|
self.parent[x]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn union(&mut self, mut x: usize, mut y: usize) {
|
||||||
|
x = self.find(x);
|
||||||
|
y = self.find(y);
|
||||||
|
if x == y {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if self.size[x] < self.size[y] {
|
||||||
|
(x, y) = (y, x);
|
||||||
|
}
|
||||||
|
self.parent[y] = x;
|
||||||
|
self.size[x] += self.size[y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct Coordinates(Vec<Vec3>);
|
||||||
|
|
||||||
|
impl Coordinates {
|
||||||
|
fn solve(&self, size: usize) -> usize {
|
||||||
|
let n = self.0.len();
|
||||||
|
|
||||||
|
let mut pairs = Vec::new();
|
||||||
|
for i in 0..n {
|
||||||
|
for j in (i + 1)..n {
|
||||||
|
let dist = self.0[i].distance(self.0[j]);
|
||||||
|
pairs.push((dist, i, j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pairs.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
|
||||||
|
|
||||||
|
let mut uf = UnionFind::new(n);
|
||||||
|
for (_, i, j) in pairs.iter().take(size) {
|
||||||
|
if uf.find(*i) != uf.find(*j) {
|
||||||
|
uf.union(*i, *j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut roots = HashSet::new();
|
||||||
|
for i in 0..n {
|
||||||
|
roots.insert(uf.find(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut sizes = roots.iter().map(|&root| uf.size[root]).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
sizes.sort_by(|a, b| b.cmp(a));
|
||||||
|
sizes[0] * sizes[1] * sizes[2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for Coordinates {
|
||||||
|
type Err = String;
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
let coords = s
|
||||||
|
.lines()
|
||||||
|
.map(vec3_from_str)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
Ok(Self(coords))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument]
|
||||||
|
#[allow(clippy::missing_panics_doc)]
|
||||||
|
#[allow(clippy::missing_errors_doc)]
|
||||||
|
pub fn process(input: &str, size: usize) -> miette::Result<usize> {
|
||||||
|
let coords = Coordinates::from_str(input).map_err(|e| miette!("{e}"))?;
|
||||||
|
Ok(coords.solve(size))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process() -> miette::Result<()> {
|
||||||
|
let input = "162,817,812
|
||||||
|
57,618,57
|
||||||
|
906,360,560
|
||||||
|
592,479,940
|
||||||
|
352,342,300
|
||||||
|
466,668,158
|
||||||
|
542,29,236
|
||||||
|
431,825,988
|
||||||
|
739,650,466
|
||||||
|
52,470,668
|
||||||
|
216,146,977
|
||||||
|
819,987,18
|
||||||
|
117,168,530
|
||||||
|
805,96,715
|
||||||
|
346,949,466
|
||||||
|
970,615,88
|
||||||
|
941,993,340
|
||||||
|
862,61,35
|
||||||
|
984,92,344
|
||||||
|
425,690,689";
|
||||||
|
let result = 40;
|
||||||
|
assert_eq!(process(input, 10)?, result);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
23
2025/day-08/src/part2.rs
Normal file
23
2025/day-08/src/part2.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use miette::Result;
|
||||||
|
|
||||||
|
#[tracing::instrument]
|
||||||
|
#[allow(clippy::missing_panics_doc)]
|
||||||
|
#[allow(clippy::missing_errors_doc)]
|
||||||
|
pub fn process(input: &str) -> Result<usize> {
|
||||||
|
todo!("day xx - part 2");
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process() -> Result<()> {
|
||||||
|
let input = "";
|
||||||
|
todo!("haven't built test yet");
|
||||||
|
let result = 0;
|
||||||
|
assert_eq!(process(input)?, result);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user