Skip to content

Commit

Permalink
day 15 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
khwilson committed Dec 15, 2022
1 parent 1cd1240 commit f54302b
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ edition = "2021"
clap = { version = "4.0", features = ["derive"] }
itertools = "0.10.5"
serde_json = "1.0"
lazy_static = "1.4.0"
regex = "1.7.0"
33 changes: 33 additions & 0 deletions input/input15
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Sensor at x=407069, y=1770807: closest beacon is at x=105942, y=2000000
Sensor at x=2968955, y=2961853: closest beacon is at x=2700669, y=3091664
Sensor at x=3069788, y=2289672: closest beacon is at x=3072064, y=2287523
Sensor at x=2206, y=1896380: closest beacon is at x=105942, y=2000000
Sensor at x=3010408, y=2580417: closest beacon is at x=2966207, y=2275132
Sensor at x=2511130, y=2230361: closest beacon is at x=2966207, y=2275132
Sensor at x=65435, y=2285654: closest beacon is at x=105942, y=2000000
Sensor at x=2811709, y=3379959: closest beacon is at x=2801189, y=3200444
Sensor at x=168413, y=3989039: closest beacon is at x=-631655, y=3592291
Sensor at x=165506, y=2154294: closest beacon is at x=105942, y=2000000
Sensor at x=2720578, y=3116882: closest beacon is at x=2700669, y=3091664
Sensor at x=786521, y=1485720: closest beacon is at x=105942, y=2000000
Sensor at x=82364, y=2011850: closest beacon is at x=105942, y=2000000
Sensor at x=2764729, y=3156203: closest beacon is at x=2801189, y=3200444
Sensor at x=1795379, y=1766882: closest beacon is at x=1616322, y=907350
Sensor at x=2708986, y=3105910: closest beacon is at x=2700669, y=3091664
Sensor at x=579597, y=439: closest beacon is at x=1616322, y=907350
Sensor at x=2671201, y=2736834: closest beacon is at x=2700669, y=3091664
Sensor at x=3901, y=2089464: closest beacon is at x=105942, y=2000000
Sensor at x=144449, y=813212: closest beacon is at x=105942, y=2000000
Sensor at x=3619265, y=3169784: closest beacon is at x=2801189, y=3200444
Sensor at x=2239333, y=3878605: closest beacon is at x=2801189, y=3200444
Sensor at x=2220630, y=2493371: closest beacon is at x=2966207, y=2275132
Sensor at x=1148022, y=403837: closest beacon is at x=1616322, y=907350
Sensor at x=996105, y=3077490: closest beacon is at x=2700669, y=3091664
Sensor at x=3763069, y=3875159: closest beacon is at x=2801189, y=3200444
Sensor at x=3994575, y=2268273: closest beacon is at x=3072064, y=2287523
Sensor at x=3025257, y=2244500: closest beacon is at x=2966207, y=2275132
Sensor at x=2721366, y=1657084: closest beacon is at x=2966207, y=2275132
Sensor at x=3783491, y=1332930: closest beacon is at x=3072064, y=2287523
Sensor at x=52706, y=2020407: closest beacon is at x=105942, y=2000000
Sensor at x=2543090, y=47584: closest beacon is at x=3450858, y=-772833
Sensor at x=3499766, y=2477193: closest beacon is at x=3072064, y=2287523
85 changes: 83 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::u32::MAX;
use std::vec;
use itertools::Itertools;
use serde_json::{Value};
use lazy_static::lazy_static;
use regex::Regex;

fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
Expand Down Expand Up @@ -851,7 +853,7 @@ fn day14() {
let max_y = coords.iter().flatten().map(|x| x.1).max().unwrap();

// Setup the canvas
let mut canvas: Vec<Vec<u8>> = (0..(max_y + 3)).map(|_| (0..2002).map(|_| 0).collect_vec()).collect_vec();
let mut canvas: Vec<Vec<u8>> = (0..(max_y + 3)).map(|_| (0..1001).map(|_| 0).collect_vec()).collect_vec();
for l in coords.iter() {
for (left, right) in l.iter().tuple_windows() {
let (left_x, left_y) = *left;
Expand All @@ -871,7 +873,7 @@ fn day14() {
}
}
}
for j in 0..2002 {
for j in 0..1001 {
canvas[max_y + 2][j] = 1;
}

Expand Down Expand Up @@ -909,6 +911,84 @@ fn day14() {
}
}

fn get_numbers(s: &str) -> (i64, i64, i64, i64) {
lazy_static! {
static ref RE: Regex = Regex::new(r"\d+").unwrap();
}
RE.find_iter(s).filter_map(|d| d.as_str().parse().ok()).collect_tuple().unwrap()
}

fn day15() {
let mut coords: Vec<_> = vec![];
if let Ok(lines) = read_lines("./input/input15") {
for line in lines {
if let Ok(ip) = line {
coords.push(get_numbers(ip.trim_end()));
}
}
}
let num_coords = coords.len();
// let part_one_row = 10;
let part_one_row = 2000000;

// What intervals are covered? Inclusive on both sides
let mut intervals: Vec<(i64, i64)> = vec![];
for i in 0..num_coords {
let (sx, sy, bx, by) = coords[i];
let dist = (sx - bx).abs() + (sy - by).abs();
let ydist = (part_one_row - sy).abs();
let max_x_dist = dist - ydist;
if max_x_dist >= 0 {
intervals.push((sx - max_x_dist, sx + max_x_dist));
}
}

// Now we take the union
intervals.sort();
let mut new_intervals = vec![];
let mut cur_left = intervals[0].0;
let mut cur_right = intervals[0].1;
for i in 1..intervals.len() {
let (left, right) = intervals[i];
if left <= cur_right {
// cur_left <= left because we sorted
// So expand to the right if it's possible
cur_right = std::cmp::max(right, cur_right);
} else {
// We're starting a new interval
new_intervals.push((cur_left, cur_right));
cur_left = left;
cur_right = right;
}
}
// Push the last interval
new_intervals.push((cur_left, cur_right));

let ans_with_beacons: i64 = new_intervals.iter().map(|(l, r)| r - l + 1).sum();

// Need to subtract out beacons in these intervals
let mut beacons: Vec<i64> = vec![];
for i in 0..num_coords {
if coords[i].3 == part_one_row {
beacons.push(coords[i].2);
}
}

let mut blah: HashSet<i64> = HashSet::new();
let num_intervals = new_intervals.len();
for i in 0..beacons.len() {
let bx = beacons[i];
for j in 0..num_intervals {
if (new_intervals[j].0 <= bx) && (bx <= new_intervals[j].1) {
blah.insert(bx);
}
}
}
let foo: i64 = i64::try_from(blah.len()).ok().unwrap();

println!("The answer to part 1 is: {}", ans_with_beacons - foo);
}

fn main() {
day01();
day02();
Expand All @@ -924,4 +1004,5 @@ fn main() {
day12();
day13();
day14();
day15();
}

0 comments on commit f54302b

Please sign in to comment.