Day 14 Part 2 Complete -> First solution took agest to run but added the hashmap to detect cycles in the rotations. Thanks reddit :)
This commit is contained in:
parent
a3b2847d6b
commit
505ea3771a
@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use super::Solution;
|
use super::Solution;
|
||||||
|
|
||||||
pub struct Day14 {}
|
pub struct Day14 {}
|
||||||
@ -19,9 +21,28 @@ impl Solution for Day14 {
|
|||||||
|
|
||||||
fn part2(
|
fn part2(
|
||||||
&self,
|
&self,
|
||||||
_input: &mut Vec<String>,
|
input: &mut Vec<String>,
|
||||||
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
|
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
|
||||||
Ok(Box::new("Ready"))
|
let mut rock_formation = input
|
||||||
|
.iter()
|
||||||
|
.map(|r| r.chars().collect::<Vec<_>>())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let mut rotations: HashMap<Vec<Vec<char>>, usize> = HashMap::new();
|
||||||
|
for i in 1..=1_000_000_000 {
|
||||||
|
for _ in 0..4 {
|
||||||
|
self.pull_north(&mut rock_formation);
|
||||||
|
rock_formation = self.rotate(&rock_formation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(rotation) = rotations.insert(rock_formation.clone(), i) {
|
||||||
|
if (1_000_000_000 - i) % (i - rotation) == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Box::new(self.get_load(&rock_formation)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_day(&self) -> u8 {
|
fn get_day(&self) -> u8 {
|
||||||
@ -59,6 +80,17 @@ impl Day14 {
|
|||||||
})
|
})
|
||||||
.sum::<usize>() as u32
|
.sum::<usize>() as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function that rotates the given vector through 90 degrees clockwise
|
||||||
|
fn rotate(&self, cave: &Vec<Vec<char>>) -> Vec<Vec<char>> {
|
||||||
|
let mut new_cave = vec![vec![' '; cave.len()]; cave[0].len()];
|
||||||
|
for row in 0..cave.len() {
|
||||||
|
for col in 0..cave[0].len() {
|
||||||
|
new_cave[col][cave.len() - row - 1] = cave[row][col];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new_cave
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test from puzzle input
|
/// Test from puzzle input
|
||||||
@ -98,6 +130,25 @@ mod test {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
assert_eq!(answer, "Ready");
|
assert_eq!(answer, "64");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rotation_test() {
|
||||||
|
let challenge = day14::Day14 {};
|
||||||
|
|
||||||
|
let mut arr = vec![
|
||||||
|
vec!['a', 'b', 'c', 'd'],
|
||||||
|
vec!['d', 'e', 'f', 'g'],
|
||||||
|
vec!['h', 'i', 'j', 'k'],
|
||||||
|
vec!['l', 'm', 'n', 'o'],
|
||||||
|
];
|
||||||
|
|
||||||
|
let ans = arr.clone();
|
||||||
|
for _ in 0..4 {
|
||||||
|
arr = challenge.rotate(&arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(arr, ans);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user