day 14 part 1 solution
parent
f88eeb1f91
commit
bbe7c7332f
|
@ -0,0 +1,124 @@
|
|||
495,144 -> 499,144
|
||||
492,40 -> 492,43 -> 484,43 -> 484,50 -> 499,50 -> 499,43 -> 496,43 -> 496,40
|
||||
499,13 -> 499,17 -> 497,17 -> 497,25 -> 505,25 -> 505,17 -> 502,17 -> 502,13
|
||||
504,141 -> 508,141
|
||||
498,135 -> 502,135
|
||||
499,13 -> 499,17 -> 497,17 -> 497,25 -> 505,25 -> 505,17 -> 502,17 -> 502,13
|
||||
511,101 -> 511,96 -> 511,101 -> 513,101 -> 513,93 -> 513,101 -> 515,101 -> 515,94 -> 515,101
|
||||
485,147 -> 490,147
|
||||
483,153 -> 488,153
|
||||
479,151 -> 484,151
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
510,141 -> 514,141
|
||||
498,53 -> 498,57 -> 495,57 -> 495,64 -> 508,64 -> 508,57 -> 502,57 -> 502,53
|
||||
511,101 -> 511,96 -> 511,101 -> 513,101 -> 513,93 -> 513,101 -> 515,101 -> 515,94 -> 515,101
|
||||
513,114 -> 513,110 -> 513,114 -> 515,114 -> 515,110 -> 515,114 -> 517,114 -> 517,107 -> 517,114
|
||||
476,153 -> 481,153
|
||||
506,83 -> 506,85 -> 503,85 -> 503,88 -> 513,88 -> 513,85 -> 511,85 -> 511,83
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
506,83 -> 506,85 -> 503,85 -> 503,88 -> 513,88 -> 513,85 -> 511,85 -> 511,83
|
||||
515,165 -> 520,165
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
508,165 -> 513,165
|
||||
494,155 -> 499,155
|
||||
506,83 -> 506,85 -> 503,85 -> 503,88 -> 513,88 -> 513,85 -> 511,85 -> 511,83
|
||||
487,30 -> 487,31 -> 499,31 -> 499,30
|
||||
497,129 -> 502,129
|
||||
473,155 -> 478,155
|
||||
517,123 -> 522,123
|
||||
511,101 -> 511,96 -> 511,101 -> 513,101 -> 513,93 -> 513,101 -> 515,101 -> 515,94 -> 515,101
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
506,83 -> 506,85 -> 503,85 -> 503,88 -> 513,88 -> 513,85 -> 511,85 -> 511,83
|
||||
489,149 -> 494,149
|
||||
507,126 -> 512,126
|
||||
489,144 -> 493,144
|
||||
506,83 -> 506,85 -> 503,85 -> 503,88 -> 513,88 -> 513,85 -> 511,85 -> 511,83
|
||||
513,114 -> 513,110 -> 513,114 -> 515,114 -> 515,110 -> 515,114 -> 517,114 -> 517,107 -> 517,114
|
||||
486,151 -> 491,151
|
||||
498,53 -> 498,57 -> 495,57 -> 495,64 -> 508,64 -> 508,57 -> 502,57 -> 502,53
|
||||
521,126 -> 526,126
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
487,155 -> 492,155
|
||||
513,120 -> 518,120
|
||||
499,13 -> 499,17 -> 497,17 -> 497,25 -> 505,25 -> 505,17 -> 502,17 -> 502,13
|
||||
482,149 -> 487,149
|
||||
500,158 -> 500,159 -> 513,159
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
499,13 -> 499,17 -> 497,17 -> 497,25 -> 505,25 -> 505,17 -> 502,17 -> 502,13
|
||||
492,40 -> 492,43 -> 484,43 -> 484,50 -> 499,50 -> 499,43 -> 496,43 -> 496,40
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
513,114 -> 513,110 -> 513,114 -> 515,114 -> 515,110 -> 515,114 -> 517,114 -> 517,107 -> 517,114
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
499,13 -> 499,17 -> 497,17 -> 497,25 -> 505,25 -> 505,17 -> 502,17 -> 502,13
|
||||
505,168 -> 510,168
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
492,40 -> 492,43 -> 484,43 -> 484,50 -> 499,50 -> 499,43 -> 496,43 -> 496,40
|
||||
513,114 -> 513,110 -> 513,114 -> 515,114 -> 515,110 -> 515,114 -> 517,114 -> 517,107 -> 517,114
|
||||
509,117 -> 514,117
|
||||
498,53 -> 498,57 -> 495,57 -> 495,64 -> 508,64 -> 508,57 -> 502,57 -> 502,53
|
||||
511,101 -> 511,96 -> 511,101 -> 513,101 -> 513,93 -> 513,101 -> 515,101 -> 515,94 -> 515,101
|
||||
498,53 -> 498,57 -> 495,57 -> 495,64 -> 508,64 -> 508,57 -> 502,57 -> 502,53
|
||||
503,123 -> 508,123
|
||||
511,101 -> 511,96 -> 511,101 -> 513,101 -> 513,93 -> 513,101 -> 515,101 -> 515,94 -> 515,101
|
||||
513,114 -> 513,110 -> 513,114 -> 515,114 -> 515,110 -> 515,114 -> 517,114 -> 517,107 -> 517,114
|
||||
498,53 -> 498,57 -> 495,57 -> 495,64 -> 508,64 -> 508,57 -> 502,57 -> 502,53
|
||||
510,123 -> 515,123
|
||||
511,101 -> 511,96 -> 511,101 -> 513,101 -> 513,93 -> 513,101 -> 515,101 -> 515,94 -> 515,101
|
||||
513,144 -> 517,144
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
501,138 -> 505,138
|
||||
507,144 -> 511,144
|
||||
513,114 -> 513,110 -> 513,114 -> 515,114 -> 515,110 -> 515,114 -> 517,114 -> 517,107 -> 517,114
|
||||
498,53 -> 498,57 -> 495,57 -> 495,64 -> 508,64 -> 508,57 -> 502,57 -> 502,53
|
||||
492,40 -> 492,43 -> 484,43 -> 484,50 -> 499,50 -> 499,43 -> 496,43 -> 496,40
|
||||
492,141 -> 496,141
|
||||
495,138 -> 499,138
|
||||
513,114 -> 513,110 -> 513,114 -> 515,114 -> 515,110 -> 515,114 -> 517,114 -> 517,107 -> 517,114
|
||||
492,40 -> 492,43 -> 484,43 -> 484,50 -> 499,50 -> 499,43 -> 496,43 -> 496,40
|
||||
498,141 -> 502,141
|
||||
501,155 -> 506,155
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
497,153 -> 502,153
|
||||
504,129 -> 509,129
|
||||
518,129 -> 523,129
|
||||
480,155 -> 485,155
|
||||
513,114 -> 513,110 -> 513,114 -> 515,114 -> 515,110 -> 515,114 -> 517,114 -> 517,107 -> 517,114
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
506,83 -> 506,85 -> 503,85 -> 503,88 -> 513,88 -> 513,85 -> 511,85 -> 511,83
|
||||
498,53 -> 498,57 -> 495,57 -> 495,64 -> 508,64 -> 508,57 -> 502,57 -> 502,53
|
||||
492,40 -> 492,43 -> 484,43 -> 484,50 -> 499,50 -> 499,43 -> 496,43 -> 496,40
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
525,129 -> 530,129
|
||||
504,135 -> 508,135
|
||||
487,30 -> 487,31 -> 499,31 -> 499,30
|
||||
487,30 -> 487,31 -> 499,31 -> 499,30
|
||||
493,151 -> 498,151
|
||||
514,126 -> 519,126
|
||||
511,101 -> 511,96 -> 511,101 -> 513,101 -> 513,93 -> 513,101 -> 515,101 -> 515,94 -> 515,101
|
||||
507,138 -> 511,138
|
||||
495,37 -> 506,37
|
||||
511,101 -> 511,96 -> 511,101 -> 513,101 -> 513,93 -> 513,101 -> 515,101 -> 515,94 -> 515,101
|
||||
500,126 -> 505,126
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
519,168 -> 524,168
|
||||
511,162 -> 516,162
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
499,13 -> 499,17 -> 497,17 -> 497,25 -> 505,25 -> 505,17 -> 502,17 -> 502,13
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
492,40 -> 492,43 -> 484,43 -> 484,50 -> 499,50 -> 499,43 -> 496,43 -> 496,40
|
||||
506,120 -> 511,120
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
501,132 -> 505,132
|
||||
511,129 -> 516,129
|
||||
494,80 -> 507,80
|
||||
506,83 -> 506,85 -> 503,85 -> 503,88 -> 513,88 -> 513,85 -> 511,85 -> 511,83
|
||||
501,144 -> 505,144
|
||||
490,153 -> 495,153
|
||||
512,168 -> 517,168
|
||||
490,77 -> 490,68 -> 490,77 -> 492,77 -> 492,75 -> 492,77 -> 494,77 -> 494,74 -> 494,77 -> 496,77 -> 496,72 -> 496,77
|
||||
507,181 -> 507,171 -> 507,181 -> 509,181 -> 509,173 -> 509,181 -> 511,181 -> 511,176 -> 511,181 -> 513,181 -> 513,176 -> 513,181
|
||||
499,13 -> 499,17 -> 497,17 -> 497,25 -> 505,25 -> 505,17 -> 502,17 -> 502,13
|
||||
500,158 -> 500,159 -> 513,159
|
|
@ -0,0 +1,180 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use std::fs;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Tile {
|
||||
Air,
|
||||
Rock,
|
||||
Source,
|
||||
Sand,
|
||||
}
|
||||
|
||||
type Cave = [[Tile; 540]; 200];
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
#[cfg(not(feature = "test_input"))]
|
||||
let file_contents = fs::read_to_string("day14.txt")?;
|
||||
|
||||
#[cfg(feature = "test_input")]
|
||||
let file_contents = fs::read_to_string("tests/day14.txt")?;
|
||||
|
||||
let mut cave: Cave = [[Tile::Air; 540]; 200];
|
||||
cave[0][500] = Tile::Source;
|
||||
let mut minx = 520;
|
||||
let mut maxx = 500;
|
||||
let mut maxy = 0;
|
||||
for line in file_contents.lines() {
|
||||
let mut lastx = 0;
|
||||
let mut lasty = 0;
|
||||
for p in line.split(" -> ").map(|e| {
|
||||
e.split(",")
|
||||
.map(|f| f.parse::<usize>().unwrap())
|
||||
.collect::<Vec<usize>>()
|
||||
}) {
|
||||
if minx > p[0] {
|
||||
minx = p[0];
|
||||
}
|
||||
if maxx < p[0] {
|
||||
maxx = p[0];
|
||||
}
|
||||
if maxy < p[1] {
|
||||
maxy = p[1];
|
||||
}
|
||||
|
||||
if lastx == 0 && lasty == 0 {
|
||||
lastx = p[0];
|
||||
lasty = p[1];
|
||||
continue;
|
||||
}
|
||||
|
||||
if lastx == p[0] {
|
||||
// vertical
|
||||
let r = if lasty < p[1] {
|
||||
lasty..=p[1]
|
||||
} else {
|
||||
p[1]..=lasty
|
||||
};
|
||||
for y in r {
|
||||
cave[y][lastx] = Tile::Rock;
|
||||
}
|
||||
} else if lasty == p[1] {
|
||||
// horizontal
|
||||
let r = if lastx < p[0] {
|
||||
lastx..=p[0]
|
||||
} else {
|
||||
p[0]..=lastx
|
||||
};
|
||||
for x in r {
|
||||
cave[lasty][x] = Tile::Rock;
|
||||
}
|
||||
} else {
|
||||
return Err(anyhow!("diagonal line"));
|
||||
}
|
||||
|
||||
lastx = p[0];
|
||||
lasty = p[1];
|
||||
}
|
||||
}
|
||||
|
||||
//print_cave(&cave, minx, 0, maxx, maxy);
|
||||
part1_sand_units(&cave, minx, maxx, maxy);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_cave(cave: &Cave, minx: usize, miny: usize, maxx: usize, maxy: usize) {
|
||||
println!("cave {},{} -> {},{}", minx, miny, maxx, maxy);
|
||||
for y in miny..=maxy {
|
||||
for x in minx..=maxx {
|
||||
match cave[y][x] {
|
||||
Tile::Air => print!("."),
|
||||
Tile::Rock => print!("#"),
|
||||
Tile::Source => print!("+"),
|
||||
Tile::Sand => print!("o"),
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
fn part1_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
|
||||
let mut cave = c.clone();
|
||||
let mut units = 0;
|
||||
|
||||
loop {
|
||||
let mut x = 500;
|
||||
let mut placed = false;
|
||||
for i in 0..200 {
|
||||
if i > maxy || placed {
|
||||
break;
|
||||
}
|
||||
match cave[i + 1][x] {
|
||||
Tile::Air => {}
|
||||
Tile::Source => {}
|
||||
_ => {
|
||||
if let Tile::Air = cave[i + 1][x - 1] {
|
||||
x -= 1;
|
||||
} else if let Tile::Air = cave[i + 1][x + 1] {
|
||||
x += 1;
|
||||
} else {
|
||||
placed = true;
|
||||
cave[i][x] = Tile::Sand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if placed {
|
||||
units += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("part one units {}", units);
|
||||
print_cave(&cave, minx, 0, maxx, maxy);
|
||||
}
|
||||
|
||||
fn part2_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
|
||||
let mut cave = c.clone();
|
||||
let mut units = 0;
|
||||
|
||||
let floor = maxy + 2;
|
||||
for i in 0..540 {
|
||||
cave[floor][i] = Tile::Rock;
|
||||
}
|
||||
|
||||
loop {
|
||||
if let Tile::Sand = cave[500][0] {
|
||||
break;
|
||||
}
|
||||
let mut x = 500;
|
||||
let mut placed = false;
|
||||
for i in 0..floor {
|
||||
if placed {
|
||||
break;
|
||||
}
|
||||
match cave[i + 1][x] {
|
||||
Tile::Air => {}
|
||||
Tile::Source => {}
|
||||
_ => {
|
||||
if let Tile::Air = cave[i + 1][x - 1] {
|
||||
x -= 1;
|
||||
} else if let Tile::Air = cave[i + 1][x + 1] {
|
||||
x += 1;
|
||||
} else {
|
||||
placed = true;
|
||||
cave[i][x] = Tile::Sand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if placed {
|
||||
units += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("part two units {}", units);
|
||||
print_cave(&cave, minx, 0, maxx, maxy);
|
||||
}
|
|
@ -3,6 +3,7 @@ pub mod day10;
|
|||
pub mod day11;
|
||||
pub mod day12;
|
||||
pub mod day13;
|
||||
pub mod day14;
|
||||
pub mod day2;
|
||||
pub mod day3;
|
||||
pub mod day4;
|
||||
|
|
|
@ -51,6 +51,9 @@ fn run_day(number: i32) -> Result<()> {
|
|||
13 => {
|
||||
days::day13::run()?;
|
||||
}
|
||||
14 => {
|
||||
days::day14::run()?;
|
||||
}
|
||||
_ => return Err(anyhow!("Invalid day provided")),
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
498,4 -> 498,6 -> 496,6
|
||||
503,4 -> 502,4 -> 502,9 -> 494,9
|
Loading…
Reference in New Issue