Refactor common types.

This commit is contained in:
hal8174 2024-08-21 20:56:03 +02:00
parent f284b692cc
commit 48419b4674
14 changed files with 376 additions and 250 deletions

View file

@ -1,32 +1,36 @@
use crate::{
belt_finding::{
common::{print_map, Direction, Position},
COLORS,
},
misc::Map,
};
use crate::prelude::*;
use crate::{belt_finding::common::print_map, misc::Map};
use rand::Rng;
use serde::{Deserialize, Serialize};
use termcolor::ColorSpec;
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
struct Block {
size: Position,
input: Vec<Interface>,
output: Vec<Interface>,
}
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
struct Interface {
offset: Position,
// dir: Direction,
target: (usize, usize),
dir: Direction,
}
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
struct Connection {
startblock: usize,
startpoint: usize,
endblock: usize,
endpoint: usize,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Problem {
size: Position,
blocks: Vec<Block>,
connections: Vec<Connection>,
}
#[derive(Debug, Clone, Copy)]
@ -43,6 +47,7 @@ impl Problem {
Self {
size,
blocks: Vec::new(),
connections: Vec::new(),
}
}
@ -56,26 +61,26 @@ impl Problem {
BlockHandle(self.blocks.len() - 1)
}
pub fn add_connection(
&mut self,
starthandle: BlockHandle,
startoffset: Position,
endhandle: BlockHandle,
endoffset: Position,
) {
let startinterface = self.blocks[starthandle.0].output.len();
let endinterface = self.blocks[endhandle.0].input.len();
// pub fn add_connection(
// &mut self,
// starthandle: BlockHandle,
// startoffset: Position,
// endhandle: BlockHandle,
// endoffset: Position,
// ) {
// let startinterface = self.blocks[starthandle.0].output.len();
// let endinterface = self.blocks[endhandle.0].input.len();
self.blocks[starthandle.0].output.push(Interface {
offset: startoffset,
target: (endhandle.0, endinterface),
});
// self.blocks[starthandle.0].output.push(Interface {
// offset: startoffset,
// target: (endhandle.0, endinterface),
// });
self.blocks[endhandle.0].input.push(Interface {
offset: endoffset,
target: (starthandle.0, startinterface),
})
}
// self.blocks[endhandle.0].input.push(Interface {
// offset: endoffset,
// target: (starthandle.0, startinterface),
// })
// }
}
impl Layout<'_> {
@ -176,17 +181,20 @@ impl Layout<'_> {
pub fn score(&self) -> i32 {
let mut sum = 0;
for ((pos, dir), b) in self.blocks.iter().zip(self.problem.blocks.iter()) {
for i in &b.output {
let startpos = Self::transform(*pos, *dir, i.offset);
let endpos = Self::transform(
self.blocks[i.target.0].0,
self.blocks[i.target.0].1,
self.problem.blocks[i.target.0].input[i.target.1].offset,
);
sum += (startpos.x - endpos.x).abs() + (startpos.y - endpos.y).abs();
}
for c in &self.problem.connections {
let startpos = Self::transform(
self.blocks[c.startblock].0,
self.blocks[c.startblock].1,
self.problem.blocks[c.startblock].output[c.startpoint].offset,
);
let endpos = Self::transform(
self.blocks[c.endblock].0,
self.blocks[c.endblock].1,
self.problem.blocks[c.endblock].input[c.endpoint].offset,
);
sum += (startpos.x - endpos.x).abs() + (startpos.y - endpos.y).abs();
}
sum
@ -202,7 +210,7 @@ impl Layout<'_> {
}
pub fn print(&self) {
let mut m: Map<Option<usize>> =
let mut m: Map<Option<(usize, &str)>> =
Map::new(self.problem.size.x as usize, self.problem.size.y as usize);
for (i, ((p, d), b)) in self
@ -215,16 +223,32 @@ impl Layout<'_> {
for x in npos.x..(npos.x + nsize.x) {
for y in npos.y..(npos.y + nsize.y) {
m.set(x as usize, y as usize, Some(i));
m.set(x as usize, y as usize, Some((i, "#")));
}
}
let pos = Self::transform(*p, *d, Position::new(0, 0));
m.set(pos.x as usize, pos.y as usize, Some((i, "X")));
for input in &b.input {
let pos = Self::transform(*p, *d, input.offset);
m.set(pos.x as usize, pos.y as usize, Some((i, "i")));
}
for output in &b.output {
let pos = Self::transform(*p, *d, output.offset);
m.set(pos.x as usize, pos.y as usize, Some((i, "o")));
}
}
let _ = print_map(self.problem.size.x, self.problem.size.y, |x, y| {
if let Some(i) = m.get(x as usize, y as usize) {
let mut color = ColorSpec::new();
color.set_fg(Some(COLORS[*i]));
(color, "#")
color.set_fg(Some(crate::common::color::COLORS[i.0]));
(color, i.1)
} else {
(ColorSpec::new(), " ")
}