|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] |
| 4 | +enum Direction { |
| 5 | + Up, |
| 6 | + Right, |
| 7 | + Down, |
| 8 | + Left, |
| 9 | +} |
| 10 | + |
| 11 | +impl Direction { |
| 12 | + fn get_vector(&self) -> (i32, i32) { |
| 13 | + match self { |
| 14 | + Direction::Up => (0, -1), |
| 15 | + Direction::Right => (1, 0), |
| 16 | + Direction::Down => (0, 1), |
| 17 | + Direction::Left => (-1, 0), |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + fn rotate_clockwise(&mut self) { |
| 22 | + *self = match self { |
| 23 | + Direction::Up => Direction::Right, |
| 24 | + Direction::Right => Direction::Down, |
| 25 | + Direction::Down => Direction::Left, |
| 26 | + Direction::Left => Direction::Up, |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +fn main() { |
| 32 | + let content = include_str!("../Day06.txt"); |
| 33 | + let path = part1(&content); |
| 34 | + part2(&content, path); |
| 35 | +} |
| 36 | + |
| 37 | +fn parse_data(content: &str) -> ((i32, i32), Vec<(i32, i32)>) { |
| 38 | + let line_iter = content.lines().enumerate(); |
| 39 | + let mut initial_pos = (0, 0); |
| 40 | + let mut obstacles = Vec::new(); |
| 41 | + |
| 42 | + for (y, line) in line_iter { |
| 43 | + for (x, c) in line.chars().enumerate() { |
| 44 | + match c { |
| 45 | + '^' => initial_pos = (x as i32, y as i32), |
| 46 | + '.' => {} |
| 47 | + '#' => obstacles.push((x as i32, y as i32)), |
| 48 | + _ => unreachable!(), |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + (initial_pos, obstacles) |
| 54 | +} |
| 55 | + |
| 56 | +fn part1(content: &str) -> HashSet<(i32, i32)> { |
| 57 | + let mut direction = Direction::Up; |
| 58 | + let line_cnt = content.lines().count() as i32; |
| 59 | + let (x_size, y_size) = (content.len() as i32 / line_cnt, line_cnt); |
| 60 | + let ((mut x, mut y), obstacles) = parse_data(content); |
| 61 | + let mut distinct_pos = HashSet::new(); |
| 62 | + |
| 63 | + loop { |
| 64 | + distinct_pos.insert((x, y)); |
| 65 | + let (mut vx, mut vy) = direction.get_vector(); |
| 66 | + let (mut nx, mut ny) = (x + vx, y + vy); |
| 67 | + |
| 68 | + if obstacles.contains(&(nx, ny)) { |
| 69 | + direction.rotate_clockwise(); |
| 70 | + (vx, vy) = direction.get_vector(); |
| 71 | + (nx, ny) = (x + vx, y + vy); |
| 72 | + } |
| 73 | + |
| 74 | + if nx < 0 || nx >= x_size || ny < 0 || ny >= y_size { |
| 75 | + // Out of bound |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + (x, y) = (nx, ny); |
| 80 | + } |
| 81 | + |
| 82 | + println!("Part 1: {}", distinct_pos.len()); |
| 83 | + distinct_pos |
| 84 | +} |
| 85 | + |
| 86 | +fn part2(content: &str, path: HashSet<(i32, i32)>) { |
| 87 | + let mut options = 0; |
| 88 | + let line_cnt = content.lines().count() as i32; |
| 89 | + let (x_size, y_size) = (content.len() as i32 / line_cnt, line_cnt); |
| 90 | + let ((gx, gy), obstacles) = parse_data(content); |
| 91 | + let mut obstacles: HashSet<(i32, i32)> = obstacles.into_iter().collect(); |
| 92 | + |
| 93 | + for (nox, noy) in path { |
| 94 | + obstacles.insert((nox, noy)); |
| 95 | + let mut turns = HashSet::new(); |
| 96 | + let (mut x, mut y) = (gx, gy); |
| 97 | + let mut direction = Direction::Up; |
| 98 | + |
| 99 | + loop { |
| 100 | + let (vx, vy) = direction.get_vector(); |
| 101 | + let (nx, ny) = (x + vx, y + vy); |
| 102 | + |
| 103 | + if obstacles.contains(&(nx, ny)) { |
| 104 | + if turns.contains(&((nx, ny), direction)) { |
| 105 | + options += 1; |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + turns.insert(((nx, ny), direction)); |
| 110 | + direction.rotate_clockwise(); |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + if nx < 0 || nx >= x_size || ny < 0 || ny >= y_size { |
| 115 | + // Out of bound |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + (x, y) = (nx, ny); |
| 120 | + } |
| 121 | + |
| 122 | + obstacles.remove(&(nox, noy)); |
| 123 | + } |
| 124 | + |
| 125 | + println!("Part 2: {options}"); |
| 126 | +} |
0 commit comments