-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
168 lines (143 loc) · 3.68 KB
/
mod.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::prelude::*;
use std::error::Error;
pub fn part1() -> Result<i64, Box<dyn Error>>
{
let mut file = File::open("src/day8/input.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let lines: Vec<_> = contents.lines().collect();
let instructions: Vec<_> = lines[0].split("").filter(|c| c.len() > 0).collect();
let mut map: HashMap<&str, (String, String)> = HashMap::new();
for i in 2..lines.len()
{
let line = lines[i];
let key_split: Vec<_> = line.split(" = ").collect();
let key = key_split[0];
let to: Vec<_> = key_split[1]
.split(", ")
.map(|t| t.replace("(", "").replace(")", ""))
.collect();
map.insert(key, (to[0].clone(), to[1].clone()));
}
let mut current_node = "AAA";
let mut i = 0;
while current_node != "ZZZ"
{
for instruction in &instructions
{
i += 1;
match instruction
{
&"L" =>
{
current_node = &map[current_node].0;
}
&"R" =>
{
current_node = &map[current_node].1;
}
_ => {}
}
if current_node == "ZZZ"
{
break;
}
}
}
return Ok(i);
}
fn find_z(map: &HashMap<&str, (String, String)>, instructions: &Vec<&str>, node: &str) -> i64
{
let mut current_node = node;
let mut d = 0;
while !current_node.ends_with("Z")
{
for instruction in instructions
{
d += 1;
match instruction
{
&"L" =>
{
current_node = &map[current_node].0;
}
&"R" =>
{
current_node = &map[current_node].1;
}
_ => {}
}
if current_node.ends_with("Z")
{
break;
}
}
}
return d;
}
fn lcm(a: i64, b: i64) -> i64
{
let mut x = a;
let mut y = b;
if x < y
{
x = b;
y = a;
}
let mut remainder = x % y;
while remainder != 0
{
x = y;
y = remainder;
remainder = x % y;
}
return a * b / y;
}
pub fn part2() -> Result<i64, Box<dyn Error>>
{
let mut file = File::open("src/day8/input.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let lines: Vec<_> = contents.lines().collect();
let instructions: Vec<_> = lines[0].split("").filter(|c| c.len() > 0).collect();
let mut map: HashMap<&str, (String, String)> = HashMap::new();
let mut starters = HashSet::new();
for i in 2..lines.len()
{
let line = lines[i];
let key_split: Vec<_> = line.split(" = ").collect();
let key = key_split[0];
if key.ends_with("A")
{
starters.insert(key);
}
let to: Vec<_> = key_split[1]
.split(", ")
.map(|t| t.replace("(", "").replace(")", ""))
.collect();
map.insert(key, (to[0].clone(), to[1].clone()));
}
let d: Vec<_> = starters
.iter()
.map(|s| find_z(&map, &instructions, s))
.collect();
let mut common = d[0];
for i in 1..d.len()
{
common = lcm(common, d[i]);
}
return Ok(common as i64);
}
#[cfg(test)]
mod tests
{
use super::*;
#[test]
fn test_parts()
{
assert_eq!(part1().ok(), Some(2));
assert_eq!(part2().ok(), Some(2));
}
}