Skip to content

Commit

Permalink
feat: Day 25
Browse files Browse the repository at this point in the history
  • Loading branch information
rgoulais committed Dec 25, 2023
1 parent 1628340 commit 55dab35
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 22](./src/bin/22.rs) | `174.0ms` | `187.1ms` |
| [Day 23](./src/bin/23.rs) | `1.0ms` | `1.6ms` |
| [Day 24](./src/bin/24.rs) | `386.0µs` | `83.6µs` |
| [Day 25](./src/bin/25.rs) | `4.1ms` | `-` |

**Total: 815.63ms**
**Total: 819.73ms**
<!--- benchmarking table --->

---
Expand Down
13 changes: 13 additions & 0 deletions data/examples/25.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jqt: rhn xhk nvd
rsh: frs pzl lsr
xhk: hfx
cmg: qnr nvd lhk bvb
rhn: xhk bvb hfx
bvb: xhk hfx
pzl: lsr hfx nvd
qnr: nvd
ntq: jqt hfx bvb xhk
nvd: lhk
lsr: lhk
rzs: qnr cmg lsr rsh
frs: qnr lhk lsr
112 changes: 112 additions & 0 deletions src/bin/25.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 {
}

0 comments on commit 55dab35

Please sign in to comment.