Skip to content

Commit

Permalink
refactor(2023): simplify day 8 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Dec 8, 2023
1 parent 0a0dedb commit 0e9320b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion rust/2023/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ With the help of [cargo-aoc](https://github.com/gobanos/cargo-aoc) I get automat
| 5 | 1.27 µs | 238.45 ms\*\* | `-16.72%` / `-98.87%` | 16.85 µs |
| 6 | 101.59 ns | 15.04 ms | | 257.13 ns / 204.78 ns |
| 7 | 364.18 µs | 359.22 µs | | 318.23 µs / 324.96 µs |
| 8 | 935.78 µs | 15.25 ms | | 130.34 µs |
| 8 | 926.09 µs | 4.47 ms | - / `-70.69%` | 137.33 µs |

\* compared to first solution<br/>
\*\* slow, didn't benchmark. Value comes from running the solver.
Expand All @@ -50,3 +50,4 @@ With the help of [cargo-aoc](https://github.com/gobanos/cargo-aoc) I get automat
| 3 | 172.23 µs | - | Baseline | [Link](https://github.com/believer/advent-of-code/blob/75a83e31024bbac99a0664f81fce4e13ec1e94af/rust/2023/src/day_03.rs) |
| 4 | 24.33 µs | 24.32 µs | Baseline | [Link](https://github.com/believer/advent-of-code/blob/c970c6322d3904048bcf3f30b1052e2916476d73/rust/2023/src/day_04.rs) |
| 5 | 1.50 µs | 21.22 s | Baseline | [Link](https://github.com/believer/advent-of-code/blob/39b0904c4921f4ae79963a6df49bb3502ef6b3be/rust/2023/src/day_05.rs) |
| 8 | - | 15.25 ms | Baseline | [Link](https://github.com/believer/advent-of-code/blob/4b96f6e935734f657469b091072282f1c430bcd3/rust/2023/src/day_08.rs) |
34 changes: 20 additions & 14 deletions rust/2023/src/day_08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,34 @@ assert_eq!(solve_part_02(&input_generator(data)), 11188774513823);
```"#]
#[aoc(day8, part2)]
pub fn solve_part_02(input: &Input) -> i64 {
let Input {
directions,
instructions,
} = input;
let mut all_steps = Vec::new();
let start_locations = input
.instructions
.keys()
.filter(|i| i.ends_with('A'))
.cloned()
.collect::<Vec<String>>();
let start_locations =
instructions
.keys()
.filter(|i| i.ends_with('A'))
.cloned()
.collect::<Vec<String>>();

for start in start_locations {
let mut current = start.clone();
let mut location = start.clone();
let mut index = 0;
let mut current_direction = input.directions.get(index).unwrap();
let mut current_direction = directions.get(index).unwrap();
let mut steps: i64 = 0;

while !current.ends_with('Z') {
current = match current_direction {
Direction::Right => input.instructions.get(&current).unwrap().1.to_string(),
Direction::Left => input.instructions.get(&current).unwrap().0.to_string(),
while !location.ends_with('Z') {
let (left, right) = instructions.get(&location).unwrap();

location = match current_direction {
Direction::Right => right.to_string(),
Direction::Left => left.to_string(),
};

index = (index + 1) % input.directions.len();
current_direction = input.directions.get(index).unwrap();
index = (index + 1) % directions.len();
current_direction = directions.get(index).unwrap();
steps += 1;
}

Expand Down

0 comments on commit 0e9320b

Please sign in to comment.