From 120d53c0ff20574866ce10fa0538fb8b0dd2ef82 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Thu, 23 Jun 2022 19:06:22 +0100 Subject: Reorganize repository structure --- 2021/day19/src/main.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 2021/day19/src/main.rs (limited to '2021/day19/src') diff --git a/2021/day19/src/main.rs b/2021/day19/src/main.rs new file mode 100644 index 0000000..48882d2 --- /dev/null +++ b/2021/day19/src/main.rs @@ -0,0 +1,81 @@ +use std::fs; +use std::path::Path; +use std::collections::HashSet; + +type Point = (i32,i32,i32); +struct Scanner { + beacons: Vec, + diffs: HashSet +} + +fn main() { + let input = Path::new("resources").join("input.txt"); + let content = fs::read_to_string(input).expect("Unable to read from input"); + let scanners: Vec = content.split("\n\n").map(|s| parse_scanner(s)).collect(); + let perms: Vec (i32,i32,i32)>> = vec![ + Box::new(|(x,y,z)| (x,y,z)), + Box::new(|(x,y,z)| (-y,x,z)), + Box::new(|(x,y,z)| (-x,-y,z)), + Box::new(|(x,y,z)| (y,-x,z)), + Box::new(|(x,y,z)| (-x,y,-z)), + Box::new(|(x,y,z)| (y,x,-z)), + Box::new(|(x,y,z)| (x,-y,-z)), + Box::new(|(x,y,z)| (-y,-x,-z)), + Box::new(|(x,y,z)| (-z,y,x)), + Box::new(|(x,y,z)| (-y,-z,x)), + Box::new(|(x,y,z)| (z,-y,x)), + Box::new(|(x,y,z)| (y,z,x)), + Box::new(|(x,y,z)| (z,y,-x)), + Box::new(|(x,y,z)| (y,-z,-x)), + Box::new(|(x,y,z)| (-z,-y,-x)), + Box::new(|(x,y,z)| (-y,z,-x)), + Box::new(|(x,y,z)| (x,-z,y)), + Box::new(|(x,y,z)| (z,x,y)), + Box::new(|(x,y,z)| (-x,z,y)), + Box::new(|(x,y,z)| (-z,-x,y)), + Box::new(|(x,y,z)| (x,z,-y)), + Box::new(|(x,y,z)| (-z,x,-y)), + Box::new(|(x,y,z)| (-x,-z,-y)), + Box::new(|(x,y,z)| (z,-x,-y)) + ]; + for i in 0..scanners.len() { + for j in i+1..scanners.len() { + for perm in perms.iter() { + let permset = &scanners[j].diffs.iter().map(|&p| perm(p)).collect(); + let common: HashSet<_> = scanners[i].diffs.intersection(&permset).collect(); + if common.len() > 11 { + println!("At least 12 points in common for scanners {}-{}", i, j) + } + } + } + } + let mut beacons = HashSet::new(); +} + +fn test(s1: &Scanner, s2: &Scanner, perm: &dyn Fn((i32,i32,i32)) -> (i32,i32,i32)) { + let bs1 = s1.beacons; + let bs2: Vec<_> = s2.beacons.iter().map(|&p| perm(p)).collect(); + //let (p1, p2) = bs1.iter().map(|b1| bs2.iter().map(|b2| (b1,b2))).find(|((x1,y1,z1),(x2,y2,z2))| )<++>; +} + +fn parse_scanner(s: &str) -> Scanner { + let mut lines = s.lines(); + let _idx: u8 = lines.next().unwrap().split_whitespace().nth(2).unwrap().parse().unwrap(); + let beacons: Vec = lines.map(|l| { + let mut coords = l.split(","); + let x = coords.next().unwrap().parse().unwrap(); + let y = coords.next().unwrap().parse().unwrap(); + let z = coords.next().unwrap().parse().unwrap(); + (x,y,z) + }).collect(); + let mut diffs = HashSet::new(); + for i in 1..beacons.len() { + for j in 0..i { + let (x1,y1,z1) = beacons[i]; + let (x2,y2,z2) = beacons[j]; + diffs.insert((x1-x2,y1-y2,z1-z2)); + } + } + Scanner { beacons, diffs } +} + -- cgit v1.2.3