Skip to content

Commit

Permalink
Solve day 21 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Riari committed Dec 22, 2023
1 parent 42ceff3 commit dbd3c53
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions src/bin/21.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::ops::Index;

use std::collections::HashMap;
use num::Integer;

advent_of_code::solution!(21);
Expand All @@ -11,15 +10,15 @@ const S: Position = (0, 1);
const E: Position = (1, 0);
const W: Position = (-1, 0);

fn is_valid_position(x: isize, y: isize, map: &Vec<Vec<char>>) -> bool {
x >= 0 && y >= 0 && x < map[0].len() as isize && y < map.len() as isize
fn is_valid_position(x: isize, y: isize, map_size: isize) -> bool {
x >= 0 && y >= 0 && x < map_size && y < map_size
}

fn get_plots_adjacent_to(x: isize, y: isize, map: &Vec<Vec<char>>) -> Vec<Position> {
let mut plots = vec![];
for (dx, dy) in &[N, S, E, W] {
let (nx, ny) = (x + dx, y + dy);
if !is_valid_position(nx, ny, map) {
if !is_valid_position(nx, ny, map.len() as isize) {
continue;
}
if map[ny as usize][nx as usize] == '.' {
Expand All @@ -29,60 +28,62 @@ fn get_plots_adjacent_to(x: isize, y: isize, map: &Vec<Vec<char>>) -> Vec<Positi
plots
}

pub fn part_one(input: &str) -> Option<u32> {
fn solve(input: &str, is_p2: bool) -> Option<u32> {
let map: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect();
let mut visited: Vec<Position> = vec![];
let mut visited_steps: Vec<usize> = vec![];
let mut visited: HashMap<Position, u32> = HashMap::new();

// Input is assumed to be square with starting position in the centre
let map_size = map.len() as isize;
visited.push((map_size / 2, map_size / 2));
visited.insert((map_size / 2, map_size / 2), 0);

let mut to_visit: Vec<Position> = vec![];
for plot in get_plots_adjacent_to(map_size / 2, map_size / 2, &map) {
to_visit.push(plot);
}

for steps in 1..7 {
for steps in 1..65 {
let mut next: Vec<Position> = vec![];
while let Some(plot) = to_visit.pop() {
let (x, y) = plot;
visited.push((x, y));
visited_steps.push(steps);
visited.insert((x, y), steps);

let neighbours = get_plots_adjacent_to(x, y, &map);
for neighbour in neighbours {
if !visited.contains(&neighbour) {
if !visited.contains_key(&neighbour) && !next.contains(&neighbour) {
next.push(neighbour);
}
}
}
to_visit = next;
}

for y in 0..map_size {
for x in 0..map_size {
if visited.contains(&(x, y)) && visited_steps[visited.iter().position(|pos| pos == &(x, y)).unwrap()].is_even() {
print!("O");
} else {
print!("{}", map[y as usize][x as usize]);
}
}
println!();
}
// for y in 0..map_size {
// for x in 0..map_size {
// if visited.contains_key(&(x, y)) && visited[&(x, y)].is_even() {
// print!("O");
// } else {
// print!("{}", map[y as usize][x as usize]);
// }
// }
// println!();
// }

let mut reachable = 0;
for i in 0..visited_steps.len() {
if visited_steps[i].is_even() {
for (_, steps) in visited {
if steps.is_even() {
reachable += 1;
}
}

Some(reachable as u32)
}

pub fn part_one(input: &str) -> Option<u32> {
solve(input, false)
}

pub fn part_two(input: &str) -> Option<u32> {
None
solve(input, true)
}

#[cfg(test)]
Expand All @@ -92,7 +93,7 @@ mod tests {
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(16));
}

#[test]
Expand Down

0 comments on commit dbd3c53

Please sign in to comment.