2022 rust day 15 wip

This commit is contained in:
Maciej Jur 2022-12-15 13:40:51 +01:00
parent 0a53a19147
commit 5aa8c83a30
4 changed files with 53 additions and 43 deletions

View file

@ -0,0 +1,29 @@
Sensor at x=3797530, y=3451192: closest beacon is at x=3316341, y=3328308
Sensor at x=3779164, y=33938: closest beacon is at x=4608350, y=708806
Sensor at x=1331810, y=3260896: closest beacon is at x=2075597, y=3280016
Sensor at x=393374, y=696899: closest beacon is at x=2021690, y=453306
Sensor at x=2928048, y=923094: closest beacon is at x=2021690, y=453306
Sensor at x=2386726, y=3645023: closest beacon is at x=2075597, y=3280016
Sensor at x=1900159, y=2381031: closest beacon is at x=1649961, y=2000000
Sensor at x=2601378, y=2979844: closest beacon is at x=2218962, y=2701963
Sensor at x=2254818, y=32199: closest beacon is at x=2021690, y=453306
Sensor at x=2689643, y=375840: closest beacon is at x=2021690, y=453306
Sensor at x=909141, y=2842547: closest beacon is at x=2218962, y=2701963
Sensor at x=3915731, y=2454320: closest beacon is at x=4268501, y=1853073
Sensor at x=1693574, y=1344104: closest beacon is at x=1649961, y=2000000
Sensor at x=1760260, y=3297662: closest beacon is at x=2075597, y=3280016
Sensor at x=1909567, y=3990737: closest beacon is at x=2075597, y=3280016
Sensor at x=2097863, y=3179766: closest beacon is at x=2075597, y=3280016
Sensor at x=3100489, y=3623847: closest beacon is at x=3104748, y=4102403
Sensor at x=2746023, y=2432826: closest beacon is at x=2218962, y=2701963
Sensor at x=3031245, y=3031354: closest beacon is at x=3316341, y=3328308
Sensor at x=277094, y=1999350: closest beacon is at x=1649961, y=2000000
Sensor at x=1763269, y=126349: closest beacon is at x=2021690, y=453306
Sensor at x=3287624, y=2695420: closest beacon is at x=3316341, y=3328308
Sensor at x=2371102, y=1745103: closest beacon is at x=1649961, y=2000000
Sensor at x=3553438, y=1563379: closest beacon is at x=4268501, y=1853073
Sensor at x=1529129, y=2735122: closest beacon is at x=2218962, y=2701963
Sensor at x=2826220, y=3958350: closest beacon is at x=3104748, y=4102403
Sensor at x=3999334, y=3912693: closest beacon is at x=3104748, y=4102403
Sensor at x=240430, y=3829436: closest beacon is at x=-742036, y=3963149
Sensor at x=3455748, y=3814861: closest beacon is at x=3316341, y=3328308

View file

@ -3,5 +3,5 @@ mod solutions;
fn main() {
solutions::day14::run();
solutions::day15::run();
}

View file

@ -5,7 +5,7 @@ use crate::utils::matrix::Matrix;
pub fn run() -> () {
let data = parse_data(&utils::read_lines(utils::Source::Day(-1)));
let data = parse_data(&utils::read_lines(utils::Source::Day(15)));
println!("Day 15");
println!("Part 1: {}", solve1(&data));
@ -43,32 +43,7 @@ fn solve1(data: &Data) -> usize {
let ranges = find_ranges(data);
let (rows, cols) = find_bounds(data);
let max_range = *ranges.values().max().unwrap();
let buffer = 2 * max_range;
let mut grid = Matrix::with_bounds(
(0, 0),
(rows + buffer, cols + buffer),
'.'
);
let mut view = grid.view_translated((-(max_range as isize), -(max_range as isize)));
for index in view.cell_indices().collect::<Vec<_>>() {
for (&sensor, &max_dist) in ranges.iter() {
if distance(sensor, index) <= max_dist {
view[index] = '#';
}
}
}
grid.iter_rows().for_each(|row| {
let xd = row.iter().filter(|&&x| x == '#').count();
println!("{xd}");
});
println!("{grid}");
grid.slice_row(max_range + 10).iter().filter(|&&x| x == '#').count()
1
}
fn solve2(data: &Data) -> i32 {
@ -108,20 +83,20 @@ mod tests {
use super::*;
static DATA: &[&str] = &[
// "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=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",
"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",
];
#[test]

View file

@ -1,6 +1,7 @@
#![allow(dead_code)]
use std::fmt;
use std::ops::{Index, IndexMut};
use std::ptr::write;
use std::slice::{ChunksExact, Iter};
@ -151,9 +152,10 @@ impl<T> FromIterator<T> for Matrix<T> {
impl<T> fmt::Display for Matrix<T> where T: fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for row in self.iter_rows() {
for (index, row) in self.iter_rows().enumerate() {
write!(f, "{index:<3}")?;
for item in row {
write!(f, "{}", item)?
write!(f, "{item}")?
};
write!(f, "\n")?;
};
@ -195,6 +197,10 @@ impl<'a, T> TranslatedViewMut<'a, T> {
let row = row - self.tl_row;
self.slice_row(row)
}
pub fn get_tl(&self) -> (isize, isize) {
(self.tl_row, self.tl_col)
}
}
impl<'a, T> Index<(isize, isize)> for TranslatedViewMut<'a, T> {