Skip to content

Commit c1f79f0

Browse files
authored
New outside numbers serializer (#207)
* Added new serializers * Fixed serializer and started migrating * Migrated tents, battleship and aquarium to new serializer * Migrated doppelblock
1 parent 3a43cc0 commit c1f79f0

8 files changed

Lines changed: 211 additions & 335 deletions

File tree

cspuz_rs/src/serializer.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,124 @@ where
11901190
Some((n_read, vec![(clue_vertical, clue_horizontal)]))
11911191
}
11921192
}
1193+
1194+
pub struct OutsideCells2<C> {
1195+
base_serializer: C,
1196+
}
1197+
1198+
impl<C> OutsideCells2<C> {
1199+
pub fn new(base_serializer: C) -> OutsideCells2<C> {
1200+
OutsideCells2 { base_serializer }
1201+
}
1202+
}
1203+
1204+
impl<T, C> Combinator<(Vec<T>, Vec<T>)> for OutsideCells2<C>
1205+
where
1206+
T: Clone + PartialEq,
1207+
C: Combinator<T>,
1208+
{
1209+
fn serialize(&self, ctx: &Context, input: &[(Vec<T>, Vec<T>)]) -> Option<(usize, Vec<u8>)> {
1210+
if input.len() == 0 {
1211+
return None;
1212+
}
1213+
let height = ctx.height?;
1214+
let width = ctx.width?;
1215+
1216+
let input = &input[0];
1217+
1218+
let surrounding = [&input.0[..], &input.1[..]].concat();
1219+
let ret = Seq::new(&self.base_serializer, width + height)
1220+
.serialize(ctx, &[surrounding])?
1221+
.1;
1222+
1223+
Some((1, ret))
1224+
}
1225+
1226+
fn deserialize(&self, ctx: &Context, input: &[u8]) -> Option<(usize, Vec<(Vec<T>, Vec<T>)>)> {
1227+
let mut sequencer = Sequencer::new(input);
1228+
1229+
let height = ctx.height?;
1230+
let width = ctx.width?;
1231+
1232+
let surrounding =
1233+
sequencer.deserialize(ctx, Seq::new(&self.base_serializer, width + height))?;
1234+
if surrounding.len() != 1 {
1235+
return None;
1236+
}
1237+
let surrounding = surrounding.into_iter().next().unwrap();
1238+
1239+
let clues_up = surrounding[..width].to_vec();
1240+
let clues_left = surrounding[width..].to_vec();
1241+
1242+
Some((sequencer.n_read(), vec![(clues_up, clues_left)]))
1243+
}
1244+
}
1245+
1246+
pub struct OutsideCells4<C> {
1247+
base_serializer: C,
1248+
}
1249+
1250+
impl<C> OutsideCells4<C> {
1251+
pub fn new(base_serializer: C) -> OutsideCells4<C> {
1252+
OutsideCells4 { base_serializer }
1253+
}
1254+
}
1255+
1256+
impl<T, C> Combinator<(Vec<T>, Vec<T>, Vec<T>, Vec<T>)> for OutsideCells4<C>
1257+
where
1258+
T: Clone + PartialEq,
1259+
C: Combinator<T>,
1260+
{
1261+
fn serialize(
1262+
&self,
1263+
ctx: &Context,
1264+
input: &[(Vec<T>, Vec<T>, Vec<T>, Vec<T>)],
1265+
) -> Option<(usize, Vec<u8>)> {
1266+
if input.len() == 0 {
1267+
return None;
1268+
}
1269+
let height = ctx.height?;
1270+
let width = ctx.width?;
1271+
1272+
let input = &input[0];
1273+
1274+
let surrounding = [&input.0[..], &input.1[..], &input.2[..], &input.3[..]].concat();
1275+
let ret = Seq::new(&self.base_serializer, width + height)
1276+
.serialize(ctx, &[surrounding])?
1277+
.1;
1278+
1279+
Some((1, ret))
1280+
}
1281+
1282+
fn deserialize(
1283+
&self,
1284+
ctx: &Context,
1285+
input: &[u8],
1286+
) -> Option<(usize, Vec<(Vec<T>, Vec<T>, Vec<T>, Vec<T>)>)> {
1287+
let mut sequencer = Sequencer::new(input);
1288+
1289+
let height = ctx.height?;
1290+
let width = ctx.width?;
1291+
1292+
let surrounding =
1293+
sequencer.deserialize(ctx, Seq::new(&self.base_serializer, 2 * (width + height)))?;
1294+
if surrounding.len() != 1 {
1295+
return None;
1296+
}
1297+
let surrounding = surrounding.into_iter().next().unwrap();
1298+
1299+
let clues_up = surrounding[..width].to_vec();
1300+
let clues_down = surrounding[width..(2 * width)].to_vec();
1301+
let clues_left = surrounding[(2 * width)..(2 * width + height)].to_vec();
1302+
let clues_right = surrounding[(2 * width + height)..].to_vec();
1303+
1304+
Some((
1305+
sequencer.n_read(),
1306+
vec![(clues_up, clues_down, clues_left, clues_right)],
1307+
))
1308+
}
1309+
}
1310+
11931311
pub struct Rooms;
11941312

