Skip to content

Commit

Permalink
Day 23! πŸ‘–πŸ„πŸŒ²πŸ›πŸŽ‰πŸ”₯
Browse files Browse the repository at this point in the history
  • Loading branch information
bozdoz committed Jan 2, 2025
1 parent fc69ee6 commit fa7d6b3
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 1 deletion.
33 changes: 33 additions & 0 deletions BLOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# What Am I Learning Each Day?

### Day 23

**Difficulty: 3/10 β˜…β˜…β˜…β˜†β˜†β˜†β˜†β˜†β˜†β˜†**

**Time: ~2 hr**

**Run Time: ~43ms**

I did all of this in [Rust Playground](https://play.rust-lang.org/).

This seemed straight forward: linking all the networks together, spotting intersections, sorting, and counting them.

I think I'm starting to understand when to use `into_iter` as opposed to `iter`: if you need the values to last longer, you need to own/copy/clone/take them with `into` as opposed to iterating references with `iter` (might not live long enough).

This was my first time using `intersection`:

```rust
let mut biggest: HashMap<&&str, HashSet<&&str>> = HashMap::new();

// check for intersections in all queues
for (a, set) in biggest.iter() {
for b in set {
if a == b {
continue;
}
let other = &biggest[b];
let mut intersection = set.intersection(other).collect::<Vec<_>>();

intersection.sort();
```

So this gets the longest list of the links of each network and intersects them with every other to get a count and compare to find the largest list of linked networks (made sense in my head at the time, though I was worried/lucky that my logic was a bit naive).

### Day 22

**Difficulty: 6/10 β˜…β˜…β˜…β˜…β˜…β˜…β˜†β˜†β˜†β˜†**
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ New: `./createDay.sh 02`
- [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)
- [Rust Playground](https://play.rust-lang.org/)
- [Codeium](https://codeium.com/)

- [ChatGPT](https://chatgpt.com/)
7 changes: 7 additions & 0 deletions day-23/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "day-23"
version = "0.1.0"
edition = "2021"

[dependencies]
lib = { version = "0.1.0", path = "../lib" }
32 changes: 32 additions & 0 deletions day-23/src/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn
165 changes: 165 additions & 0 deletions day-23/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#![allow(unused)]

use std::{ collections::{ HashMap, HashSet }, fs, time::Instant };
use lib::get_part;

fn get_networks(data: &str) -> HashMap<&str, HashSet<&str>> {
let mut map: HashMap<&str, HashSet<&str>> = HashMap::new();

for line in data.lines() {
let (a, b) = line.split_once("-").unwrap();

map.entry(a)
.and_modify(|x| {
x.insert(b);
})
.or_insert_with(|| { HashSet::from([b]) });

map.entry(b)
.and_modify(|x| {
x.insert(a);
})
.or_insert_with(|| { HashSet::from([a]) });
}

map
}

fn part_one(networks: &HashMap<&str, HashSet<&str>>) -> usize {
let mut trios = HashSet::new();

for (a, a_set) in networks.iter() {
// println!("{a} {a_set:?}");

for b in a_set.iter() {
for c in networks[b].iter() {
if a_set.contains(c) {
let mut nets = [a, b, c];
nets.sort();

trios.insert(nets);
}
}
}
}

trios
.iter()
.filter(|x| {
for a in x.iter() {
if a.starts_with('t') {
return true;
}
}

false
})
.count()
}

fn part_two(networks: &HashMap<&str, HashSet<&str>>) -> String {
let mut biggest = HashMap::new();

for (a, a_set) in networks.iter() {
let mut visited = HashSet::from([a]);
let mut cur = HashSet::from([a]);
let mut queue = a_set.into_iter().collect::<Vec<_>>();

while let Some(v) = queue.pop() {
if visited.contains(&v) {
continue;
}

if networks[v].contains(a) {
// keep going
cur.insert(v);

for b in networks[v].iter() {
queue.push(b);
}
}

visited.insert(v);
}

biggest.insert(a, cur);
}

let mut counts = HashMap::new();

// check for intersections in all queues
for (a, set) in biggest.iter() {
for b in set {
if a == b {
continue;
}
let other = &biggest[b];
let mut intersection = set.intersection(other).collect::<Vec<_>>();

intersection.sort();

counts
.entry(intersection)
.and_modify(|x| {
*x += 1;
})
.or_insert(1);
}
}

let mut biggest = (0, vec![]);

for (k, v) in counts {
if v > biggest.0 {
biggest = (v, k);
}
}

biggest.1
.into_iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(",")
}

fn main() {
let (one, two) = get_part();
let start = Instant::now();
let data = fs::read_to_string("./src/input.txt").unwrap();
let networks = get_networks(&data);

if one {
let now = Instant::now();
let ans = part_one(&networks);
println!("Part one: {:?} {:?}", ans, now.elapsed());
}

if two {
let now = Instant::now();
let ans = part_two(&networks);
println!("Part two: {} {:?}", ans, now.elapsed());
}

println!("Time: {:?}", start.elapsed())
}

#[cfg(test)]
mod tests {
use super::*;

const EXAMPLE: &str = include_str!("./example.txt");

#[test]
fn test_part_one() {
let ans = part_one(&get_networks(EXAMPLE));

assert_eq!(ans, 7);
}

#[test]
fn test_part_two() {
let ans = part_two(&get_networks(EXAMPLE));

assert_eq!(ans, "co,de,ka,ta");
}
}

0 comments on commit fa7d6b3

Please sign in to comment.