This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rs
145 lines (108 loc) · 3.37 KB
/
08.rs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::{str::FromStr, collections::HashMap};
use num::integer::lcm;
advent_of_code::solution!(8);
#[derive(Copy, Clone)]
enum Direction {
Left,
Right,
}
impl Into<Direction> for char {
fn into(self) -> Direction {
match self {
'L' => Direction::Left,
_ => Direction::Right,
}
}
}
#[derive(Clone)]
struct Node {
id: String,
left: String,
right: String,
}
impl Node {
fn get_next(&self, dir: Direction) -> String {
match dir {
Direction::Left => self.left.to_owned(),
Direction::Right => self.right.to_owned(),
}
}
}
#[derive(Debug, PartialEq, Eq)]
struct ParseNodeError;
impl FromStr for Node {
type Err = ParseNodeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.split(" = ").collect();
let id = parts[0].to_string();
let mut dirs: String = parts[1].to_string();
dirs.pop();
dirs = dirs[1..].to_string();
let parts2: Vec<&str> = dirs.split(", ").collect();
let left = parts2[0].to_string();
let right = parts2[1].to_string();
Ok(Node { id, left, right })
}
}
fn parse(input: &str) -> (Vec<Direction>, HashMap<String, Node>) {
let mut lines = input.lines();
let dirs: Vec<Direction> = lines.next()
.unwrap()
.chars()
.map(|c| c.into())
.collect();
lines.next();
let nodes: Vec<Node> = lines.map(|l| l.parse().unwrap()).collect();
let mut graph: HashMap<String, Node> = HashMap::new();
for n in nodes.iter() {
graph.insert(n.id.clone(), n.to_owned());
};
(dirs, graph)
}
fn count_steps(graph: &HashMap<String, Node>, directions: &Vec<Direction>, start: &str, single: bool) -> u64 {
let mut dirs = directions.iter().cycle();
let mut k: String = start.to_string();
let mut steps = 0;
while let Some(node) = graph.get(&k) {
if single {
if node.id == "ZZZ" { break; }
} else {
if node.id.ends_with("Z") { break; }
}
let curr_dir = dirs.next().unwrap();
k = node.get_next(*curr_dir);
steps += 1;
};
steps
}
pub fn part_one(input: &str) -> Option<u64> {
let (directions, graph) = parse(input);
Some(count_steps(&graph, &directions, "AAA", true))
}
pub fn part_two(input: &str) -> Option<u64> {
let (directions, graph) = parse(input);
let starting_nodes: Vec<String> = graph.keys()
.filter(|k| k.ends_with("A"))
.map(|s| s.to_owned())
.collect();
let mut steps = vec![];
for node in starting_nodes {
steps.push(count_steps(&graph, &directions, node.as_str(), false));
}
let lcm = steps.iter().fold(1, |s, acc| lcm(s, *acc));
Some(lcm)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file_part("examples", DAY, 1));
assert_eq!(result, Some(6));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part("examples", DAY, 2));
assert_eq!(result, Some(6));
}
}