This commit is contained in:
Dory 2024-03-27 20:02:43 -07:00
parent c7a6c47f0e
commit 0b0e9bc0f4
2 changed files with 108 additions and 3 deletions

View File

@ -48,6 +48,13 @@ fn main() {
}
lists.push(list);
}
// part2 hack here
minx = 0;
maxx = 1000;
maxy = maxy + 2;
lists.push(vec![(minx, maxy), (maxx, maxy)]);
println!("{:?} {:?} {:?} {:?}", minx, miny, maxx, maxy);
let mut map: Vec<Vec<char>> =
vec![vec!['.'; maxx - minx + 3]; maxy + 1];
@ -74,9 +81,7 @@ fn main() {
let (mut rockx, mut rocky) = (500, 0);
'rock: loop {
//println!("{:?}, {:?}", rockx, rocky);
if rockx < minx || rockx > maxx || rocky == maxy {
break 'map;
} else if map[rocky + 1][rockx + 1 - minx] == '.' {
if map[rocky + 1][rockx + 1 - minx] == '.' {
rocky = rocky + 1;
} else if map[rocky + 1][rockx + 1 - minx - 1] == '.' {
rocky = rocky + 1;
@ -87,6 +92,9 @@ fn main() {
} else {
map[rocky][rockx - minx + 1] = 'o';
total += 1;
if rockx == 500 && rocky == 0 {
break 'map;
}
//print_map(&map);
break 'rock;
}

97
day14/src/main1.rs Normal file
View File

@ -0,0 +1,97 @@
use std::env;
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::cmp::{min, max};
fn parse_num(s: &[u8]) -> (usize, usize) {
let mut num: usize = 0;
let mut len = 0;
for i in 0..s.len() {
if s[i].is_ascii_digit() {
num = num*10 + (s[i] as usize - '0' as usize);
len += 1;
} else {
break;
}
}
(num, len)
}
fn print_map(map: &Vec<Vec<char>>) {
for row in map {
let s: String = row.iter().collect();
println!("{}", s);
}
println!("");
}
fn main() {
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
let lines = BufReader::new(file).lines();
let (mut minx, mut miny, mut maxx, mut maxy) =
(usize::MAX, usize::MAX, 0, 0);
let mut lists: Vec<Vec<(usize, usize)>> = Vec::new();
for line in lines.flatten() {
let l_bytes = line.as_bytes();
let mut cursor = 0;
let mut list: Vec<(usize, usize)> = Vec::new();
while cursor < l_bytes.len() {
let (x, lenx) = parse_num(&l_bytes[cursor..]);
let (y, leny) = parse_num(&l_bytes[cursor + lenx + 1..]);
list.push((x, y));
minx = if x < minx {x} else {minx};
miny = if y < miny {y} else {miny};
maxx = if x > maxx {x} else {maxx};
maxy = if y > maxy {y} else {maxy};
cursor = cursor + lenx + 1 + leny + 4;
}
lists.push(list);
}
println!("{:?} {:?} {:?} {:?}", minx, miny, maxx, maxy);
let mut map: Vec<Vec<char>> =
vec![vec!['.'; maxx - minx + 3]; maxy + 1];
for list in lists {
let (mut xprev, mut yprev) = &list[0];
for (x, y) in &list[1..] {
if *x == xprev {
for ty in *min(y, &yprev)..=*max(y, &yprev) {
map[ty][*x - minx + 1] = '#';
}
} else if *y == yprev {
for tx in *min(x, &xprev)..=*max(x, &xprev) {
map[*y][tx - minx + 1] = '#';
}
}
xprev = *x;
yprev = *y;
}
}
print_map(&map);
let mut total = 0;
'map: loop {
let (mut rockx, mut rocky) = (500, 0);
'rock: loop {
//println!("{:?}, {:?}", rockx, rocky);
if rockx < minx || rockx > maxx || rocky == maxy {
break 'map;
} else if map[rocky + 1][rockx + 1 - minx] == '.' {
rocky = rocky + 1;
} else if map[rocky + 1][rockx + 1 - minx - 1] == '.' {
rocky = rocky + 1;
rockx = rockx - 1;
} else if map[rocky + 1][rockx + 1 - minx + 1] == '.' {
rocky = rocky + 1;
rockx = rockx + 1;
} else {
map[rocky][rockx - minx + 1] = 'o';
total += 1;
//print_map(&map);
break 'rock;
}
}
}
print_map(&map);
println!("{:?}", total);
}