cargo fmt
parent
d3690d856e
commit
c82b633b65
|
@ -31,8 +31,14 @@ fn find_range_in_line(line: &str) -> (Range<usize>, Range<usize>) {
|
|||
let mut right_numbers = right_parts.nth(1).unwrap().split("..");
|
||||
let right_number_1: usize = right_numbers.nth(0).unwrap().parse().unwrap();
|
||||
let right_number_2: usize = right_numbers.nth(0).unwrap().parse().unwrap();
|
||||
let left_range = Range { start: left_number, end: left_number + 1 };
|
||||
let right_range = Range { start: right_number_1, end: right_number_2 + 1 };
|
||||
let left_range = Range {
|
||||
start: left_number,
|
||||
end: left_number + 1,
|
||||
};
|
||||
let right_range = Range {
|
||||
start: right_number_1,
|
||||
end: right_number_2 + 1,
|
||||
};
|
||||
if left_coord_type == "x" {
|
||||
(left_range, right_range)
|
||||
} else {
|
||||
|
@ -84,32 +90,62 @@ fn build_map_from_file(filename: &str) -> (Map, MapBounds) {
|
|||
|
||||
fn pour_over(m: &mut Map, start_x: usize, start_y: usize) {
|
||||
let mut frontier: VecDeque<Coord> = VecDeque::new();
|
||||
frontier.push_back(Coord { x: start_x, y: start_y });
|
||||
frontier.push_back(Coord {
|
||||
x: start_x,
|
||||
y: start_y,
|
||||
});
|
||||
|
||||
while let Some(coord) = frontier.pop_front() {
|
||||
match m[coord.x][coord.y] {
|
||||
FOUNTAIN => frontier.push_back(Coord { x: coord.x, y: coord.y + 1 }),
|
||||
FOUNTAIN => frontier.push_back(Coord {
|
||||
x: coord.x,
|
||||
y: coord.y + 1,
|
||||
}),
|
||||
EMPTY_SPACE => {
|
||||
m[coord.x][coord.y] = FALLING_WATER;
|
||||
if coord.y + 1 < MAP_SIZE {
|
||||
match m[coord.x][coord.y + 1] {
|
||||
WALL_SPACE => frontier.push_back(Coord { x: coord.x, y: coord.y }),
|
||||
EMPTY_SPACE => frontier.push_back(Coord { x: coord.x, y: coord.y + 1 }),
|
||||
FALLING_WATER => frontier.push_back(Coord { x: coord.x, y: coord.y }),
|
||||
WALL_SPACE => frontier.push_back(Coord {
|
||||
x: coord.x,
|
||||
y: coord.y,
|
||||
}),
|
||||
EMPTY_SPACE => frontier.push_back(Coord {
|
||||
x: coord.x,
|
||||
y: coord.y + 1,
|
||||
}),
|
||||
FALLING_WATER => frontier.push_back(Coord {
|
||||
x: coord.x,
|
||||
y: coord.y,
|
||||
}),
|
||||
WATER => {
|
||||
if m[coord.x + 1][coord.y] == WALL_SPACE || m[coord.x - 1][coord.y] == WALL_SPACE {
|
||||
frontier.push_back(Coord { x: coord.x, y: coord.y });
|
||||
} else if m[coord.x + 2][coord.y] == WALL_SPACE || m[coord.x - 2][coord.y] == WALL_SPACE {
|
||||
frontier.push_back(Coord { x: coord.x, y: coord.y });
|
||||
} else if m[coord.x + 1][coord.y] == FALLING_WATER || m[coord.x - 1][coord.y] == FALLING_WATER {
|
||||
frontier.push_back(Coord { x: coord.x, y: coord.y });
|
||||
if m[coord.x + 1][coord.y] == WALL_SPACE
|
||||
|| m[coord.x - 1][coord.y] == WALL_SPACE
|
||||
{
|
||||
frontier.push_back(Coord {
|
||||
x: coord.x,
|
||||
y: coord.y,
|
||||
});
|
||||
} else if m[coord.x + 2][coord.y] == WALL_SPACE
|
||||
|| m[coord.x - 2][coord.y] == WALL_SPACE
|
||||
{
|
||||
frontier.push_back(Coord {
|
||||
x: coord.x,
|
||||
y: coord.y,
|
||||
});
|
||||
} else if m[coord.x + 1][coord.y] == FALLING_WATER
|
||||
|| m[coord.x - 1][coord.y] == FALLING_WATER
|
||||
{
|
||||
frontier.push_back(Coord {
|
||||
x: coord.x,
|
||||
y: coord.y,
|
||||
});
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
},
|
||||
WALL_SPACE => {},
|
||||
}
|
||||
WALL_SPACE => {}
|
||||
FALLING_WATER => {
|
||||
let mut found_down = false;
|
||||
// go left as far as possible
|
||||
|
@ -126,9 +162,9 @@ fn pour_over(m: &mut Map, start_x: usize, start_y: usize) {
|
|||
} else {
|
||||
m[x][coord.y] = WATER;
|
||||
}
|
||||
},
|
||||
WATER => {},
|
||||
FALLING_WATER => {},
|
||||
}
|
||||
WATER => {}
|
||||
FALLING_WATER => {}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
|
@ -146,21 +182,24 @@ fn pour_over(m: &mut Map, start_x: usize, start_y: usize) {
|
|||
} else {
|
||||
m[x][coord.y] = WATER;
|
||||
}
|
||||
},
|
||||
WATER => {},
|
||||
FALLING_WATER => {},
|
||||
}
|
||||
WATER => {}
|
||||
FALLING_WATER => {}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
// if no down, then push the square up
|
||||
if !found_down {
|
||||
let c = Coord { x: coord.x, y: coord.y - 1 };
|
||||
let c = Coord {
|
||||
x: coord.x,
|
||||
y: coord.y - 1,
|
||||
};
|
||||
if !frontier.contains(&c) {
|
||||
frontier.push_back(c);
|
||||
}
|
||||
}
|
||||
},
|
||||
WATER => {},
|
||||
}
|
||||
WATER => {}
|
||||
_ => panic!("invalid map square at {:#?}", coord),
|
||||
}
|
||||
}
|
||||
|
@ -179,7 +218,7 @@ fn lock_water(m: &mut Map, bounds: &MapBounds) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
for k in (x+1)..MAP_SIZE {
|
||||
for k in (x + 1)..MAP_SIZE {
|
||||
if m[k][y] == WALL_SPACE {
|
||||
break;
|
||||
} else if m[k][y] == EMPTY_SPACE {
|
||||
|
@ -196,7 +235,7 @@ fn lock_water(m: &mut Map, bounds: &MapBounds) {
|
|||
}
|
||||
m[k][y] = FALLING_WATER;
|
||||
}
|
||||
for k in (x+1)..MAP_SIZE {
|
||||
for k in (x + 1)..MAP_SIZE {
|
||||
if m[k][y] == EMPTY_SPACE || m[k][y] == WALL_SPACE {
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::collections::HashSet;
|
||||
|
||||
const OPEN: char = '.';
|
||||
const WOODED: char = '|';
|
||||
|
@ -136,7 +136,10 @@ fn part_two_score(map: &mut Map, width: usize, height: usize, target_ticks: u64)
|
|||
println!("first occurrence at {}", index);
|
||||
println!("remaining steps {}", remaining_steps);
|
||||
println!("cycle len {}", cycle_len);
|
||||
println!("remainder of {} with final tick at {}", remainder, final_tick);
|
||||
println!(
|
||||
"remainder of {} with final tick at {}",
|
||||
remainder, final_tick
|
||||
);
|
||||
return tick(&mut map.clone(), width, height, final_tick);
|
||||
}
|
||||
}
|
||||
|
@ -151,7 +154,7 @@ fn part_one_score(map: &Map, width: usize, height: usize) -> u64 {
|
|||
match map[x][y] {
|
||||
WOODED => woods += 1,
|
||||
LUMBERYARD => lumberyards += 1,
|
||||
_ => ()
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue