summaryrefslogtreecommitdiff
path: root/2021/day19/src
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2022-06-23 19:06:22 +0100
committerFederico Igne <git@federicoigne.com>2022-06-23 19:06:22 +0100
commit120d53c0ff20574866ce10fa0538fb8b0dd2ef82 (patch)
tree4ac370f9b525c713635a6af09530f20758e91fb7 /2021/day19/src
parent5d99706992101d684c9920475e2ecaab65ba21ba (diff)
downloadaoc-120d53c0ff20574866ce10fa0538fb8b0dd2ef82.tar.gz
aoc-120d53c0ff20574866ce10fa0538fb8b0dd2ef82.zip
Reorganize repository structure
Diffstat (limited to '2021/day19/src')
-rw-r--r--2021/day19/src/main.rs81
1 files changed, 81 insertions, 0 deletions
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 @@
1use std::fs;
2use std::path::Path;
3use std::collections::HashSet;
4
5type Point = (i32,i32,i32);
6struct Scanner {
7 beacons: Vec<Point>,
8 diffs: HashSet<Point>
9}
10
11fn main() {
12 let input = Path::new("resources").join("input.txt");
13 let content = fs::read_to_string(input).expect("Unable to read from input");
14 let scanners: Vec<Scanner> = content.split("\n\n").map(|s| parse_scanner(s)).collect();
15 let perms: Vec<Box<dyn Fn((i32,i32,i32)) -> (i32,i32,i32)>> = vec![
16 Box::new(|(x,y,z)| (x,y,z)),
17 Box::new(|(x,y,z)| (-y,x,z)),
18 Box::new(|(x,y,z)| (-x,-y,z)),
19 Box::new(|(x,y,z)| (y,-x,z)),
20 Box::new(|(x,y,z)| (-x,y,-z)),
21 Box::new(|(x,y,z)| (y,x,-z)),
22 Box::new(|(x,y,z)| (x,-y,-z)),
23 Box::new(|(x,y,z)| (-y,-x,-z)),
24 Box::new(|(x,y,z)| (-z,y,x)),
25 Box::new(|(x,y,z)| (-y,-z,x)),
26 Box::new(|(x,y,z)| (z,-y,x)),
27 Box::new(|(x,y,z)| (y,z,x)),
28 Box::new(|(x,y,z)| (z,y,-x)),
29 Box::new(|(x,y,z)| (y,-z,-x)),
30 Box::new(|(x,y,z)| (-z,-y,-x)),
31 Box::new(|(x,y,z)| (-y,z,-x)),
32 Box::new(|(x,y,z)| (x,-z,y)),
33 Box::new(|(x,y,z)| (z,x,y)),
34 Box::new(|(x,y,z)| (-x,z,y)),
35 Box::new(|(x,y,z)| (-z,-x,y)),
36 Box::new(|(x,y,z)| (x,z,-y)),
37 Box::new(|(x,y,z)| (-z,x,-y)),
38 Box::new(|(x,y,z)| (-x,-z,-y)),
39 Box::new(|(x,y,z)| (z,-x,-y))
40 ];
41 for i in 0..scanners.len() {
42 for j in i+1..scanners.len() {
43 for perm in perms.iter() {
44 let permset = &scanners[j].diffs.iter().map(|&p| perm(p)).collect();
45 let common: HashSet<_> = scanners[i].diffs.intersection(&permset).collect();
46 if common.len() > 11 {
47 println!("At least 12 points in common for scanners {}-{}", i, j)
48 }
49 }
50 }
51 }
52 let mut beacons = HashSet::new();
53}
54
55fn test(s1: &Scanner, s2: &Scanner, perm: &dyn Fn((i32,i32,i32)) -> (i32,i32,i32)) {
56 let bs1 = s1.beacons;
57 let bs2: Vec<_> = s2.beacons.iter().map(|&p| perm(p)).collect();
58 //let (p1, p2) = bs1.iter().map(|b1| bs2.iter().map(|b2| (b1,b2))).find(|((x1,y1,z1),(x2,y2,z2))| )<++>;
59}
60
61fn parse_scanner(s: &str) -> Scanner {
62 let mut lines = s.lines();
63 let _idx: u8 = lines.next().unwrap().split_whitespace().nth(2).unwrap().parse().unwrap();
64 let beacons: Vec<Point> = lines.map(|l| {
65 let mut coords = l.split(",");
66 let x = coords.next().unwrap().parse().unwrap();
67 let y = coords.next().unwrap().parse().unwrap();
68 let z = coords.next().unwrap().parse().unwrap();
69 (x,y,z)
70 }).collect();
71 let mut diffs = HashSet::new();
72 for i in 1..beacons.len() {
73 for j in 0..i {
74 let (x1,y1,z1) = beacons[i];
75 let (x2,y2,z2) = beacons[j];
76 diffs.insert((x1-x2,y1-y2,z1-z2));
77 }
78 }
79 Scanner { beacons, diffs }
80}
81