This commit is contained in:
BlackDemonFire 2023-12-12 08:24:46 +01:00
parent 1d338eea9d
commit 38ccaeef6e
Signed by: lucy
SSH key fingerprint: SHA256:5NG0Um+vQQHuWkYn+Vd84YFm10edFBJiOIpOuHjl8UE
8 changed files with 146 additions and 0 deletions

8
Cargo.lock generated
View file

@ -57,3 +57,11 @@ version = "0.1.0"
[[package]]
name = "d07t2"
version = "0.1.0"
[[package]]
name = "d08t1"
version = "0.1.0"
[[package]]
name = "d08t2"
version = "0.1.0"

View file

@ -16,4 +16,6 @@ members = [
"d06t2",
"d07t1",
"d07t2",
"d08t1",
"d08t2",
]

8
d08t1/Cargo.toml Normal file
View file

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

5
d08t1/demo_input.txt Normal file
View file

@ -0,0 +1,5 @@
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)

40
d08t1/src/main.rs Normal file
View file

@ -0,0 +1,40 @@
use std::{collections::HashMap, fs::read_to_string};
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d08t1: {}", d08t1(&input));
}
#[must_use]
pub fn d08t1(input: &str) -> usize {
let mut lines = input.lines();
let mut directions = lines.next().unwrap().chars().cycle();
let mut locations: HashMap<String, (String, String)> = HashMap::new();
for line in lines.skip(1) {
let (source, destination) = line.split_once(" = ").unwrap();
let destination = &destination.replace(['(', ')'], "");
let (left, right) = destination.split_once(", ").unwrap();
locations.insert(source.to_string(), (left.to_string(), right.to_string()));
}
let mut result = 0;
let mut current_position = "AAA";
while current_position != "ZZZ" {
result += 1;
current_position = if directions.next().unwrap() == 'L' {
&locations.get(current_position).unwrap().0
} else {
&locations.get(current_position).unwrap().1
};
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d08t1() {
let input = include_str!("../demo_input.txt");
assert_eq!(d08t1(input), 6);
}
}

8
d08t2/Cargo.toml Normal file
View file

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

10
d08t2/demo_input.txt Normal file
View file

@ -0,0 +1,10 @@
LR
11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)

65
d08t2/src/main.rs Normal file
View file

@ -0,0 +1,65 @@
use std::{collections::HashMap, fs::read_to_string};
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d08t2: {}", d08t2(&input));
}
pub fn d08t2(input: &str) -> usize {
let mut lines = input.lines();
let directions = lines.next().unwrap().chars().cycle();
let mut locations: HashMap<String, (String, String)> = HashMap::new();
for line in lines.skip(1) {
let (source, destination) = line.split_once(" = ").unwrap();
let destination = &destination.replace(['(', ')'], "");
let (left, right) = destination.split_once(", ").unwrap();
locations.insert(source.to_string(), (left.to_string(), right.to_string()));
}
let keys = locations.keys();
keys.filter(|k| k.ends_with('A'))
.map(Clone::clone)
.map(|start| {
let mut dirs = directions.clone();
let mut res = 0;
let mut current_position = start.clone();
while !current_position.ends_with('Z') {
res += 1;
current_position = if dirs.next().unwrap() == 'L' {
&locations.get(&current_position).unwrap().0
} else {
&locations.get(&current_position).unwrap().1
}
.clone();
}
res
})
.reduce(lcm)
.unwrap()
}
fn lcm(first: usize, second: usize) -> usize {
first * second / gcd(first, second)
}
fn gcd(first: usize, second: usize) -> usize {
let mut max = first.max(second);
let mut min = first.min(second);
loop {
let res = max % min;
if res == 0 {
return min;
}
max = min;
min = res;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d08t2() {
let input = include_str!("../demo_input.txt");
assert_eq!(d08t2(input), 6);
}
}