1
0
Fork 0

Compare commits

..

2 Commits

Author SHA1 Message Date
Andrew Coleman 8f46feaf69 day 15 part 1 solution 2022-12-18 15:44:54 -05:00
Andrew Coleman 5113eba2a9 day14 part 2 solution 2022-12-18 14:46:26 -05:00
6 changed files with 147 additions and 8 deletions

31
2022/day15.txt Normal file
View File

@ -0,0 +1,31 @@
Sensor at x=3923513, y=2770279: closest beacon is at x=3866712, y=2438950
Sensor at x=675683, y=3223762: closest beacon is at x=-224297, y=2997209
Sensor at x=129453, y=2652332: closest beacon is at x=92656, y=2629486
Sensor at x=3906125, y=2154618: closest beacon is at x=3866712, y=2438950
Sensor at x=65723, y=902062: closest beacon is at x=92656, y=2629486
Sensor at x=3137156, y=2876347: closest beacon is at x=2907507, y=3100765
Sensor at x=32848, y=2676435: closest beacon is at x=92656, y=2629486
Sensor at x=3272472, y=3445147: closest beacon is at x=2907507, y=3100765
Sensor at x=2926008, y=128948: closest beacon is at x=3089364, y=-501737
Sensor at x=2975, y=2769838: closest beacon is at x=92656, y=2629486
Sensor at x=3540455, y=2469135: closest beacon is at x=3866712, y=2438950
Sensor at x=3674809, y=2062166: closest beacon is at x=3719980, y=2000000
Sensor at x=3693706, y=2027384: closest beacon is at x=3719980, y=2000000
Sensor at x=3869683, y=2291983: closest beacon is at x=3866712, y=2438950
Sensor at x=2666499, y=2796436: closest beacon is at x=2650643, y=2489479
Sensor at x=492, y=2601991: closest beacon is at x=92656, y=2629486
Sensor at x=2710282, y=3892347: closest beacon is at x=2907507, y=3100765
Sensor at x=28974, y=3971342: closest beacon is at x=-224297, y=2997209
Sensor at x=3990214, y=2399722: closest beacon is at x=3866712, y=2438950
Sensor at x=3853352, y=1009020: closest beacon is at x=3719980, y=2000000
Sensor at x=1231833, y=3999338: closest beacon is at x=1313797, y=4674300
Sensor at x=2083669, y=875035: closest beacon is at x=1369276, y=-160751
Sensor at x=1317274, y=2146819: closest beacon is at x=2650643, y=2489479
Sensor at x=3712875, y=2018770: closest beacon is at x=3719980, y=2000000
Sensor at x=963055, y=23644: closest beacon is at x=1369276, y=-160751
Sensor at x=3671967, y=64054: closest beacon is at x=3089364, y=-501737
Sensor at x=3109065, y=2222392: closest beacon is at x=2650643, y=2489479
Sensor at x=3218890, y=1517419: closest beacon is at x=3719980, y=2000000
Sensor at x=3856777, y=3987650: closest beacon is at x=4166706, y=3171774
Sensor at x=1912696, y=3392788: closest beacon is at x=2907507, y=3100765
Sensor at x=3597620, y=3100104: closest beacon is at x=4166706, y=3171774

View File

@ -9,7 +9,7 @@ enum Tile {
Sand, Sand,
} }
type Cave = [[Tile; 540]; 200]; type Cave = [[Tile; 800]; 200];
pub fn run() -> Result<()> { pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))] #[cfg(not(feature = "test_input"))]
@ -18,7 +18,7 @@ pub fn run() -> Result<()> {
#[cfg(feature = "test_input")] #[cfg(feature = "test_input")]
let file_contents = fs::read_to_string("tests/day14.txt")?; let file_contents = fs::read_to_string("tests/day14.txt")?;
let mut cave: Cave = [[Tile::Air; 540]; 200]; let mut cave: Cave = [[Tile::Air; 800]; 200];
cave[0][500] = Tile::Source; cave[0][500] = Tile::Source;
let mut minx = 520; let mut minx = 520;
let mut maxx = 500; let mut maxx = 500;
@ -76,8 +76,8 @@ pub fn run() -> Result<()> {
} }
} }
//print_cave(&cave, minx, 0, maxx, maxy);
part1_sand_units(&cave, minx, maxx, maxy); part1_sand_units(&cave, minx, maxx, maxy);
part2_sand_units(&cave, maxy);
Ok(()) Ok(())
} }
@ -134,23 +134,31 @@ fn part1_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
print_cave(&cave, minx, 0, maxx, maxy); print_cave(&cave, minx, 0, maxx, maxy);
} }
fn part2_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) { fn part2_sand_units(c: &Cave, maxy: usize) {
let mut cave = c.clone(); let mut cave = c.clone();
let mut units = 0; let mut units = 0;
let mut minx = 800;
let mut maxx = 0;
let floor = maxy + 2; let floor = maxy + 2;
for i in 0..540 { for i in 0..800 {
cave[floor][i] = Tile::Rock; cave[floor][i] = Tile::Rock;
} }
loop { loop {
if let Tile::Sand = cave[500][0] { if let Tile::Sand = cave[0][500] {
break; break;
} }
let mut x = 500; let mut x = 500;
let mut placed = false; let mut placed = false;
for i in 0..floor { for i in 0..floor {
if placed { if placed {
if x < minx {
minx = x;
}
if x > maxx {
maxx = x;
}
break; break;
} }
match cave[i + 1][x] { match cave[i + 1][x] {
@ -164,7 +172,6 @@ fn part2_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
} else { } else {
placed = true; placed = true;
cave[i][x] = Tile::Sand; cave[i][x] = Tile::Sand;
break;
} }
} }
} }
@ -176,5 +183,5 @@ fn part2_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
} }
} }
println!("part two units {}", units); println!("part two units {}", units);
print_cave(&cave, minx, 0, maxx, maxy); print_cave(&cave, minx, 0, maxx, floor);
} }

