Basic painting optimizer
All checks were successful
Rust / build_and_test (push) Successful in 24s

This commit is contained in:
Lukas Wölfer
2025-07-27 20:34:40 +02:00
parent 56702053cf
commit e5b5e31f46
5 changed files with 270 additions and 65 deletions

View File

@@ -1,19 +1,56 @@
use crate::{CardField, Icon}; use crate::{CardField, Icon};
#[derive(Debug, Default)] #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
// There are 60 cards in the base set // There are 60 cards in the base set
pub struct Card { pub struct Card {
pub field: [CardField; 5], field: u32,
}
pub struct CardFieldIter {
index: u8,
field: u32,
}
impl Iterator for CardFieldIter {
type Item = CardField;
fn next(&mut self) -> Option<Self::Item> {
if self.index < 5 {
let old_index = self.index;
self.index += 1;
let bitmask = (1 << (CardField::BITPACKED_SIZE as u8)) - 1;
let result = (self.field >> (CardField::BITPACKED_SIZE as u8 * old_index)) & bitmask;
Some(CardField::from_bits(result as u8))
} else {
None
}
}
}
impl From<[CardField; 5]> for Card {
fn from(value: [CardField; 5]) -> Self {
let field = value
.iter()
.map(|x| x.to_bits())
.zip((0u8..).step_by(CardField::BITPACKED_SIZE))
.map(|(a, shift_count)| (a as u32) << (shift_count))
.fold(0u32, |a, b| a | b);
Self { field }
}
} }
impl Card { impl Card {
pub const fn empty() -> Self { pub fn field_iter(&self) -> CardFieldIter {
Self { CardFieldIter {
field: [CardField::Empty; 5], index: 0,
field: self.field,
} }
} }
pub const fn empty() -> Self {
Self { field: u32::MAX }
}
pub fn from_dbg_string(text: &str) -> Self { pub fn from_dbg_string(text: &str) -> Self {
let mut result = Self::empty(); let mut result = [CardField::Empty; 5];
let mut current_field = 0; let mut current_field = 0;
let mut char_iter = text.chars(); let mut char_iter = text.chars();
let match_icon = |x: char| { let match_icon = |x: char| {
@@ -28,7 +65,7 @@ impl Card {
}; };
while let Some(c) = char_iter.next() { while let Some(c) = char_iter.next() {
if let Some(f) = match_icon(c) { if let Some(f) = match_icon(c) {
result.field[current_field] = CardField::Icon(f); result[current_field] = CardField::Icon(f);
current_field += 1; current_field += 1;
continue; continue;
} }
@@ -48,16 +85,37 @@ impl Card {
.expect("No second icon after double symbol"), .expect("No second icon after double symbol"),
) )
.expect("Incorrect second icon after double symbol"); .expect("Incorrect second icon after double symbol");
CardField::DoubleIcon((f1, f2)) CardField::DoubleIcon(f1, f2)
} }
'.' => CardField::Empty, '.' => CardField::Empty,
'-' => continue, '-' => continue,
_ => panic!("Unknown character {c}"), _ => panic!("Unknown character {c}"),
}; };
result.field[current_field] = field; result[current_field] = field;
current_field += 1; current_field += 1;
} }
result result.into()
}
}
#[cfg(test)]
mod tests {
use crate::{CardField, Icon, card::Card};
#[test]
fn test_correctness() {
let card = Card::from_dbg_string("l..t.");
println!("{:0x}", card.field);
assert_eq!(
card.field_iter().collect::<Vec<_>>(),
vec![
CardField::Icon(Icon::Color),
CardField::Empty,
CardField::Empty,
CardField::Icon(Icon::Triangle),
CardField::Empty,
]
)
} }
} }

View File

