Compare commits

...

2 Commits

Author SHA1 Message Date
ccc4301d62 d15p2 2024-03-29 16:04:15 -07:00
c3437af12b d15p1 2024-03-28 19:00:59 -07:00
6 changed files with 223 additions and 0 deletions

7
day15/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day15"
version = "0.1.0"

8
day15/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day15"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

33
day15/input.txt Normal file
View File

@ -0,0 +1,33 @@
Sensor at x=2899860, y=3122031: closest beacon is at x=2701269, y=3542780
Sensor at x=1836719, y=1116779: closest beacon is at x=2037055, y=2000000
Sensor at x=3995802, y=2706630: closest beacon is at x=3944538, y=3053285
Sensor at x=2591204, y=2008272: closest beacon is at x=2597837, y=2509170
Sensor at x=2546593, y=1538222: closest beacon is at x=2037055, y=2000000
Sensor at x=252214, y=61954: closest beacon is at x=1087565, y=-690123
Sensor at x=950, y=1106672: closest beacon is at x=-893678, y=1276864
Sensor at x=1349445, y=1752783: closest beacon is at x=2037055, y=2000000
Sensor at x=3195828, y=3483667: closest beacon is at x=3334657, y=3531523
Sensor at x=2057761, y=2154359: closest beacon is at x=2037055, y=2000000
Sensor at x=2315350, y=3364640: closest beacon is at x=2701269, y=3542780
Sensor at x=327139, y=2426600: closest beacon is at x=-88420, y=3646947
Sensor at x=3943522, y=2854345: closest beacon is at x=3944538, y=3053285
Sensor at x=3358620, y=516881: closest beacon is at x=3260516, y=2246079
Sensor at x=1788376, y=8679: closest beacon is at x=1087565, y=-690123
Sensor at x=3344883, y=3537985: closest beacon is at x=3334657, y=3531523
Sensor at x=2961064, y=2697125: closest beacon is at x=2597837, y=2509170
Sensor at x=3780090, y=2093546: closest beacon is at x=3260516, y=2246079
Sensor at x=3291917, y=3398703: closest beacon is at x=3334657, y=3531523
Sensor at x=3999864, y=2998005: closest beacon is at x=3944538, y=3053285
Sensor at x=2919272, y=3732950: closest beacon is at x=2701269, y=3542780
Sensor at x=2057404, y=2947435: closest beacon is at x=2037055, y=2000000
Sensor at x=1072126, y=645784: closest beacon is at x=1087565, y=-690123
Sensor at x=3549465, y=2554712: closest beacon is at x=3260516, y=2246079
Sensor at x=3550313, y=3121694: closest beacon is at x=3944538, y=3053285
Sensor at x=3405149, y=3483630: closest beacon is at x=3334657, y=3531523
Sensor at x=2600212, y=3961193: closest beacon is at x=2701269, y=3542780
Sensor at x=1102632, y=3932527: closest beacon is at x=-88420, y=3646947
Sensor at x=67001, y=3506079: closest beacon is at x=-88420, y=3646947
Sensor at x=3994250, y=3975025: closest beacon is at x=3944538, y=3053285
Sensor at x=3019750, y=2125144: closest beacon is at x=3260516, y=2246079
Sensor at x=3282319, y=3656404: closest beacon is at x=3334657, y=3531523
Sensor at x=2797371, y=3645126: closest beacon is at x=2701269, y=3542780

83
day15/src/main.rs Normal file
View File

