|
| 1 | +use crate::input_reader; |
| 2 | + |
| 3 | +#[derive(Debug, Clone, Copy)] |
| 4 | +struct Point { |
| 5 | + x: i32, |
| 6 | + y: i32, |
| 7 | +} |
| 8 | + |
| 9 | +#[derive(Debug, Clone, Copy)] |
| 10 | +struct Scan { |
| 11 | + sensor: Point, |
| 12 | + beacon: Point, |
| 13 | +} |
| 14 | + |
| 15 | +// Ranges are inclusive, meaning Range {x: 1, y: 5} = 1,2,3,4,5 |
| 16 | +#[derive(Debug, Clone, Copy)] |
| 17 | +struct Range { |
| 18 | + start: i32, |
| 19 | + end: i32, |
| 20 | +} |
| 21 | + |
| 22 | +// Returns a range of x-cooridinates that have been scanned by the sensor. |
| 23 | +// |
| 24 | +// In the example below, the scanned line --RRRRRRRRR---- would be: |
| 25 | +// Range { start: 2, end: 10 } |
| 26 | +// |
| 27 | +// 1 |
| 28 | +// 0 5 0 |
| 29 | +// ............... |
| 30 | +// ......x........ |
| 31 | +// .....x.x....... |
| 32 | +// ....B...x...... |
| 33 | +// ...x.....x..... |
| 34 | +// --RRRRRRRRR---- |
| 35 | +// .x....X....x... |
| 36 | +// ..x.......x.... |
| 37 | +// ...x.....x..... |
| 38 | +// ....x...x...... |
| 39 | +// .....x.x....... |
| 40 | +// ......x........ |
| 41 | +fn get_x_range(scan: &Scan, inspect_y: i32, without_beacons: bool) -> Option<Range> { |
| 42 | + let distance = (scan.beacon.x - scan.sensor.x).abs() + (scan.beacon.y - scan.sensor.y).abs(); |
| 43 | + if inspect_y > scan.sensor.y + distance || inspect_y < scan.sensor.y - distance { |
| 44 | + return None |
| 45 | + } |
| 46 | + |
| 47 | + let delta = (scan.sensor.y - inspect_y).abs(); |
| 48 | + let mut start = scan.sensor.x - distance + delta; |
| 49 | + let mut end = scan.sensor.x + distance - delta; |
| 50 | + if without_beacons { |
| 51 | + if inspect_y == scan.beacon.y { |
| 52 | + if scan.beacon.x == scan.sensor.x { |
| 53 | + |
| 54 | + } else if scan.beacon.x <= scan.sensor.x { |
| 55 | + start += 1; |
| 56 | + } else { |
| 57 | + end -= 1; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + // if start == end { |
| 62 | + // return None |
| 63 | + // } |
| 64 | + return Some(Range { |
| 65 | + start, |
| 66 | + end, |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +// Returns the range that comes after the position x. |
| 71 | +fn get_next_range(ranges: &Vec<Range>, x: i32) -> Option<Range> { |
| 72 | + let mut min_range: Option<Range> = None; |
| 73 | + for range in ranges { |
| 74 | + if range.end < x { |
| 75 | + continue; |
| 76 | + } |
| 77 | + if min_range.is_some() && range.start > min_range.unwrap().start { |
| 78 | + continue; |
| 79 | + } |
| 80 | + min_range = Some(range.clone()); |
| 81 | + } |
| 82 | + return min_range |
| 83 | +} |
| 84 | + |
| 85 | +fn part1(scans: &Vec<Scan>, inspect_y: i32) { |
| 86 | + let mut ranges = vec![]; |
| 87 | + let mut min_range_opt = None; |
| 88 | + let mut min_x = i32::MAX; |
| 89 | + for scan in scans { |
| 90 | + let range_opt = get_x_range(scan, inspect_y, true); |
| 91 | + if range_opt.is_some() { |
| 92 | + let range = range_opt.unwrap(); |
| 93 | + ranges.push(range); |
| 94 | + if range.start < min_x { |
| 95 | + min_x = range.start; |
| 96 | + min_range_opt = Some(range); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if min_range_opt.is_none() { |
| 102 | + println!("Part 1: 0"); |
| 103 | + } |
| 104 | + let min_range = min_range_opt.unwrap(); |
| 105 | + |
| 106 | + // Sweep through the ranges counting how many places that have been scanned. |
| 107 | + let mut cursor = min_x; |
| 108 | + let mut cursor_range = min_range; |
| 109 | + let mut count = 0; |
| 110 | + loop { |
| 111 | + // Sum up the current range. |
| 112 | + count += cursor_range.end - cursor + 1; // +1 to account for the fact that the ranges are "inclusive". |
| 113 | + cursor = cursor_range.end + 1; |
| 114 | + |
| 115 | + // The find the next range. |
| 116 | + let next_range = get_next_range(&ranges, cursor); |
| 117 | + if next_range.is_some() { |
| 118 | + cursor = i32::max(cursor, next_range.unwrap().start); |
| 119 | + cursor_range = next_range.unwrap(); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + break; |
| 124 | + } |
| 125 | + |
| 126 | + println!("Part 1: {}", count); |
| 127 | +} |
| 128 | + |
| 129 | +fn part2(scans: &Vec<Scan>, inspect_max: i32) { |
| 130 | + for y in 0..=inspect_max { |
| 131 | + let ranges: Vec<Range> = scans.iter() |
| 132 | + .map(|&scan| { get_x_range(&scan, y, false) }) |
| 133 | + .filter(|x| x.is_some()) |
| 134 | + .map(|range| { range.unwrap() }) |
| 135 | + .collect(); |
| 136 | + |
| 137 | + // println!("{:?}", ranges); |
| 138 | + |
| 139 | + let mut cursor = 0; |
| 140 | + let mut cursor_range: Option<Range> = None; |
| 141 | + while cursor <= inspect_max { |
| 142 | + let next_range = get_next_range(&ranges, cursor); |
| 143 | + //println!("{:?}, {:?}", cursor_range, next_range); |
| 144 | + if next_range.is_some() { |
| 145 | + if cursor_range.is_none() || next_range.unwrap().start <= cursor_range.unwrap().end + 1 { |
| 146 | + cursor = next_range.unwrap().end + 1; |
| 147 | + cursor_range = next_range; |
| 148 | + continue |
| 149 | + } else { |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + if cursor < inspect_max { |
| 155 | + println!("Part 2: {}", (inspect_max as usize) * (cursor as usize) + (y as usize)); |
| 156 | + break; |
| 157 | + } else { |
| 158 | + // println!("Part 2: None{}\n\n\n", y); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +pub fn run() { |
| 164 | + let mut input = input_reader::read_file_in_cwd("src/day_15.data"); |
| 165 | + input = input |
| 166 | + .replace("=", " ") |
| 167 | + .replace(", ", " ") |
| 168 | + .replace(": ", " "); |
| 169 | + |
| 170 | + let scans_str: Vec<&str> = input.split("\n").collect(); |
| 171 | + let scans: Vec<Scan> = scans_str.iter().map(|&val| { |
| 172 | + let scan_str: Vec<&str> = val.split(" ").collect(); |
| 173 | + |
| 174 | + return Scan { |
| 175 | + sensor: Point { |
| 176 | + x: scan_str[3].parse::<i32>().unwrap(), |
| 177 | + y: scan_str[5].parse::<i32>().unwrap(), |
| 178 | + }, |
| 179 | + beacon: Point { |
| 180 | + x: scan_str[11].parse::<i32>().unwrap(), |
| 181 | + y: scan_str[13].parse::<i32>().unwrap(), |
| 182 | + } |
| 183 | + }; |
| 184 | + }).collect(); |
| 185 | + part1(&scans, 2000000); |
| 186 | + part2(&scans, 4000000); |
| 187 | + // part1(&scans, 10); |
| 188 | + // part2(&scans, 20); |
| 189 | +} |
0 commit comments