1
0

Added up day 15

This commit is contained in:
Ishan Jain 2022-12-15 14:32:05 +05:30
parent c3f94ad140
commit ab213c672a
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
4 changed files with 263 additions and 0 deletions

View File

@ -0,0 +1,105 @@
#![feature(test)]
use std::collections::HashSet;
extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
#[derive(Debug)]
struct Sensor {
sx: i32,
sy: i32,
bx: i32,
by: i32,
}
fn parse(input: &str) -> Vec<Sensor> {
input
.lines()
.filter(|c| !c.is_empty())
.filter_map(|line| {
line.split_once(':').map(|(sensor, beacon)| {
let (sx, sy) = sensor
.split_once(',')
.map(|(x, y)| {
let xneg = x.contains('-');
let yneg = y.contains('-');
let x = x
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as i32);
let y = y
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as i32);
(if xneg { -x } else { x }, if yneg { -y } else { y })
})
.unwrap();
let (bx, by) = beacon
.split_once(',')
.map(|(x, y)| {
let xneg = x.contains('-');
let yneg = y.contains('-');
let x = x
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as i32);
let y = y
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as i32);
(if xneg { -x } else { x }, if yneg { -y } else { y })
})
.unwrap();
Sensor { sx, sy, bx, by }
})
})
.collect()
}
fn solution(mut input: Vec<Sensor>, line: i32) -> usize {
let mut beacons = HashSet::new();
let mut set = HashSet::new();
input.sort_unstable_by_key(|c| c.sx);
for Sensor { sx, sy, bx, by } in input {
if by == line {
beacons.insert(bx);
}
let area = (bx - sx).abs() + (by - sy).abs();
for ix in sx - area..=sx + area {
if inside((sx, sy), (ix, line)) <= area {
set.insert(ix);
};
}
}
set.len() - beacons.len()
}
#[inline]
const fn inside((sx, sy): (i32, i32), (px, py): (i32, i32)) -> i32 {
(sx - px).abs() + (sy - py).abs()
}
fn main() {
let output = parse(INPUTS[0]);
let score = solution(output, 10);
println!("{}", score);
let output = parse(INPUTS[1]);
let score = solution(output, 2000000);
println!("{}", score);
}
#[bench]
fn solution_bench(b: &mut test::Bencher) {
b.iter(|| {
let input = parse(INPUTS[1]);
let result = solution(input, 2000000);
test::black_box(result);
})
}

View File

@ -0,0 +1,106 @@
#![feature(test)]
extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
#[derive(Debug)]
struct Sensor {
sx: i64,
sy: i64,
area: i64,
}
fn parse(input: &str) -> Vec<Sensor> {
input
.lines()
.filter(|c| !c.is_empty())
.filter_map(|line| {
line.split_once(':').map(|(sensor, beacon)| {
let (sx, sy) = sensor
.split_once(',')
.map(|(x, y)| {
let xneg = x.contains('-');
let yneg = y.contains('-');
let x = x
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as i64);
let y = y
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as i64);
(if xneg { -x } else { x }, if yneg { -y } else { y })
})
.unwrap();
let (bx, by) = beacon
.split_once(',')
.map(|(x, y)| {
let xneg = x.contains('-');
let yneg = y.contains('-');
let x = x
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as i64);
let y = y
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as i64);
(if xneg { -x } else { x }, if yneg { -y } else { y })
})
.unwrap();
Sensor {
sx,
sy,
area: (sx - bx).abs() + (sy - by).abs(),
}
})
})
.collect()
}
fn solution(input: Vec<Sensor>, line: i64) -> i64 {
for j in 0..=line {
let mut i = 0;
'out: while i <= line {
for beacon in input.iter() {
if inside((beacon.sx, beacon.sy), (i, j)) <= beacon.area {
let dy = (j - beacon.sy).abs();
let dx = (beacon.sx - i) + (beacon.area - dy) + 1;
i += dx;
continue 'out;
}
}
return i * 4000000 + j;
}
}
0
}
#[inline]
const fn inside((sx, sy): (i64, i64), (px, py): (i64, i64)) -> i64 {
(sx - px).abs() + (sy - py).abs()
}
fn main() {
let output = parse(INPUTS[0]);
let score = solution(output, 20);
println!("{}", score);
let output = parse(INPUTS[1]);
let score = solution(output, 4000000);
println!("{}", score);
}
#[bench]
fn solution_bench(b: &mut test::Bencher) {
b.iter(|| {
let input = parse(INPUTS[1]);
let result = solution(input, 4000000);
test::black_box(result);
})
}

