1
0
Fork 0

cargo fmt

main
Andrew Coleman 2019-01-20 13:17:22 -05:00
parent d3690d856e
commit c82b633b65
2 changed files with 73 additions and 31 deletions

View File

@ -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 mut right_numbers = right_parts.nth(1).unwrap().split("..");
let right_number_1: usize = right_numbers.nth(0).unwrap().parse().unwrap(); 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 right_number_2: usize = right_numbers.nth(0).unwrap().parse().unwrap();
let left_range = Range { start: left_number, end: left_number + 1 }; let left_range = Range {
let right_range = Range { start: right_number_1, end: right_number_2 + 1 }; 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" { if left_coord_type == "x" {
(left_range, right_range) (left_range, right_range)
} else { } 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) { fn pour_over(m: &mut Map, start_x: usize, start_y: usize) {
let mut frontier: VecDeque<Coord> = VecDeque::new(); 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() { while let Some(coord) = frontier.pop_front() {
match m[coord.x][coord.y] { 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 => { EMPTY_SPACE => {
m[coord.x][coord.y] = FALLING_WATER; m[coord.x][coord.y] = FALLING_WATER;
if coord.y + 1 < MAP_SIZE { if coord.y + 1 < MAP_SIZE {
match m[coord.x][coord.y + 1] { match m[coord.x][coord.y + 1] {
WALL_SPACE => frontier.push_back(Coord { x: coord.x, y: coord.y }), WALL_SPACE => frontier.push_back(Coord {
EMPTY_SPACE => frontier.push_back(Coord { x: coord.x, y: coord.y + 1 }), x: coord.x,
FALLING_WATER => frontier.push_back(Coord { x: coord.x, y: coord.y }), 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 => { WATER => {
if m[coord.x + 1][coord.y] == WALL_SPACE || m[coord.x - 1][coord.y] == WALL_SPACE { if m[coord.x + 1][coord.y] == WALL_SPACE
frontier.push_back(Coord { x: coord.x, y: coord.y }); || m[coord.x - 1][coord.y] == WALL_SPACE
} 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 }); frontier.push_back(Coord {
} else if m[coord.x + 1][coord.y] == FALLING_WATER || m[coord.x - 1][coord.y] == FALLING_WATER { x: coord.x,
frontier.push_back(Coord { x: coord.x, y: coord.y }); 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 => { FALLING_WATER => {
let mut found_down = false; let mut found_down = false;
// go left as far as possible // go left as far as possible
@ -126,9 +162,9 @@ fn pour_over(m: &mut Map, start_x: usize, start_y: usize) {
} else { } else {
m[x][coord.y] = WATER; m[x][coord.y] = WATER;
} }
}, }
WATER => {}, WATER => {}
FALLING_WATER => {}, FALLING_WATER => {}
_ => break, _ => break,
} }
} }
@ -146,21 +182,24 @@ fn pour_over(m: &mut Map, start_x: usize, start_y: usize) {
} else { } else {
m[x][coord.y] = WATER; m[x][coord.y] = WATER;
} }
}, }
WATER => {}, WATER => {}
FALLING_WATER => {}, FALLING_WATER => {}
_ => break, _ => break,
} }
} }
// if no down, then push the square up // if no down, then push the square up
if !found_down { 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) { if !frontier.contains(&c) {
frontier.push_back(c); frontier.push_back(c);
} }
} }
}, }
WATER => {}, WATER => {}
_ => panic!("invalid map square at {:#?}", coord), _ => panic!("invalid map square at {:#?}", coord),
} }
} }
@ -179,7 +218,7 @@ fn lock_water(m: &mut Map, bounds: &MapBounds) {
break; break;
} }
} }
for k in (x+1)..MAP_SIZE { for k in (x + 1)..MAP_SIZE {
if m[k][y] == WALL_SPACE { if m[k][y] == WALL_SPACE {
break; break;
} else if m[k][y] == EMPTY_SPACE { } else if m[k][y] == EMPTY_SPACE {
@ -196,7 +235,7 @@ fn lock_water(m: &mut Map, bounds: &MapBounds) {
} }
m[k][y] = FALLING_WATER; 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 { if m[k][y] == EMPTY_SPACE || m[k][y] == WALL_SPACE {
break; break;
} }

View File

@ -1,6 +1,6 @@
use std::collections::HashSet;
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::collections::HashSet;
const OPEN: char = '.'; const OPEN: char = '.';
const WOODED: 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!("first occurrence at {}", index);
println!("remaining steps {}", remaining_steps); println!("remaining steps {}", remaining_steps);
println!("cycle len {}", cycle_len); 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); 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] { match map[x][y] {
WOODED => woods += 1, WOODED => woods += 1,
LUMBERYARD => lumberyards += 1, LUMBERYARD => lumberyards += 1,
_ => () _ => (),
} }
} }
} }