Skip to content

Commit

Permalink
Add day6 part2
Browse files Browse the repository at this point in the history
  • Loading branch information
qselle committed Dec 8, 2024
1 parent 13ac229 commit 924cd12
Showing 1 changed file with 106 additions and 25 deletions.
131 changes: 106 additions & 25 deletions src/day6.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use aoc_runner_derive::{aoc, aoc_generator};
use std::collections::HashMap;

#[derive(Debug)]
pub struct Map {
Expand All @@ -18,6 +19,7 @@ pub fn input_generator(input: &str) -> Map {
}
}

#[derive(PartialEq, Copy, Clone)]
enum Direction {
Up,
Down,
Expand All @@ -40,31 +42,110 @@ impl Direction {
#[aoc(day6, part1)]
pub fn part1(input: &Map) -> usize {
let mut direction: Direction = Direction::Up;
let (mut y, mut x) = input.current;
let mut new_map = input.map.clone();
let mut count = 0;
while y < input.map.len() && x < input.map[y].len() {
if input.map[y][x] == '#' {
// put back
match direction {
Direction::Up => y += 1,
Direction::Down => y -= 1,
Direction::Left => x += 1,
Direction::Right => x -= 1,
}
let mut visited = input.map.clone();
let (mut row, mut col) = input.current;
let row_limit = input.map.len();
let col_limit = input.map[0].len();
let mut total: usize = 0;
loop {
let (next_row, next_col) = match direction {
Direction::Up if row > 0 => (row - 1, col),
Direction::Down if row < row_limit - 1 => (row + 1, col),
Direction::Left if col > 0 => (row, col - 1),
Direction::Right if col < col_limit - 1 => (row, col + 1),
_ => break,
};

if visited[next_row][next_col] == '#' {
direction = direction.rotate();
} else if new_map[y][x] != 'X' {
count += 1;
new_map[y][x] = 'X';
} else if visited[next_row][next_col] == 'X' {
row = next_row;
col = next_col;
} else {
visited[next_row][next_col] = 'X';
total += 1;
row = next_row;
col = next_col;
}
match direction {
Direction::Up => y -= 1,
Direction::Down => y += 1,
Direction::Left => x -= 1,
Direction::Right => x += 1,
}
total
}

#[aoc(day6, part2)]
pub fn part2(input: &Map) -> usize {
let mut total: usize = 0;

let mut direction: Direction = Direction::Up;
let row_limit = input.map.len();
let col_limit = input.map[0].len();

let (mut row, mut col) = input.current;

loop {
let (next_row, next_col) = match direction {
Direction::Up if row > 0 => (row - 1, col),
Direction::Down if row < row_limit - 1 => (row + 1, col),
Direction::Left if col > 0 => (row, col - 1),
Direction::Right if col < col_limit - 1 => (row, col + 1),
_ => break,
};

if input.map[next_row][next_col] == '#' {
direction = direction.rotate();
} else {
let mut loop_visited = input.map.clone();
let mut loop_direction = direction;
loop_visited[next_row][next_col] = 'O';

let mut visited: HashMap<(usize, usize), Direction> = HashMap::new();

let (mut loop_row, mut loop_col) = (row, col);

loop {
let (next_loop_row, next_loop_col) = match loop_direction {
Direction::Up if loop_row > 0 => (loop_row - 1, loop_col),
Direction::Down if loop_row < row_limit - 1 => (loop_row + 1, loop_col),
Direction::Left if loop_col > 0 => (loop_row, loop_col - 1),
Direction::Right if loop_col < col_limit - 1 => (loop_row, loop_col + 1),
_ => break,
};
if loop_visited[next_loop_row][next_loop_col] == '#'
|| loop_visited[next_loop_row][next_loop_col] == 'O'
{
loop_direction = loop_direction.rotate();
} else {
loop_row = next_loop_row;
loop_col = next_loop_col;
loop_visited[next_loop_row][next_loop_col] = 'X';
match visited.entry((loop_row, loop_col)) {
std::collections::hash_map::Entry::Vacant(entry) => {
entry.insert(loop_direction);
}
std::collections::hash_map::Entry::Occupied(entry) => {
if *entry.get() == loop_direction {
total += 1;
loop_visited[loop_row][loop_col] = 'T';
for i in loop_visited {
for j in i {
print!("{j}");
}
println!();
}
println!();
println!();
break;
}
}
}
}
}

// total += 1;
row = next_row;
col = next_col;
}
}
count
total
}

#[cfg(test)]
Expand All @@ -87,8 +168,8 @@ mod tests {
assert_eq!(41, part1(&input_generator(INPUT)))
}

// #[test]
// fn test_part2() {
// assert_eq!(123, part2(&input_generator(INPUT)))
// }
#[test]
fn test_part2() {
assert_eq!(6, part2(&input_generator(INPUT)))
}
}

0 comments on commit 924cd12

Please sign in to comment.