11951313
impl Combinator<InnerGridEdges<Vec<Vec<bool>>>> for Rooms {

cspuz_rs_puzzles/src/puzzles/aquarium.rs

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use cspuz_rs::graph;
22
use cspuz_rs::serializer::{
33
problem_to_url_with_context, url_to_problem, Choice, Combinator, Context, Dict, HexInt,
4-
Optionalize, PrefixAndSuffix, Rooms, Seq, Sequencer, Size, Spaces, Tuple2,
4+
Optionalize, OutsideCells2, PrefixAndSuffix, Rooms, Size, Spaces, Tuple2,
55
};
66
use cspuz_rs::solver::Solver;
77

@@ -89,62 +89,6 @@ type Problem = (
8989
),
9090
);
9191

92-
fn internal_combinator() -> impl Combinator<Option<i32>> {
93-
Choice::new(vec![
94-
Box::new(Optionalize::new(HexInt)),
95-
Box::new(Spaces::new(None, 'g')),
96-
])
97-
}
98-
99-
pub struct AquariumCombinator;
100-
101-
impl Combinator<(Vec<Option<i32>>, Vec<Option<i32>>)> for AquariumCombinator {
102-
fn serialize(
103-
&self,
104-
ctx: &cspuz_rs::serializer::Context,
105-
input: &[(Vec<Option<i32>>, Vec<Option<i32>>)],
106-
) -> Option<(usize, Vec<u8>)> {
107-
if input.is_empty() {
108-
return None;
109-
}
110-
111-
let height = ctx.height?;
112-
let width = ctx.width?;
113-
114-
let problem = &input[0];
115-
116-
let surrounding = [&problem.0[..], &problem.1[..]].concat();
117-
let ret = Seq::new(internal_combinator(), width + height)
118-
.serialize(ctx, &[surrounding])?
119-
.1;
120-
121-
Some((1, ret))
122-
}
123-
124-
fn deserialize(
125-
&self,
126-
ctx: &cspuz_rs::serializer::Context,
127-
input: &[u8],
128-
) -> Option<(usize, Vec<(Vec<Option<i32>>, Vec<Option<i32>>)>)> {
129-
let mut sequencer = Sequencer::new(input);
130-
131-
let height = ctx.height?;
132-
let width = ctx.width?;
133-
134-
let surrounding =
135-
sequencer.deserialize(ctx, Seq::new(internal_combinator(), width + height))?;
136-
if surrounding.len() != 1 {
137-
return None;
138-
}
139-
let surrounding = surrounding.into_iter().next().unwrap();
140-
141-
let clues_up = surrounding[..width].to_vec();
142-
let clues_left = surrounding[width..].to_vec();
143-
144-
Some((sequencer.n_read(), vec![(clues_up, clues_left)]))
145-
}
146-
}
147-
14892
fn combinator() -> impl Combinator<Problem> {
14993
Tuple2::new(
15094
Choice::new(vec![
@@ -153,7 +97,14 @@ fn combinator() -> impl Combinator<Problem> {
15397
]),
15498
Size::new(Tuple2::new(
15599
Rooms,
156-
PrefixAndSuffix::new("/", AquariumCombinator, ""),
100+
PrefixAndSuffix::new(
101+
"/",
102+
OutsideCells2::new(Choice::new(vec![
103+
Box::new(Optionalize::new(HexInt)),
104+
Box::new(Spaces::new(None, 'g')),
105+
])),
106+
"",
107+
),
157108
)),
158109
)
159110
}

0 commit comments

Comments
 (0)