Add day 9 + remove compiler warnings

bram
Bram 2025-12-11 08:44:59 +01:00
parent df6262af0e
commit 2eea8f113d
5 changed files with 119 additions and 15 deletions

View File

@ -23,14 +23,14 @@ pub fn answer(text: String) -> (usize, u32) {
// println!("Coord {c1} ({:?}) and coord {c2} ({:?}) are close together!", coords[c1], coords[c2]);
uf.union(c1, c2);
}
println!("{:?}", uf);
// println!("{:?}", uf);
// Collect sizes of equivalence classes
let mut groups: HashMap<usize, usize> = HashMap::new();
for x in uf.into_labeling().iter() {
*groups.entry(*x).or_insert(0) += 1;
}
println!("{:?}", groups);
// println!("{:?}", groups);
let mut equiv_class_sizes : Vec<isize> = groups
.values()
@ -38,7 +38,7 @@ pub fn answer(text: String) -> (usize, u32) {
.collect();
equiv_class_sizes.sort_unstable();
println!("{:?}", equiv_class_sizes);
// println!("{:?}", equiv_class_sizes);
( (equiv_class_sizes[0] * equiv_class_sizes[1] * equiv_class_sizes[2]).abs() as usize
// 87048 too high

110
bram/rust/src/day_09/mod.rs Normal file
View File

@ -0,0 +1,110 @@
use itertools::Itertools;
use crate::utils;
pub fn answer(text: String) -> (u64, u64) {
let mut coords: Vec<Coord> = text
.trim()
.split_whitespace()
.filter_map(Coord::from)
.collect();
if let Some((idx, _)) = coords.iter()
.enumerate()
.min_by_key(|(_, c)| c.x)
{
coords.rotate_left(idx);
}
( coords.iter()
.combinations(2)
.map(|items| items[0].square_to(items[1]))
.max()
.unwrap_or(0)
, 0
// , coords.iter()
// .combinations(2)
// .map(|items| ( items[0], items[1], items[0].square_to(items[1])))
// .filter(|(c1, c2, _)| !overlaps_with(&coords, *c1, *c2))
// .map(|(_, _, size)| size)
// .max()
// .unwrap_or(0)
// 4638696212 too high
)
}
struct Coord { x: u64, y: u64 }
impl Coord {
fn from(s: &str) -> Option<Coord> {
let (x, y) = s.split_once(",")?;
Some(Coord { x: utils::str_to_u64(x)?, y: utils::str_to_u64(y)? })
}
fn square_to(&self, other: &Coord) -> u64 {
(self.x.abs_diff(other.x) + 1) * (self.y.abs_diff(other.y) + 1)
}
}
// enum Direction { NegX, NegY, PosX, PosY }
// fn overlaps_with(coords: &Vec<Coord>, c1: &Coord, c2: &Coord) -> bool {
// let head: &Coord = coords.first().unwrap();
// let mut a: &Coord;
// let mut b: &Coord = head;
// let mut d: Direction = Direction::PosY;
// let (x1, x2) = (c1.x.min(c2.x), c1.x.max(c2.x));
// let (y1, y2) = (c1.y.min(c2.y), c1.y.max(c2.y));
// for c in coords.iter().chain(vec![head].into_iter()) {
// // d = match d {
// // Direction::NegX => {
// // // ^
// // // |
// // // b-----c
// // // |
// // // <--|
// // // |
// // // a
// // // a
// // // |
// // // <--|
// // // |
// // // b-----c
// // // |
// // // v
// // // c-----b
// // // | |
// // // v |
// // // <-|
// // // |
// // // a
// // // a
// // // |
// // // <-|
// // // ^ |
// // // | |
// // // c-----b
// // },
// // };
// (a, b) = (b, c);
// if x1 < a.x && a.x < x2 && y1 < a.y && a.y < y2 {
// return true;
// }
// if x1 < b.x && b.x < x2 && y1 < b.y && b.y < y2 {
// return true;
// }
// }
// // Currently couldn't think of any other ways
// // that the square could overlap with the edge
// false
// }

View File

@ -1,11 +1,7 @@
use crate::utils;
mod test_machine;
mod machine;
use itertools::Itertools;
use machine::Machine;
use test_machine::TestMachine;
pub fn answer(text: String) -> ( u32, u32 ) {
let machines: Vec<Machine> = text

View File

@ -1,7 +1,5 @@
use std::collections::HashMap;
use crate::utils;
enum PathSize { Looping, Fixed(u64) }
pub fn answer(text: String) -> ( u64, u64 ) {

View File

@ -28,9 +28,9 @@ pub fn str_to_i16(s : &str) -> Option<i16> {
s.trim().to_string().parse::<i16>().ok()
}
pub fn str_to_i64(s : &str) -> Option<i64> {
s.trim().to_string().parse::<i64>().ok()
}
// pub fn str_to_i64(s : &str) -> Option<i64> {
// s.trim().to_string().parse::<i64>().ok()
// }
// pub fn str_to_i32(s : &str) -> Option<i32> {
// s.trim().to_string().parse::<i32>().ok()
@ -44,9 +44,9 @@ pub fn str_to_i64(s : &str) -> Option<i64> {
// s.trim().to_string().parse::<u16>().ok()
// }
pub fn str_to_u32(s : &str) -> Option<u32> {
s.trim().to_string().parse::<u32>().ok()
}
// pub fn str_to_u32(s : &str) -> Option<u32> {
// s.trim().to_string().parse::<u32>().ok()
// }
pub fn str_to_u64(s : &str) -> Option<u64> {
s.trim().to_string().parse::<u64>().ok()