@@ -38,7 +38,7 @@ impl IconBitfield {
debug_assert!(index < 5); debug_assert!(index < 5);
match field { match field {
CardField::Icon(icon) => self.add_icon(icon, index), CardField::Icon(icon) => self.add_icon(icon, index),
CardField::DoubleIcon((icon1, icon2)) => { CardField::DoubleIcon(icon1, icon2) => {
self.add_icon(icon1, index); self.add_icon(icon1, index);
self.add_icon(icon2, index); self.add_icon(icon2, index);
} }
@@ -50,8 +50,8 @@ impl IconBitfield {
pub fn new(card: &Card) -> Self { pub fn new(card: &Card) -> Self {
let mut result = Self::default(); let mut result = Self::default();
(0u8..) (0u8..)
.zip(card.field.iter()) .zip(card.field_iter())
.for_each(|(index, field)| result.add_field(*field, index)); .for_each(|(index, field)| result.add_field(field, index));
result result
} }
@@ -84,35 +84,40 @@ impl IconBitfield {
} }
} }
#[test] #[cfg(test)]
fn test_add_field() { mod tests {
let mut x: IconBitfield = Default::default(); use crate::{Icon, icon_bitfield::IconBitfield};
x.add_icon(Icon::Circle, 2);
let expected = IconBitfield {
circle: 0b00100,
..Default::default()
};
assert_eq!(x, expected);
}
#[test] #[test]
fn test_layer_below() { fn test_add_field() {
let mut a: IconBitfield = IconBitfield { let mut x: IconBitfield = Default::default();
color: 0b00010, x.add_icon(Icon::Circle, 2);
points: 0b01000, let expected = IconBitfield {
..Default::default() circle: 0b00100,
}; ..Default::default()
let b = IconBitfield { };
triangle: a.get_point_bits(), assert_eq!(x, expected);
square: 0b10000, }
..Default::default()
}; #[test]
let expected = IconBitfield { fn test_layer_below() {
color: a.get_icon_bits(Icon::Color), let mut a: IconBitfield = IconBitfield {
points: a.get_point_bits(), color: 0b00010,
square: b.get_icon_bits(Icon::Square), points: 0b01000,
..Default::default() ..Default::default()
}; };
a.layer_below(&b); let b = IconBitfield {
assert_eq!(a, expected); triangle: a.get_point_bits(),
square: 0b10000,
..Default::default()
};
let expected = IconBitfield {
color: a.get_icon_bits(Icon::Color),
points: a.get_point_bits(),
square: b.get_icon_bits(Icon::Square),
..Default::default()
};
a.layer_below(&b);
assert_eq!(a, expected);
}
} }

View File

@@ -1,4 +1,5 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::objective::Objective; use crate::objective::Objective;
mod card; mod card;
@@ -14,23 +15,88 @@ struct Shop {
coins: [u8; 5], coins: [u8; 5],
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Icon { pub enum Icon {
Circle, Circle = 0b00,
Square, Square = 0b01,
Triangle, Triangle = 0b10,
Color, Color = 0b11,
} }
#[derive(Debug, Clone, Copy, Default)] impl TryFrom<u8> for Icon {
enum CardField { type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
ICON_ORDER
.get(value as usize)
.map(|x| Ok(*x))
.unwrap_or(Err(()))
}
}
const ICON_ORDER: [Icon; 4] = [Icon::Circle, Icon::Square, Icon::Triangle, Icon::Color];
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum CardField {
Icon(Icon), Icon(Icon),
DoubleIcon((Icon, Icon)),
PointsPer(Icon), PointsPer(Icon),
DoubleIcon(Icon, Icon),
#[default] #[default]
Empty, Empty,
} }
impl CardField {
const BITPACKED_SIZE: usize = 4;
fn from_bits(bits: u8) -> Self {
{
if bits == 0b1111 {
Self::Empty
} else if (bits & (1 << 3)) != 0 {
let bits = bits & ((1 << 3) - 1);
if bits == 5 {
Self::DoubleIcon(Icon::Triangle, Icon::Color)
} else if bits >= 3 {
Self::DoubleIcon(Icon::Square, Icon::try_from(bits + 2 - 3).unwrap())
} else {
Self::DoubleIcon(Icon::Circle, Icon::try_from(bits + 1).unwrap())
}
} else if bits & (1 << 2) != 0 {
let bits = bits & ((1 << 2) - 1);
Self::PointsPer(Icon::try_from(bits).unwrap())
} else {
Self::Icon(Icon::try_from(bits).unwrap())
}
}
}
/// There are only 4 + 4 + 6 + 1 possibilities when double icons are sorted
fn to_bits(self) -> u8 {
match self {
CardField::Icon(icon) => icon as u8,
CardField::PointsPer(icon) => icon as u8 | (1 << 2),
CardField::DoubleIcon(mut a, mut b) => {
debug_assert_ne!(a, b);
if a > b {
(a, b) = (b, a);
}
let tag = match a {
// 0..=2
Icon::Circle => ICON_ORDER.iter().position(|x| b == *x).unwrap() as u8 - 1,
// 3..=4
Icon::Square => ICON_ORDER.iter().position(|x| b == *x).unwrap() as u8 - 2 + 3,
// 5
Icon::Triangle => {
debug_assert_eq!(b, Icon::Color);
5
}
Icon::Color => panic!("Color as first double is not possible"),
};
tag | (1 << 3)
}
CardField::Empty => (1 << 4) - 1,
}
}
}
#[derive(Debug)] #[derive(Debug)]
struct Player { struct Player {
cards: Vec<Card>, cards: Vec<Card>,

View File

@@ -1,4 +1,4 @@
use crate::{Icon, Painting, icon_bitfield::IconBitfield}; use crate::{icon_bitfield::IconBitfield, Icon, Painting, ICON_ORDER};
// There are 12 objectives/criteria in the base set // There are 12 objectives/criteria in the base set
#[derive(Debug)] #[derive(Debug)]
@@ -17,7 +17,6 @@ pub enum Objective {
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IconSet(u8); pub struct IconSet(u8);
const ICON_SET_ORDER: [Icon; 4] = [Icon::Circle, Icon::Triangle, Icon::Square, Icon::Color];
pub struct IconSetIterator { pub struct IconSetIterator {
icon_set: IconSet, icon_set: IconSet,
@@ -28,7 +27,7 @@ impl From<&[Icon]> for IconSet {
fn from(value: &[Icon]) -> Self { fn from(value: &[Icon]) -> Self {
let v = value let v = value
.iter() .iter()
.map(|x| 1 << ICON_SET_ORDER.iter().position(|v| x == v).unwrap()) .map(|x| 1 << ICON_ORDER.iter().position(|v| x == v).unwrap())
.fold(0, |a, b| a | b); .fold(0, |a, b| a | b);
Self(v) Self(v)
} }
@@ -49,7 +48,7 @@ impl Iterator for IconSetIterator {
self.index += 1; self.index += 1;
if self.icon_set.0 & (1 << old_index) != 0 { if self.icon_set.0 & (1 << old_index) != 0 {
return Some(*ICON_SET_ORDER.get(old_index as usize).unwrap()); return Some(*ICON_ORDER.get(old_index as usize).unwrap());
} }
} }
None None

View File

@@ -1,14 +1,91 @@
use itertools::Itertools; use itertools::Itertools;
use crate::{CriteriaCard, icon_bitfield::IconBitfield}; use crate::{CardField, CriteriaCard, card::Card, icon_bitfield::IconBitfield};
fn draw_painting(cards: &[IconBitfield], _obj: Vec<(&CriteriaCard, u8)>) -> [usize; 3] { struct OneIndexIterator {
for stack in cards.iter().permutations(3) { n: u64,
let stack: [&IconBitfield; 3] = stack.try_into().unwrap(); offset: u8,
let mut painting = stack[0].clone(); }
painting.layer_below(stack[1]).layer_below(stack[2]);
// obj.iter() impl From<u64> for OneIndexIterator {
// .map(|x| x.0.objective.count_matches_bitfield(&painting)); fn from(value: u64) -> Self {
} Self {
todo!() n: value,
offset: 0,
}
}
}
impl Iterator for OneIndexIterator {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if self.n == 0 {
return None;
}
let new_offset = self.n.trailing_zeros() as u8;
self.n >>= new_offset + 1;
let result = self.offset + new_offset;
self.offset += new_offset + 1;
Some(result)
}
}
fn draw_painting(cards: &[Card], obj: &[(&CriteriaCard, u8)]) -> Option<[usize; 3]> {
let bitfields = cards.iter().map(IconBitfield::new).collect::<Vec<_>>();
let mut best: Option<([usize; 3], u8)> = None;
for stack in (0..cards.len()).permutations(3) {
let stack_indices: [_; 3] = stack.try_into().unwrap();
let stack = stack_indices.map(|x| cards[x]);
let maps = stack_indices.map(|x| &bitfields[x]);
let mut painting = maps[0].clone();
painting.layer_below(maps[1]).layer_below(maps[2]);
let criteria_points = obj
.iter()
.map(|(criteria, reached)| {
criteria.points.get(usize::from(
criteria.objective.count_matches_bitfield(&painting) + *reached,
))
})
.sum::<u8>();
let bonus_points = OneIndexIterator::from(u64::from(painting.get_point_bits()))
.map(|bit_index| {
let matched_icon = stack
.iter()
.map(|x| x.field_iter().nth(usize::from(bit_index)).unwrap())
.find_map(|x| match x {
CardField::PointsPer(icon) => Some(icon),
_ => None,
})
.expect("Point bits lied");
painting.get_icon_bits(matched_icon).count_ones() as u8 * 2
})
.sum::<u8>();
let total_points = bonus_points + criteria_points;
let stack_result = (stack_indices, total_points);
dbg!(stack_result);
if let Some(bp) = &mut best {
if bp.1 < total_points {
*bp = stack_result;
}
} else {
best = Some(stack_result)
}
}
best.map(|x| x.0)
}
#[cfg(test)]
mod tests {
use crate::{card::Card, optimizer::draw_painting};
#[test]
fn test_basic() {
let cards = vec![
Card::from_dbg_string(".ttt."),
Card::from_dbg_string("t.l.."),
Card::from_dbg_string("ll..pt"),
];
assert_eq!(draw_painting(&cards, &[]), Some([0, 1, 2]));
}
} }