Jirka Sejkora
3 years ago
4 changed files with 228 additions and 24 deletions
@ -0,0 +1,53 @@ |
|||||
|
use indicatif::{ProgressBar, ProgressStyle}; |
||||
|
use std::collections::HashSet; |
||||
|
use main::{get_neighbors, House, City}; |
||||
|
|
||||
|
mod main; |
||||
|
|
||||
|
fn main() { |
||||
|
let city = City::read_from_file("01.in"); |
||||
|
let bar = ProgressBar::new(city.get_house_count() as u64); |
||||
|
bar.set_style(ProgressStyle::default_bar() |
||||
|
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({msg}) ({eta})") |
||||
|
.progress_chars("#>-")); |
||||
|
|
||||
|
let mut useless_count = 0; |
||||
|
let mut checked_count = 0; |
||||
|
for y in 0..main::SIZE { |
||||
|
for x in 0..main::SIZE { |
||||
|
if city.is_house_xy(x, y) { |
||||
|
let house = House::new(x, y); |
||||
|
|
||||
|
let house_neighbors = get_neighbors(&city, &house); |
||||
|
let mut useless = true; |
||||
|
for neighbor in &house_neighbors { |
||||
|
if city.get_price(&house) < city.get_price(&neighbor) { |
||||
|
useless = false; |
||||
|
break; |
||||
|
} |
||||
|
let neighbor_neighbors: HashSet<_> = get_neighbors(&city, &neighbor).into_iter().collect(); |
||||
|
// Check if house_neighbors is a subset of neighbor_neighbors
|
||||
|
let all_in = &house_neighbors.iter().all(|item| neighbor_neighbors.contains(item)); |
||||
|
if !all_in { |
||||
|
useless = false; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if useless { |
||||
|
println!("{} {}", y, x); |
||||
|
useless_count += 1; |
||||
|
} else { |
||||
|
//println!("Y{} X{} may be sometimes worth buying", y, x);
|
||||
|
} |
||||
|
|
||||
|
checked_count += 1; |
||||
|
bar.set_message(&*format!("{}, {:.2}%", useless_count, 100.0 * useless_count as f64/checked_count as f64)); |
||||
|
bar.inc(1); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bar.finish(); |
||||
|
} |
||||
|
|
Loading…
Reference in new issue