2021-01-12 01:19:40 +01:00
|
|
|
use db::LayoutDB;
|
2021-01-13 02:00:32 +01:00
|
|
|
use city::{City, SIZE};
|
2021-01-12 01:19:40 +01:00
|
|
|
use itertools::Itertools;
|
2021-01-13 02:00:32 +01:00
|
|
|
use crate::combine::transpose_layout;
|
2021-01-12 01:19:40 +01:00
|
|
|
|
|
|
|
mod city;
|
|
|
|
mod db;
|
|
|
|
mod combine;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut db = LayoutDB::from_file("layouts.sqlite").expect("Failed to load the DB");
|
|
|
|
eprintln!("Loaded the DB, {} stored layouts", db.layouts().len());
|
|
|
|
|
|
|
|
let city = City::read_from_file("01.in");
|
|
|
|
eprintln!("Loaded the city file, {} houses", city.get_house_count());
|
|
|
|
|
2021-01-13 02:00:32 +01:00
|
|
|
eprintln!("Building a transposed city...");
|
|
|
|
let transposed_city = transpose_city(&city);
|
|
|
|
eprintln!("Finished building a transposed city");
|
2021-01-12 01:19:40 +01:00
|
|
|
|
2021-01-13 02:00:32 +01:00
|
|
|
loop {
|
|
|
|
let sorted: Vec<_> = db.layouts().iter()
|
|
|
|
.sorted_by(|x, y| city::get_price(&city, x.houses()).cmp(&city::get_price(&city, y.houses())))
|
|
|
|
.map(|layout| layout.clone())
|
|
|
|
.collect();
|
2021-01-13 00:03:09 +01:00
|
|
|
|
2021-01-13 02:00:32 +01:00
|
|
|
const TOP_LAYOUT_COUNT: usize = 1000;
|
2021-01-13 00:03:09 +01:00
|
|
|
|
2021-01-13 02:00:32 +01:00
|
|
|
let chosen_layouts: Vec<_> = sorted.iter().take(TOP_LAYOUT_COUNT).map(|l| l.houses().clone()).collect();
|
2021-01-13 01:43:23 +01:00
|
|
|
|
2021-01-13 02:00:32 +01:00
|
|
|
eprintln!("Starting to combine {} top houses DB; vertical cuts", TOP_LAYOUT_COUNT);
|
|
|
|
combine::try_combine(&city, &chosen_layouts, &chosen_layouts, &mut db, false);
|
|
|
|
|
|
|
|
let transposed_chosen_layouts: Vec<_> = chosen_layouts.iter().map(|x| transpose_layout(x)).collect();
|
|
|
|
eprintln!("Starting to combine {} top houses DB; horizontal cuts", TOP_LAYOUT_COUNT);
|
|
|
|
combine::try_combine(&transposed_city, &transposed_chosen_layouts, &transposed_chosen_layouts, &mut db, true);
|
|
|
|
}
|
2021-01-13 01:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn transpose_city(city: &City) -> City {
|
|
|
|
let mut transposed_prices = vec![0u16; SIZE * SIZE];
|
|
|
|
for y in 0..SIZE {
|
|
|
|
for x in 0..SIZE {
|
|
|
|
// Sorry, cache! Not worth optimizing with blocks,
|
|
|
|
// this is not going to be ran often.
|
|
|
|
transposed_prices[y * SIZE + x] = city.get_price_xy(y, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
City::new(transposed_prices)
|
|
|
|
}
|