@ -0,0 +1,83 @@
use std::env;
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::cmp::{min, max};
use std::collections::HashSet;
fn parse_num(s: &[u8]) -> (i64, usize) {
let mut num: i64 = 0;
let negative = s[0] == '-' as u8;
let start = if negative {1} else {0};
let mut len = if negative {1} else {0};
for i in start..s.len() {
if s[i].is_ascii_digit() {
num = num*10 + (s[i] as i64 - '0' as i64);
len += 1;
} else {
break;
}
}
(if negative {-num} else {num}, len)
}
fn combine(ranges: &mut Vec<(i64, i64)>, l: i64, r: i64) {
for (l0, r0) in ranges.iter_mut() {
if *l0 <= r && l <= *r0 {
*l0 = min(*l0, l);
*r0 = max(*r0, r);
return;
}
}
if ranges.len() > 0 {
let (_, last_r) = ranges.last_mut().unwrap();
if l == *last_r + 1 {
*last_r = r;
return;
}
}
ranges.push((l, r));
}
fn main() {
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
let lines = BufReader::new(file).lines();
let mut sensors: Vec<(i64, i64, u64)> = Vec::new();
let mut beacons: HashSet<(i64, i64)> = HashSet::new();
for line in lines.flatten() {
let l_bytes = line.as_bytes();
let (sx, e1) = parse_num(&l_bytes[12..]);
let (sy, e2) = parse_num(&l_bytes[12 + e1 + 4..]);
let (bx, e3) = parse_num(&l_bytes[12 + e1 + 4 + e2 + 25..]);
let (by, _) = parse_num(&l_bytes[12 + e1 + 4 + e2 + 25 + e3 + 4..]);
sensors.push((sx, sy, ((bx - sx).abs() + (by - sy).abs()) as u64));
beacons.insert((bx, by));
}
sensors.sort_by_key(|(x, _, _)| *x);
println!("{:?}", sensors);
println!("{:?}", beacons);
let max_y = &env::args().nth(2).expect("max y").parse::<i64>().unwrap();
for target_y in 0..=*max_y {
let mut line_coverages: Vec<(i64, i64)> = Vec::new();
for (sx, sy, range) in &sensors {
let clip = (*range as i64) - (sy - target_y).abs();
if clip < 0 {
continue;
}
let startx = sx - clip;
let endx = sx + clip;
line_coverages.push((startx, endx));
}
line_coverages.sort_by_key(|(x, _)| *x);
let mut coverage: Vec<(i64, i64)> = Vec::new();
for (startx, endx) in line_coverages {
combine(&mut coverage, startx, endx);
}
if coverage.len() > 1 {
let score = 4000000 * (coverage[0].1 + 1) + target_y;
println!("{:?} --> {:?}", coverage, score);
break;
}
}
}

78
day15/src/main1.rs Normal file
View File

@ -0,0 +1,78 @@
use std::env;
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::cmp::{min, max};
use std::collections::HashSet;
fn parse_num(s: &[u8]) -> (i64, usize) {
let mut num: i64 = 0;
let negative = s[0] == '-' as u8;
let start = if negative {1} else {0};
let mut len = if negative {1} else {0};
for i in start..s.len() {
if s[i].is_ascii_digit() {
num = num*10 + (s[i] as i64 - '0' as i64);
len += 1;
} else {
break;
}
}
(if negative {-num} else {num}, len)
}
fn combine(ranges: &mut Vec<(i64, i64)>, l: i64, r: i64) {
for (l0, r0) in ranges.iter_mut() {
if *l0 <= r && l <= *r0 {
*l0 = min(*l0, l);
*r0 = max(*r0, r);
return;
}
}
ranges.push((l, r));
}
fn main() {
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
let lines = BufReader::new(file).lines();
let mut sensors: Vec<(i64, i64, u64)> = Vec::new();
let mut beacons: HashSet<(i64, i64)> = HashSet::new();
for line in lines.flatten() {
let l_bytes = line.as_bytes();
let (sx, e1) = parse_num(&l_bytes[12..]);
let (sy, e2) = parse_num(&l_bytes[12 + e1 + 4..]);
let (bx, e3) = parse_num(&l_bytes[12 + e1 + 4 + e2 + 25..]);
let (by, _) = parse_num(&l_bytes[12 + e1 + 4 + e2 + 25 + e3 + 4..]);
sensors.push((sx, sy, ((bx - sx).abs() + (by - sy).abs()) as u64));
beacons.insert((bx, by));
}
sensors.sort_by_key(|(x, _, _)| *x);
println!("{:?}", sensors);
println!("{:?}", beacons);
let target_y = &env::args().nth(2).expect("line").parse::<i64>().unwrap();
let mut coverage: Vec<(i64, i64)> = Vec::new();
for (sx, sy, range) in sensors {
let clip = (range as i64) - (sy - target_y).abs();
if clip < 0 {
continue;
}
let startx = sx - clip;
let endx = sx + clip;
combine(&mut coverage, startx, endx);
}
println!("{:?}", coverage);
let mut sum = 0;
for (l, r) in coverage {
let mut overlap = 0;
for (bx, by) in &beacons {
if *by == *target_y && l <= *bx && r >= *bx {
overlap += 1;
}
}
sum += r - l + 1 - overlap;
}
println!("answer: {:?}", sum);
}

14
day15/test.txt Normal file
View File

@ -0,0 +1,14 @@
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3