Skip to content

Instantly share code, notes, and snippets.

@metatoaster
Created December 18, 2022 12:37
Show Gist options
  • Save metatoaster/31ed6952c9951fbcec1205b7a818ba3b to your computer and use it in GitHub Desktop.
Save metatoaster/31ed6952c9951fbcec1205b7a818ba3b to your computer and use it in GitHub Desktop.
/*
[dependencies]
rand = "0.8"
*/
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::collections::HashMap;
fn main() {
let w: i16 = 8;
let h: i16 = 8;
let b: i16 = 10;
let a: i16 = (w * h).try_into().unwrap();
let s: i16 = a - b;
let mut line = [
vec![1i8; u16::try_from(b).unwrap().into()],
vec![0i8; u16::try_from(s).unwrap().into()]].concat();
line.shuffle(&mut thread_rng());
let checkidxs = (0i16..a)
.map(|idx| [
if (idx - 1 + w ) / w == (idx + w) / w { idx - h - 1 } else { -1 },
idx - h,
if (idx + 1) / w == idx / w { idx - h + 1 } else { -1 },
if (idx - 1 + w) / w == (idx + w) / w { idx - 1 } else { -1 },
if (idx + 1) / w == idx / w { idx + 1 } else { -1 },
if (idx - 1 + w) / w == (idx + w) / w { idx + h - 1 } else { -1 },
idx + h,
if (idx + 1) / w == idx / w { idx + h + 1 } else { -1 },
].into_iter()
.filter(|chk| -1i16 < *chk && *chk < a)
.map(|chk| u16::try_from(chk).unwrap())
.collect::<Vec<u16>>())
.collect::<Vec<Vec<u16>>>();
let scoreline = checkidxs.iter()
.enumerate()
.map(|(chk, checkidx)| {
if line[chk] == 0 {
checkidx.iter().map::<i8, _>(|idx| line[usize::from(*idx)]).sum()
} else {
-1
}
})
.collect::<Vec<i8>>();
// let scoregrid: Vec<&[i8]> = scoreline.chunks(h.try_into().unwrap()).collect();
let symbol = HashMap::from([
(0, ":zero:"),
(1, ":one:"),
(2, ":two:"),
(3, ":three:"),
(4, ":four:"),
(5, ":five:"),
(6, ":six:"),
(7, ":seven:"),
(8, ":eight:"),
(9, ":nine:"),
(-1, ":bomb:"),
]);
let discoline: Vec<String> = scoreline.iter()
.map(|score| "||".to_owned() + symbol.get(score).unwrap() + "||")
.collect();
let discogrid: Vec<&[String]> = discoline.chunks(h.try_into().unwrap()).collect();
println!("{} :bomb:", format!("{}", b)
.chars()
.map(|c| symbol
.get(&(i8::try_from(c.to_digit(10).unwrap())).unwrap())
.unwrap()
.to_string()
)
.collect::<Vec<String>>().join(""));
println!("{}", discogrid.iter().map(|d| d.join("")).collect::<Vec<String>>().join("\n"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment