d17p2
This commit is contained in:
@@ -19,7 +19,7 @@ struct Shape {
|
||||
#[derive(Debug)]
|
||||
struct Pos {y: usize, x: usize}
|
||||
|
||||
static SHAPE_NAMES: [char; 5] = ['-', '+', 'L', '|', '.'];
|
||||
//static SHAPE_NAMES: [char; 5] = ['-', '+', 'L', '|', '.'];
|
||||
static SHAPES: [Shape; 5] = [
|
||||
Shape { // -
|
||||
bot: [0, 0, 0, 0], top: [0, 0, 0, 0],
|
||||
@@ -113,9 +113,11 @@ fn iterate(
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let input_fname = env::args().nth(1).expect("need input file");
|
||||
let limit = env::args().nth(2).expect("need limit").parse::<usize>().unwrap();
|
||||
let mut winds: Vec<u8> = Vec::new();
|
||||
let _ = BufReader::new(File::open(input_fname)?).read_until(b'\n', &mut winds);
|
||||
if winds.last() == Some(&b'\n') { winds.pop(); }
|
||||
println!("winds = {}, shapes = {}", winds.len(), SHAPES.len());
|
||||
|
||||
let mut shape_id = 0;
|
||||
let mut shape = &SHAPES[shape_id];
|
||||
@@ -127,13 +129,12 @@ fn main() -> io::Result<()> {
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
];
|
||||
prettyprint(&map, &shape_pos, SHAPE_NAMES[shape_id]);
|
||||
|
||||
let mut nrocks = 0;
|
||||
let mut answers: Vec<(usize, usize)> = Vec::new();
|
||||
|
||||
let mut i = 0;
|
||||
while nrocks < 2022 { //debug
|
||||
'mainloop: while answers.len() < limit {
|
||||
let c = winds[i % winds.len()];
|
||||
i += 1;
|
||||
if iterate(&mut map, shape, &mut shape_pos, c) {
|
||||
let last_shape_top = shape_pos.y + shape.height - 1;
|
||||
if last_shape_top > current_top {
|
||||
@@ -141,28 +142,69 @@ fn main() -> io::Result<()> {
|
||||
}
|
||||
shape_id = (shape_id + 1) % SHAPES.len(); // next shape
|
||||
shape = &SHAPES[shape_id];
|
||||
//println!("{} -> {}", map.len(), last_shape_top + 4 + shape.height);
|
||||
for _ in map.len()..(current_top + 4 + shape.height) {
|
||||
map.push([0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
nrocks += 1;
|
||||
shape_pos = Pos{x:2, y: current_top + 4};
|
||||
//println!("= {} ({})", current_top, nrocks);
|
||||
prettyprint(&map, &shape_pos, SHAPE_NAMES[shape_id]);
|
||||
answers.push((i, current_top));
|
||||
let last_rock = answers.len() - 1;
|
||||
print!(
|
||||
"rock {} - (i {}, top {})",
|
||||
last_rock, i, current_top
|
||||
);
|
||||
|
||||
if last_rock >= 2*SHAPES.len() {
|
||||
let mut j = last_rock - SHAPES.len();
|
||||
'lookback: while j > (last_rock / 2) {
|
||||
let (hist_i, hist_top) = answers[j];
|
||||
let next_j = 2*j - last_rock;
|
||||
let (hist2_i, hist2_top) = answers[next_j];
|
||||
if (i - hist_i) % winds.len() == 0
|
||||
&& (i - hist_i) == (hist_i - hist2_i)
|
||||
&& (current_top - hist_top) == (hist_top - hist2_top) {
|
||||
print!(
|
||||
" {}-{}-{}-({}-{}-{})",
|
||||
j, hist_i, hist_top,
|
||||
next_j, hist2_i, hist2_top,
|
||||
);
|
||||
print!(
|
||||
" rock cycle {}, off {} | \
|
||||
top cycle {} off {} ",
|
||||
last_rock - j, next_j,
|
||||
current_top - hist_top, hist2_top,
|
||||
);
|
||||
if (limit - 1 - next_j) % (last_rock - j) == 0 {
|
||||
let n_cycles = (limit - 1 - next_j) / (last_rock - j);
|
||||
println!(
|
||||
"\nheight = {}",
|
||||
hist2_top + n_cycles*(current_top - hist_top) + 1
|
||||
);
|
||||
break 'mainloop;
|
||||
} else {
|
||||
break 'lookback;
|
||||
}
|
||||
}
|
||||
j -= SHAPES.len();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
println!();
|
||||
//prettyprint(&map, &shape_pos, SHAPE_NAMES[shape_id]);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
println!("height = {}", current_top + 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prettyprint(map: &Vec<[u8; 7]>, pos: &Pos, shape_name: char) {
|
||||
// let mut i = map.len();
|
||||
// for row in map.into_iter().rev() {
|
||||
// i -= 1;
|
||||
// print!("|");
|
||||
// for j in 0..7 {
|
||||
// print!("{}", if row[j] == 0 { "." } else { "#" });
|
||||
// }
|
||||
// println!("| {} {}", i, if i == pos.y {shape_name} else {' '});
|
||||
// }
|
||||
let mut i = map.len();
|
||||
for row in map.into_iter().rev() {
|
||||
i -= 1;
|
||||
print!("|");
|
||||
for j in 0..7 {
|
||||
print!("{}", if row[j] == 0 { "." } else { "#" });
|
||||
}
|
||||
println!("| {} {}", i, if i == pos.y {shape_name} else {' '});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user