Skip to content

Commit

Permalink
day21: Find start position
Browse files Browse the repository at this point in the history
  • Loading branch information
pedantic79 committed Jan 6, 2024
1 parent d35fcac commit 7aa0b04
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/day21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ impl Grid {
}
}
input.clear();
std::mem::swap(input, temp);
}
}

Expand All @@ -103,14 +102,16 @@ fn to_usize(i: isize) -> usize {
pub fn generator(input: &str) -> Grid {
let width = input.find('\n').unwrap();
let height = input.len() / width;
let start = input.find('S').unwrap();
let start = (start / (width + 1), start % (width + 1));

let grid = unsafe { std::mem::transmute::<&[u8], &[State]>(input.as_bytes()) };

Grid {
grid: grid.to_vec(),
height: to_isize(height),
width: to_isize(width),
start: (to_isize(height / 2), to_isize(width / 2)),
start: (to_isize(start.0), to_isize(start.1)),
}
}

Expand All @@ -120,33 +121,33 @@ fn solve(inputs: &Grid, steps: usize) -> usize {

fn solve_multiple(inputs: &Grid, steps: usize, target_steps: &[usize]) -> Vec<usize> {
let mut even_output = HashSet::new();
let mut even = HashSet::new();
let mut odd_output = HashSet::new();

let mut frontier = vec![inputs.start];
let mut temp = vec![];
let mut frontier_odd = vec![inputs.start];
let mut frontier_even = vec![];

let mut pos = 0;
let mut output = Vec::with_capacity(target_steps.len());

for i in 0..steps / 2 {
inputs.step(&mut frontier, &mut even, &mut temp);
inputs.step(&mut frontier_odd, &mut odd_output, &mut frontier_even);
if i * 2 + 1 == target_steps[pos] {
pos += 1;
output.push(even.len());
output.push(odd_output.len());
}
// inputs.display(&even);

inputs.step(&mut frontier, &mut even_output, &mut temp);
inputs.step(&mut frontier_even, &mut even_output, &mut frontier_odd);
if i * 2 + 2 == target_steps[pos] {
pos += 1;
output.push(even_output.len());
}
// inputs.display(&odd);
}
if steps.is_odd() {
inputs.step(&mut frontier, &mut even, &mut temp);
inputs.step(&mut frontier_odd, &mut odd_output, &mut frontier_even);
if steps == target_steps[pos] {
output.push(even.len());
output.push(odd_output.len());
}
}

Expand All @@ -165,7 +166,7 @@ pub fn part2(inputs: &Grid) -> usize {

let xs = [0.0, 1.0, 2.0];
let ixs = [rem, rem + n, rem + n * 2];
let iys = solve_multiple(inputs, ixs.last().copied().unwrap(), &ixs);
let iys = solve_multiple(inputs, ixs[ixs.len() - 1], &ixs);
let ys: Vec<_> = iys.into_iter().map(|y| y as f64).collect();

let coefficients = polyfit(&xs, &ys, 2).unwrap();
Expand Down

0 comments on commit 7aa0b04

Please sign in to comment.