Solutions for 2022, as well as 2015-2018 and 2019 up to day 11

This commit is contained in:
Chris Alge 2023-03-12 15:20:02 +01:00
commit 1895197c49
722 changed files with 375457 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "day12-digital_plumber"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,59 @@
\--- Day 12: Digital Plumber ---
----------
Walking along the memory banks of the stream, you find a small village that is experiencing a little confusion: some programs can't communicate with each other.
Programs in this village communicate using a fixed system of *pipes*. Messages are passed between programs using these pipes, but most programs aren't connected to each other directly. Instead, programs pass messages between each other until the message reaches the intended recipient.
For some reason, though, some of these messages aren't ever reaching their intended recipient, and the programs suspect that some pipes are missing. They would like you to investigate.
You walk through the village and record the ID of each program and the IDs with which it can communicate directly (your puzzle input). Each program has one or more programs with which it can communicate, and these pipes are bidirectional; if `8` says it can communicate with `11`, then `11` will say it can communicate with `8`.
You need to figure out how many programs are in the group that contains program ID `0`.
For example, suppose you go door-to-door like a travelling salesman and record the following list:
```
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5
```
In this example, the following programs are in the group that contains program ID `0`:
* Program `0` by definition.
* Program `2`, directly connected to program `0`.
* Program `3` via program `2`.
* Program `4` via program `2`.
* Program `5` via programs `6`, then `4`, then `2`.
* Program `6` via programs `4`, then `2`.
Therefore, a total of `6` programs are in this group; all but program `1`, which has a pipe that connects it to itself.
*How many programs* are in the group that contains program ID `0`?
Your puzzle answer was `169`.
\--- Part Two ---
----------
There are more programs than just the ones in the group containing program ID `0`. The rest of them have no way of reaching that group, and still might have no way of reaching each other.
A *group* is a collection of programs that can all communicate via pipes either directly or indirectly. The programs you identified just a moment ago are all part of the same group. Now, they would like you to determine the total number of groups.
In the example above, there were `2` groups: one consisting of programs `0,2,3,4,5,6`, and the other consisting solely of program `1`.
*How many groups are there* in total?
Your puzzle answer was `179`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, all that is left is for you to [admire your Advent calendar](/2017).
If you still want to see it, you can [get your puzzle input](12/input).

View file

@ -0,0 +1,59 @@
pub fn run(input: &str) -> (usize, usize) {
let mut network: Vec<_> = input.lines().map(parse_connections).collect();
let root_network = get_clique(&mut network, 0);
let first = root_network.len();
let mut second = 1;
while let Some(idx) = network.iter().position(|v| !v.is_empty()) {
second += 1;
_ = get_clique(&mut network, idx);
}
(first, second)
}
fn parse_connections(line: &str) -> Vec<usize> {
let (_id, rest) = line.split_once(" <-> ").unwrap();
rest.split(", ").map(|i| i.parse::<usize>().unwrap()).collect()
}
fn get_clique(network: &mut [Vec<usize>], start: usize) -> Vec<usize> {
let mut clique = Vec::from([start]);
let mut last_step = Vec::from([start]);
while !last_step.is_empty() {
let mut new_this_step = Vec::new();
for id in &last_step {
for neighbour in network[*id].iter() {
if !new_this_step.contains(neighbour) && clique.binary_search(neighbour).is_err() {
new_this_step.push(*neighbour);
}
}
network[*id] = Vec::new();
}
clique.append(&mut new_this_step.to_vec());
clique.sort();
std::mem::swap(&mut new_this_step, &mut last_step);
}
clique
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
}
#[test]
fn test_sample() {
let sample_input = read_file("tests/sample_input");
assert_eq!(run(&sample_input), (6, 2));
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), (169, 179));
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,7 @@
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5