Day 8 Part 1 complete
This commit is contained in:
+60
-4
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::Solution;
|
||||
|
||||
pub struct Day08 {}
|
||||
@@ -5,9 +7,37 @@ pub struct Day08 {}
|
||||
impl Solution for Day08 {
|
||||
fn part1(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new("Ready"))
|
||||
let instructions: String = input[0].clone();
|
||||
let mut map: HashMap<String, (String, String)> = HashMap::new();
|
||||
|
||||
self.populate_map(&input[2..input.len()], &mut map)?;
|
||||
|
||||
let mut count = 0u32;
|
||||
let mut current = map.get("AAA").unwrap();
|
||||
let mut found = false;
|
||||
|
||||
while !found {
|
||||
for i in instructions.chars() {
|
||||
count += 1;
|
||||
let next: &String;
|
||||
match i {
|
||||
'L' => next = ¤t.0,
|
||||
'R' => next = ¤t.1,
|
||||
_ => {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if next == "ZZZ" {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
current = map.get(next).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Box::new(count))
|
||||
}
|
||||
|
||||
fn part2(
|
||||
@@ -22,7 +52,33 @@ impl Solution for Day08 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Day08 {}
|
||||
impl Day08 {
|
||||
fn populate_map<'a>(
|
||||
&self,
|
||||
input: &[String],
|
||||
map: &mut HashMap<String, (String, String)>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
for p in input {
|
||||
let mut x = p.split('=');
|
||||
|
||||
let key = x.nth(0).unwrap().trim().to_owned();
|
||||
let (l, r) = x.nth(0).unwrap().split_at(6);
|
||||
map.insert(
|
||||
key,
|
||||
(
|
||||
l.to_owned()
|
||||
.replace(",", "")
|
||||
.replace("(", "")
|
||||
.trim()
|
||||
.to_owned(),
|
||||
r.trim().to_owned().replace(")", ""),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Test from puzzle input
|
||||
#[cfg(test)]
|
||||
@@ -44,7 +100,7 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "6");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user