-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.rs
More file actions
112 lines (99 loc) · 3.29 KB
/
25.rs
File metadata and controls
112 lines (99 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::collections::{ HashSet};
advent_of_code::solution!(25);
use std::fs::File;
use std::io::BufWriter;
use std::io::prelude::*;
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
struct Wire {
c1: usize,
c2: usize,
}
struct Solver {
data: Vec<Wire>,
group1: HashSet<usize>,
group2: HashSet<usize>,
}
impl Solver {
fn new() -> Self {
Self {
data: Vec::new(),
group1: HashSet::new(),
group2: HashSet::new(),
}
}
fn solve_part1(&mut self, input: &str) -> std::io::Result<usize> {
let file = File::create("wires.dot")?;
let mut writer = BufWriter::new(file);
writeln!(writer, "graph wires {{")?;
for line in input.lines() {
let mut parts = line.split(":");
writeln!(writer, "{} -- {{ {} }}", parts.next().unwrap(), parts.next().unwrap())?;
}
writeln!(writer, "}}")?;
//println!("Wrote wires.dot .. Use neato -Tsvg wires.dot > wires.svg to generate a picture");
for line in input.lines() {
let mut parts = line.split_whitespace();
let lhs = parts.next().unwrap().strip_suffix(':').unwrap();
let c1 = usize::from_str_radix(lhs, 36).unwrap();
while let Some(c) = parts.next() {
let c2 = usize::from_str_radix(c, 36).unwrap();
let wire = Wire { c1, c2 };
self.data.push(wire);
}
}
//println!("Found {} wires", self.data.len());
self.add_to_group_1(usize::from_str_radix("lms", 36).unwrap());
self.add_to_group_2(usize::from_str_radix("tmc", 36).unwrap());
return Ok(self.group1.len() * self.group2.len());
}
fn add_to_group_1(&mut self, val: usize) {
if val == usize::from_str_radix("tmc", 36).unwrap() {
return;
}
if val == usize::from_str_radix("nhg", 36).unwrap() {
return;
}
if val == usize::from_str_radix("xnn", 36).unwrap() {
return;
}
self.group1.insert(val);
for i in 0..self.data.len() {
let wire = self.data[i];
if wire.c2 == val && !self.group1.contains(&wire.c1){
self.add_to_group_1(wire.c1);
} else if wire.c1 == val && !self.group1.contains(&wire.c2){
self.add_to_group_1(wire.c2);
}
}
}
fn add_to_group_2(&mut self, val: usize) {
if val == usize::from_str_radix("txf", 36).unwrap() {
return;
}
if val == usize::from_str_radix("jjn", 36).unwrap() {
return;
}
if val == usize::from_str_radix("lms", 36).unwrap() {
return;
}
self.group2.insert(val);
for i in 0..self.data.len() {
let wire = self.data[i];
if wire.c2 == val && !self.group2.contains(&wire.c1){
self.add_to_group_2(wire.c1);
} else if wire.c1 == val && !self.group2.contains(&wire.c2){
self.add_to_group_2(wire.c2);
}
}
}
}
pub fn part_one(input: &str) -> Option<usize> {
let mut solver = Solver::new();
Some(solver.solve_part1(input).unwrap())
}
pub fn part_two(_input: &str) -> Option<usize> {
None
}
#[cfg(test)]
mod tests {
}