#![cfg_attr(cardiac, no_std)]
#[cfg(cardiac)]
#[inline(never)]
pub fn inp() -> i16 {
0
}
#[cfg(cardiac)]
#[inline(never)]
pub fn out(_v: i16) {}
#[cfg(cardiac)]
#[inline(never)]
pub fn overlay() {}
#[cfg(cardiac)]
#[inline(never)]
pub fn load_resource<const N: usize>(_name: &str) -> [i16; N] {
[0; N]
}
#[inline(never)]
pub fn digit(v: i16, k: i16) -> i16 {
if k == 0 {
v / 100
} else if k == 1 {
v / 10 % 10
} else {
v % 10
}
}
#[cfg(cardiac)]
#[inline(never)]
pub fn skip_cards(_n: i16) {}
#[cfg(cardiac)]
#[inline(never)]
pub fn add_times(_c: i16, _n: i16) -> i16 {
0
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Flag(i16);
impl Flag {
pub const FALSE: Flag = Flag(-1);
pub const TRUE: Flag = Flag(0);
#[inline(always)]
pub fn get(self) -> bool {
self.0 >= 0
}
}
impl From<bool> for Flag {
#[inline(always)]
fn from(b: bool) -> Flag {
if b {
Flag::TRUE
} else {
Flag::FALSE
}
}
}
impl From<Flag> for bool {
#[inline(always)]
fn from(f: Flag) -> bool {
f.get()
}
}
impl Default for Flag {
#[inline(always)]
fn default() -> Flag {
Flag::FALSE
}
}
impl core::ops::Not for Flag {
type Output = Flag;
#[inline(always)]
fn not(self) -> Flag {
Flag(-1 - self.0)
}
}
#[cfg(cardiac)]
#[inline(always)]
pub unsafe fn assume(condition: bool) {
if cfg!(debug_assertions) {
assert!(condition);
} else {
unsafe { core::hint::assert_unchecked(condition) }
}
}
#[cfg(not(cardiac))]
mod native {
use std::collections::VecDeque;
use std::io::{BufRead, Write};
use std::path::PathBuf;
use std::sync::{Mutex, Once};
static INPUT: Mutex<VecDeque<String>> = Mutex::new(VecDeque::new());
static PANIC_CARD: Once = Once::new();
#[used]
#[cfg_attr(target_vendor = "apple", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(all(unix, not(target_vendor = "apple")), link_section = ".init_array")]
#[cfg_attr(windows, link_section = ".CRT$XCU")]
static BEFORE_MAIN: extern "C" fn() = before_main;
extern "C" fn before_main() {
setup();
}
pub fn setup() {
PANIC_CARD.call_once(|| {
let rust = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
println!("-999");
let _ = std::io::stdout().flush();
rust(info)
}));
});
}
fn stop(message: Option<String>) -> ! {
let _ = std::io::stdout().flush();
match message {
None => std::process::exit(0),
Some(m) => {
eprintln!("{m}");
std::process::exit(2)
}
}
}
fn card(word: &str, from: &str) -> i16 {
match word.parse::<i16>() {
Ok(v) if (-999..=999).contains(&v) => v,
_ => stop(Some(format!(
"{from}not a card: {word} (a card holds a number from -999 to 999)"
))),
}
}
pub fn next() -> i16 {
setup();
let mut input = INPUT.lock().unwrap();
while input.is_empty() {
let mut line = String::new();
match std::io::stdin().lock().read_line(&mut line) {
Ok(0) => stop(None),
Ok(_) => input.extend(line.split_whitespace().map(String::from)),
Err(e) => stop(Some(format!("reading standard input: {e}"))),
}
}
let word = input.pop_front().unwrap();
card(&word, "")
}
pub fn resource<const N: usize>(name: &str) -> [i16; N] {
setup();
let dir = std::env::var_os("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap_or_default();
let path = dir.join("resources").join(name);
let text = std::fs::read_to_string(&path)
.unwrap_or_else(|e| stop(Some(format!("resource {name}: {}: {e}", path.display()))));
let from = format!("resource {name}: {}: ", path.display());
let values: Vec<i16> = text.split_whitespace().map(|w| card(w, &from)).collect();
let n = values.len();
values
.try_into()
.unwrap_or_else(|_| stop(Some(format!("{from}{n} values, and the program loads {N}"))))
}
}
#[cfg(not(cardiac))]
pub fn inp() -> i16 {
native::next()
}
#[cfg(not(cardiac))]
pub fn out(v: i16) {
native::setup();
assert!(
(-999..=999).contains(&v),
"out({v}): a card holds a number from -999 to 999"
);
println!("{v}");
}
#[cfg(not(cardiac))]
pub fn overlay() {}
#[cfg(not(cardiac))]
pub fn load_resource<const N: usize>(name: &str) -> [i16; N] {
native::resource(name)
}
#[cfg(not(cardiac))]
#[track_caller]
pub fn skip_cards(n: i16) {
assert!((0..=999).contains(&n), "skip_cards({n}): n is 0 to 999");
for _ in 0..n {
inp();
}
}
#[cfg(not(cardiac))]
#[track_caller]
pub fn add_times(c: i16, n: i16) -> i16 {
native::setup();
assert!((0..=99).contains(&n), "add_times({c}, {n}): n is 0 to 99");
let product = c as i32 * n as i32;
assert!(
(-999..=999).contains(&product),
"add_times({c}, {n}): {product} doesn't fit on a card"
);
product as i16
}
#[cfg(not(cardiac))]
#[track_caller]
pub unsafe fn assume(condition: bool) {
assert!(condition, "a broken assumption");
}