Skip to content

Commit

Permalink
day 14 + 15 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-the-Bomb committed Dec 16, 2023
1 parent b150983 commit 053788f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
44 changes: 21 additions & 23 deletions src/bin/day14.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Day 14: Parabolic Reflector Dish
//!
//! <https://adventofcode.com/2023/day/14>
use std::{collections::HashMap, fmt::Display};
use std::{collections::VecDeque, fmt::Display};
use aoc_2023::Solution;

pub struct Day14;
Expand Down Expand Up @@ -104,36 +104,34 @@ impl Day14 {
)
}

/// # Panics
///
/// If the cycles vec is empty
pub fn part_two<T: Display>(&self, inp: T) -> usize {
let grid = Self::get_grid(inp);

let mut start = 0;
let mut next_term = Self::cycle(&grid);
let mut cycles = HashMap::from([(grid, 0)]);
for i in 1.. {
next_term = Self::cycle(&next_term);
let mut cycles = VecDeque::from([grid]);
let start = loop {
let next_term = Self::cycle(cycles
.back()
.unwrap()
);

if let Some(&index) =
cycles.get(&next_term)
if let Some(index) = cycles
.iter()
.position(|term| term == &next_term)
{
start = index;
break;
break index;
}
cycles.insert(next_term.clone(), i);
}
cycles.push_back(next_term);
};

Self::get_load(
&cycles
.iter()
.find_map(|(grid, &index)|
(index + 1
== (1_000_000_000 - start)
% (cycles.len() - start)
+ start
)
.then_some(grid)
)
.unwrap()
&cycles[
(1_000_000_000 - start)
% (cycles.len() - start)
+ start
]
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/bin/day15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Day15;
impl Day15 {
/// Hashing algorithm
/// turns any string into an 8-bit integer
///
///
/// `((current_value + ascii_value(character)) * 17) % 256`
fn hash<T>(string: T) -> u8
where
Expand All @@ -34,7 +34,7 @@ impl Day15 {
}

/// # Panics
///
///
/// If neither a '=' or '-' exist in an entry in the input
pub fn part_two<T: Display>(&self, inp: T) -> u32 {
let inp = inp.to_string();
Expand All @@ -45,7 +45,7 @@ impl Day15 {
{
let (label, focus) = string
.split_once('=')
.unwrap_or_else(||
.unwrap_or_else(||
string
.split_once('-')
.unwrap()
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Day15 {
// therefore this will never happen, so we do nothing
_ => (),
}
}
}
}

boxes
Expand Down

0 comments on commit 053788f

Please sign in to comment.