38
src/day15/input.txt Normal file
View File

@ -0,0 +1,38 @@
Sensor at x=220580, y=684270: closest beacon is at x=436611, y=263737
Sensor at x=3329538, y=3016377: closest beacon is at x=3355914, y=2862466
Sensor at x=2605308, y=2023938: closest beacon is at x=2197530, y=2271330
Sensor at x=1810202, y=3423309: closest beacon is at x=1829362, y=3182862
Sensor at x=480296, y=3999646: closest beacon is at x=1694700, y=4178942
Sensor at x=46556, y=1283362: closest beacon is at x=-91140, y=1441882
Sensor at x=3741660, y=3959257: closest beacon is at x=3537901, y=3368697
Sensor at x=3399994, y=700264: closest beacon is at x=3748004, y=2000000
Sensor at x=1531981, y=3801761: closest beacon is at x=1694700, y=4178942
Sensor at x=193367, y=2712458: closest beacon is at x=-91140, y=1441882
Sensor at x=3199067, y=2194575: closest beacon is at x=3748004, y=2000000
Sensor at x=1878117, y=2578817: closest beacon is at x=2197530, y=2271330
Sensor at x=2439089, y=3168242: closest beacon is at x=1829362, y=3182862
Sensor at x=273443, y=171076: closest beacon is at x=436611, y=263737
Sensor at x=3680413, y=2477027: closest beacon is at x=3748004, y=2000000
Sensor at x=3620241, y=2904998: closest beacon is at x=3355914, y=2862466
Sensor at x=1728351, y=2895399: closest beacon is at x=1829362, y=3182862
Sensor at x=1894207, y=1168355: closest beacon is at x=2197530, y=2271330
Sensor at x=856867, y=3271314: closest beacon is at x=1829362, y=3182862
Sensor at x=3056788, y=2626224: closest beacon is at x=3355914, y=2862466
Sensor at x=3598024, y=3322247: closest beacon is at x=3537901, y=3368697
Sensor at x=1662543, y=3128823: closest beacon is at x=1829362, y=3182862
Sensor at x=3992558, y=1933059: closest beacon is at x=3748004, y=2000000
Sensor at x=1844282, y=2994285: closest beacon is at x=1829362, y=3182862
Sensor at x=3604375, y=3668021: closest beacon is at x=3537901, y=3368697
Sensor at x=2569893, y=3911832: closest beacon is at x=1694700, y=4178942
Sensor at x=117970, y=37503: closest beacon is at x=436611, y=263737
Sensor at x=3951385, y=3125577: closest beacon is at x=3537901, y=3368697
Sensor at x=2482373, y=2648092: closest beacon is at x=2197530, y=2271330
Sensor at x=915040, y=1835970: closest beacon is at x=-91140, y=1441882
Sensor at x=3047883, y=3301452: closest beacon is at x=3537901, y=3368697
Sensor at x=117432, y=1503889: closest beacon is at x=-91140, y=1441882
Sensor at x=1136011, y=261705: closest beacon is at x=436611, y=263737
Sensor at x=2343111, y=66183: closest beacon is at x=2081841, y=-807749
Sensor at x=608229, y=955721: closest beacon is at x=436611, y=263737
Sensor at x=1189379, y=3999750: closest beacon is at x=1694700, y=4178942
Sensor at x=766640, y=26597: closest beacon is at x=436611, y=263737
Sensor at x=3891093, y=2110588: closest beacon is at x=3748004, y=2000000

14
src/day15/sample.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