group wires into one tool option

This commit is contained in:
Crispy 2024-10-05 19:45:25 +02:00
parent ea02eff82b
commit 465b5c40d1
4 changed files with 89 additions and 78 deletions

View file

@ -35,6 +35,25 @@ pub struct Board {
}
impl Board {
pub fn parse(source: &str) -> Self {
let mut rows = Vec::new();
let mut width = 0;
for line in source.lines() {
width = width.max(line.len());
let mut tiles = Vec::new();
for char in line.chars() {
tiles.push(Tile::from_char(char));
}
rows.push(tiles);
}
for line in &mut rows {
line.resize(width, Tile::Blank);
}
Board::new(rows)
}
pub fn new_empty(width: usize, height: usize) -> Self {
let rows = vec![vec![Tile::Blank; width]; height];
Self {

View file

@ -68,6 +68,42 @@ pub enum Direction {
}
impl Tile {
pub const fn from_char(c: char) -> Tile {
match c {
'o' => Tile::Marble {
value: 0,
dir: Direction::Down,
},
'*' => Tile::Powerable(PTile::Trigger, false),
'-' => Tile::Powerable(PTile::Wire(WireType::Horizontal), false),
'|' => Tile::Powerable(PTile::Wire(WireType::Vertical), false),
'+' => Tile::Powerable(PTile::Wire(WireType::Cross), false),
'/' => Tile::Mirror(MirrorType::Forward),
'\\' => Tile::Mirror(MirrorType::Back),
'^' => Tile::Arrow(Direction::Up),
'v' => Tile::Arrow(Direction::Down),
'<' => Tile::Arrow(Direction::Left),
'>' => Tile::Arrow(Direction::Right),
'=' => Tile::Powerable(PTile::Gate(GateType::Equal), false),
'!' => Tile::Powerable(PTile::Gate(GateType::NotEqual), false),
'L' => Tile::Powerable(PTile::Gate(GateType::LessThan), false),
'G' => Tile::Powerable(PTile::Gate(GateType::GreaterThan), false),
'P' => Tile::Powerable(PTile::Output, false),
'I' => Tile::Powerable(PTile::Input, false),
'F' => Tile::Powerable(PTile::Flipper, false),
'A' => Tile::Powerable(PTile::Math(MathOp::Add), false),
'S' => Tile::Powerable(PTile::Math(MathOp::Sub), false),
'M' => Tile::Powerable(PTile::Math(MathOp::Mul), false),
'D' => Tile::Powerable(PTile::Math(MathOp::Div), false),
'R' => Tile::Powerable(PTile::Math(MathOp::Rem), false),
'B' => Tile::Powerable(PTile::Bag, false),
d @ '0'..='9' => Tile::Digit(d as u8 - b'0'),
'#' => Tile::Block,
' ' => Tile::Blank,
_ => Tile::Blank,
}
}
pub fn is_blank(&self) -> bool {
matches!(self, Tile::Blank)
}
@ -223,3 +259,4 @@ impl GateType {
}
}