83
2022/src/days/day15.rs Normal file
View File

@ -0,0 +1,83 @@
use anyhow::Result;
use std::fs;
use std::collections::HashSet;
#[derive(Debug)]
struct Pair {
sensor: Coord,
beacon: Coord,
}
#[derive(Debug, Eq, Hash, PartialEq)]
struct Coord {
x: i32,
y: i32,
}
impl Pair {
fn distance(self: &Self) -> i32 {
(self.sensor.x - self.beacon.x).abs() + (self.sensor.y - self.beacon.y).abs()
}
}
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
let file_contents = fs::read_to_string("day15.txt")?;
#[cfg(feature = "test_input")]
let file_contents = fs::read_to_string("tests/day15.txt")?;
let mut pairs: Vec<Pair> = Vec::with_capacity(25);
for line in file_contents.lines() {
let mut parts = line.split(": ");
let spart = parts.next().unwrap().strip_prefix("Sensor at ").unwrap();
let bpart = parts.next().unwrap().strip_prefix("closest beacon is at ").unwrap();
let mut scoords = spart.split(", ");
let sx = scoords.next().unwrap().strip_prefix("x=").unwrap().parse::<i32>().unwrap();
let sy = scoords.next().unwrap().strip_prefix("y=").unwrap().parse::<i32>().unwrap();
let mut bcoords = bpart.split(", ");
let bx = bcoords.next().unwrap().strip_prefix("x=").unwrap().parse::<i32>().unwrap();
let by = bcoords.next().unwrap().strip_prefix("y=").unwrap().parse::<i32>().unwrap();
pairs.push(Pair {
sensor: Coord { x: sx, y: sy },
beacon: Coord { x: bx, y: by },
});
}
#[cfg(not(feature = "test_input"))]
spots_taken_on_row(&pairs, 2000000);
#[cfg(feature = "test_input")]
spots_taken_on_row(&pairs, 10);
// println!("pairs {:?}", pairs);
Ok(())
}
fn spots_taken_on_row(pairs: &Vec<Pair>, row: i32) {
let mut c: HashSet<i32> = HashSet::new();
for p in pairs.iter() {
let d = p.distance();
if row > p.sensor.y && row - p.sensor.y > d {
continue;
} else if row < p.sensor.y && p.sensor.y - row > d {
continue;
}
let remaining_distance = d - (row - p.sensor.y).abs();
let minx = p.sensor.x - remaining_distance;
let maxx = p.sensor.x + remaining_distance;
// println!("{:?} distance {} from {}..={}", p.sensor, d, minx, maxx);
for x in minx..=maxx {
c.insert(x);
}
}
for p in pairs.iter() {
if p.beacon.y == row {
c.remove(&p.beacon.x);
}
}
println!("spots taken on row {} = {}", row, c.len());
}

View File

@ -4,6 +4,7 @@ pub mod day11;
pub mod day12; pub mod day12;
pub mod day13; pub mod day13;
pub mod day14; pub mod day14;
pub mod day15;
pub mod day2; pub mod day2;
pub mod day3; pub mod day3;
pub mod day4; pub mod day4;

View File

@ -54,6 +54,9 @@ fn run_day(number: i32) -> Result<()> {
14 => { 14 => {
days::day14::run()?; days::day14::run()?;
} }
15 => {
days::day15::run()?;
}
_ => return Err(anyhow!("Invalid day provided")), _ => return Err(anyhow!("Invalid day provided")),
} }
Ok(()) Ok(())

14
2022/tests/day15.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