|
| 1 | +use aoc_rust_common::Solution; |
| 2 | +use std::fmt::Display; |
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +pub struct Day17; |
| 6 | + |
| 7 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 8 | +enum Rock { |
| 9 | + Horizontal, |
| 10 | + Plus, |
| 11 | + Corner, |
| 12 | + Vertical, |
| 13 | + Square, |
| 14 | +} |
| 15 | + |
| 16 | +impl Rock { |
| 17 | + fn get_offsets(&self) -> Vec<(i32, i32)> { |
| 18 | + match self { |
| 19 | + Rock::Horizontal => vec![(0, 0), (1, 0), (2, 0), (3, 0)], |
| 20 | + Rock::Plus => vec![(1, 0), (0, 1), (1, 1), (2, 1), (1, 2)], |
| 21 | + Rock::Corner => vec![(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], |
| 22 | + Rock::Vertical => vec![(0, 0), (0, 1), (0, 2), (0, 3)], |
| 23 | + Rock::Square => vec![(0, 0), (1, 0), (0, 1), (1, 1)], |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const ROCKS: [Rock; 5] = [ |
| 29 | + Rock::Horizontal, |
| 30 | + Rock::Plus, |
| 31 | + Rock::Corner, |
| 32 | + Rock::Vertical, |
| 33 | + Rock::Square, |
| 34 | +]; |
| 35 | + |
| 36 | +impl Solution for Day17 { |
| 37 | + fn year(&self) -> u32 { 2022 } |
| 38 | + fn day(&self) -> u32 { 17 } |
| 39 | + |
| 40 | + fn part1(&self, input: &str) -> Box<dyn Display> { |
| 41 | + Box::new(solve(input, 2022)) |
| 42 | + } |
| 43 | + |
| 44 | + fn part2(&self, input: &str) -> Box<dyn Display> { |
| 45 | + Box::new(solve(input, 1_000_000_000_000)) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +fn solve(input: &str, count: u64) -> u64 { |
| 50 | + let jets: Vec<i32> = input.trim().chars().map(|c| if c == '>' { 1 } else { -1 }).collect(); |
| 51 | + let mut grid = HashSet::new(); |
| 52 | + for x in 0..7 { grid.insert((x, 0)); } |
| 53 | + |
| 54 | + let mut height = 0; |
| 55 | + let mut jet_idx = 0; |
| 56 | + let mut seen = HashMap::new(); |
| 57 | + let mut heights = Vec::new(); |
| 58 | + |
| 59 | + for i in 0..count { |
| 60 | + let rock = ROCKS[(i % 5) as usize]; |
| 61 | + let state = (i % 5, jet_idx, get_profile(&grid, height)); |
| 62 | + |
| 63 | + if let Some((prev_i, prev_h)) = seen.get(&state) { |
| 64 | + let cycle_len = i - prev_i; |
| 65 | + let cycle_h = height - prev_h; |
| 66 | + let remaining = count - i; |
| 67 | + let cycles = remaining / cycle_len; |
| 68 | + return height + (cycles * cycle_h) + solve_remainder(&grid, &jets[jet_idx..], i % 5, remaining % cycle_len, height); |
| 69 | + } |
| 70 | + seen.insert(state, (i, height)); |
| 71 | + heights.push(height); |
| 72 | + |
| 73 | + let mut pos = (2, height + 4); |
| 74 | + loop { |
| 75 | + let jet = jets[jet_idx]; |
| 76 | + jet_idx = (jet_idx + 1) % jets.len(); |
| 77 | + if can_move(&grid, &rock, pos.0 + jet, pos.1) { pos.0 += jet; } |
| 78 | + if can_move(&grid, &rock, pos.0, pos.1 - 1) { pos.1 -= 1; } |
| 79 | + else { |
| 80 | + for (dx, dy) in rock.get_offsets() { |
| 81 | + grid.insert((pos.0 + dx, pos.1 + dy), 0); |
| 82 | + height = height.max(pos.1 + dy); |
| 83 | + } |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + height |
| 89 | +} |
| 90 | + |
| 91 | +fn get_profile(grid: &HashSet<(i32, i32)>, height: i32) -> Vec<i32> { |
| 92 | + (0..7).map(|x| (0..height).rev().find(|&y| grid.contains(&(x, y))).map_or(0, |y| height - y)).collect() |
| 93 | +} |
| 94 | + |
| 95 | +fn can_move(grid: &HashSet<(i32, i32)>, rock: &Rock, x: i32, y: i32) -> bool { |
| 96 | + rock.get_offsets().iter().all(|&(dx, dy)| { |
| 97 | + let nx = x + dx; |
| 98 | + let ny = y + dy; |
| 99 | + nx >= 0 && nx < 7 && ny > 0 && !grid.contains(&(nx, ny)) |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +fn solve_remainder(grid: &HashSet<(i32, i32)>, jets: &[i32], rock_idx: usize, count: u64, mut height: i32) -> u64 { |
| 104 | + let mut grid = grid.clone(); |
| 105 | + let mut jet_idx = 0; |
| 106 | + for i in 0..count { |
| 107 | + let rock = ROCKS[(rock_idx + i as usize) % 5]; |
| 108 | + let mut pos = (2, height + 4); |
| 109 | + loop { |
| 110 | + let jet = jets[jet_idx]; |
| 111 | + jet_idx = (jet_idx + 1) % jets.len(); |
| 112 | + if can_move(&grid, &rock, pos.0 + jet, pos.1) { pos.0 += jet; } |
| 113 | + if can_move(&grid, &rock, pos.0, pos.1 - 1) { pos.1 -= 1; } |
| 114 | + else { |
| 115 | + for (dx, dy) in rock.get_offsets() { |
| 116 | + grid.insert((pos.0 + dx, pos.1 + dy), 0); |
| 117 | + height = height.max(pos.1 + dy); |
| 118 | + } |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + (height - *grid.keys().map(|&(_, y)| y).filter(|&y| y < 0).max().unwrap_or(&0)) as u64 // This is a bit hacky, but should work for the remainder |
| 124 | +} |
0 commit comments