Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

110
Task/UPC/Pluto/upc.pluto Normal file
View file

@ -0,0 +1,110 @@
local fmt = require "fmt"
local digitL <const> = {
[" ## #"] = 0,
[" ## #"] = 1,
[" # ##"] = 2,
[" #### #"] = 3,
[" # ##"] = 4,
[" ## #"] = 5,
[" # ####"] = 6,
[" ### ##"] = 7,
[" ## ###"] = 8,
[" # ##"] = 9
}
local digitR <const> = {
["### # "] = 0,
["## ## "] = 1,
["## ## "] = 2,
["# # "] = 3,
["# ### "] = 4,
["# ### "] = 5,
["# # "] = 6,
["# # "] = 7,
["# # "] = 8,
["### # "] = 9
}
local end_sentinel <const> = "# #" -- also at start
local mid_sentinel <const> = " # # "
local function decode_upc(s)
local function decode_upc_impl(input)
local upc = input:strip()
if #upc != 95 then return false end
local pos = 1
local digits = {}
local sum = 0
local one_three = {1, 3}
-- end sentinel
if upc:sub(pos, pos + 2) == end_sentinel then
pos += 3
else
return false
end
-- 6 left hand digits
for _ = 1, 6 do
local digit = digitL[upc:sub(pos, pos + 6)]
if !digit then return false end
digits:insert(digit)
sum += digit * one_three[#digits % 2 + 1]
pos += 7
end
-- mid sentinel
if upc:sub(pos, pos + 4) == mid_sentinel then
pos += 5
else
return false
end
-- 6 right hand digits
for _ = 1, 6 do
local digit = digitR[upc:sub(pos, pos + 6)]
if !digit then return false end
digits:insert(digit)
sum += digit * one_three[#digits % 2 + 1]
pos += 7
end
-- end sentinel
if upc:sub(pos, pos + 2) != end_sentinel then return false end
if sum % 10 == 0 then
fmt.write("%s ", fmt.swrite(digits))
return true
end
io.write("Failed Checksum ")
return false
end
if decode_upc_impl(s) then
print("Rightside Up")
elseif decode_upc_impl(s:reverse()) then
print("Upside Down")
else
print("Invalid digit(s)")
end
end
local barcodes <const> = {
" # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ",
" # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ",
" # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ",
" # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ",
" # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ",
" # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ",
" # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ",
" # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ",
" # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ",
" # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # # "
}
local n = 0
for barcodes as barcode do
++n
fmt.write("%2d: ", n)
decode_upc(barcode)
end

161
Task/UPC/Rust/upc.rs Normal file
View file

@ -0,0 +1,161 @@
use std::collections::HashMap;
fn trim(s: &str) -> &str {
s.trim()
}
// Helper function to create the left digits map
fn create_left_digits() -> HashMap<&'static str, i32> {
let mut map = HashMap::new();
map.insert(" ## #", 0);
map.insert(" ## #", 1);
map.insert(" # ##", 2);
map.insert(" #### #", 3);
map.insert(" # ##", 4);
map.insert(" ## #", 5);
map.insert(" # ####", 6);
map.insert(" ### ##", 7);
map.insert(" ## ###", 8);
map.insert(" # ##", 9);
map
}
// Helper function to create the right digits map
fn create_right_digits() -> HashMap<&'static str, i32> {
let mut map = HashMap::new();
map.insert("### # ", 0);
map.insert("## ## ", 1);
map.insert("## ## ", 2);
map.insert("# # ", 3);
map.insert("# ### ", 4);
map.insert("# ### ", 5);
map.insert("# # ", 6);
map.insert("# # ", 7);
map.insert("# # ", 8);
map.insert("### # ", 9);
map
}
const END_SENTINEL: &str = "# #";
const MID_SENTINEL: &str = " # # ";
fn decode_upc(input: &str) {
let left_digits = create_left_digits();
let right_digits = create_right_digits();
let decode = |candidate: &str| -> (bool, Vec<i32>) {
let mut output = Vec::new();
let mut pos = 0;
// Check start sentinel
if candidate.len() < pos + END_SENTINEL.len() {
return (false, output);
}
let part = &candidate[pos..pos + END_SENTINEL.len()];
if part == END_SENTINEL {
pos += END_SENTINEL.len();
} else {
return (false, output);
}
// Decode left 6 digits
for _ in 0..6 {
if candidate.len() < pos + 7 {
return (false, output);
}
let part = &candidate[pos..pos + 7];
pos += 7;
if let Some(&digit) = left_digits.get(part) {
output.push(digit);
} else {
return (false, output);
}
}
// Check middle sentinel
if candidate.len() < pos + MID_SENTINEL.len() {
return (false, output);
}
let part = &candidate[pos..pos + MID_SENTINEL.len()];
if part == MID_SENTINEL {
pos += MID_SENTINEL.len();
} else {
return (false, output);
}
// Decode right 6 digits
for _ in 0..6 {
if candidate.len() < pos + 7 {
return (false, output);
}
let part = &candidate[pos..pos + 7];
pos += 7;
if let Some(&digit) = right_digits.get(part) {
output.push(digit);
} else {
return (false, output);
}
}
// Check end sentinel
if candidate.len() < pos + END_SENTINEL.len() {
return (false, output);
}
let part = &candidate[pos..pos + END_SENTINEL.len()];
if part == END_SENTINEL {
pos += END_SENTINEL.len();
} else {
return (false, output);
}
// Calculate checksum
let sum: i32 = output.iter().enumerate().map(|(i, &digit)| {
if i % 2 == 0 {
3 * digit
} else {
digit
}
}).sum();
(sum % 10 == 0, output)
};
let candidate = trim(input);
let (valid, digits) = decode(candidate);
if valid {
println!("{:?}", digits);
} else {
// Try upside down
let reversed: String = candidate.chars().rev().collect();
let (valid_reversed, digits_reversed) = decode(&reversed);
if valid_reversed {
println!("{:?} Upside down", digits_reversed);
} else if !digits.is_empty() {
println!("Invalid checksum");
} else {
println!("Invalid digit(s)");
}
}
}
fn main() {
let barcodes = vec![
" # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ",
" # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ",
" # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ",
" # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ",
" # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ",
" # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ",
" # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ",
" # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ",
" # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ",
" # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # # ",
];
for barcode in &barcodes {
decode_upc(barcode);
}
}

129
Task/UPC/Scala/upc.scala Normal file
View file

@ -0,0 +1,129 @@
object UPC {
private val SEVEN = 7
private val LEFT_DIGITS = Map(
" ## #" -> 0,
" ## #" -> 1,
" # ##" -> 2,
" #### #" -> 3,
" # ##" -> 4,
" ## #" -> 5,
" # ####" -> 6,
" ### ##" -> 7,
" ## ###" -> 8,
" # ##" -> 9
)
private val RIGHT_DIGITS = LEFT_DIGITS.map { case (key, value) =>
key.replace(' ', 's').replace('#', ' ').replace('s', '#') -> value
}
private val END_SENTINEL = "# #"
private val MID_SENTINEL = " # # "
private def decode(candidate: String): (Boolean, List[Int]) = {
var pos = 0
val output = scala.collection.mutable.ListBuffer[Int]()
// Check start sentinel
if (candidate.length < pos + END_SENTINEL.length ||
candidate.substring(pos, pos + END_SENTINEL.length) != END_SENTINEL) {
return (false, output.toList)
}
pos += END_SENTINEL.length
// Decode left side digits
for (_ <- 1 until SEVEN) {
if (candidate.length < pos + SEVEN) {
return (false, output.toList)
}
val part = candidate.substring(pos, pos + SEVEN)
pos += SEVEN
LEFT_DIGITS.get(part) match {
case Some(digit) => output += digit
case None => return (false, output.toList)
}
}
// Check middle sentinel
if (candidate.length < pos + MID_SENTINEL.length ||
candidate.substring(pos, pos + MID_SENTINEL.length) != MID_SENTINEL) {
return (false, output.toList)
}
pos += MID_SENTINEL.length
// Decode right side digits
for (_ <- 1 until SEVEN) {
if (candidate.length < pos + SEVEN) {
return (false, output.toList)
}
val part = candidate.substring(pos, pos + SEVEN)
pos += SEVEN
RIGHT_DIGITS.get(part) match {
case Some(digit) => output += digit
case None => return (false, output.toList)
}
}
// Check end sentinel
if (candidate.length < pos + END_SENTINEL.length ||
candidate.substring(pos, pos + END_SENTINEL.length) != END_SENTINEL) {
return (false, output.toList)
}
// Calculate checksum
val sum = output.zipWithIndex.map { case (digit, index) =>
if (index % 2 == 0) 3 * digit else digit
}.sum
(sum % 10 == 0, output.toList)
}
private def printList(list: List[Int]): Unit = {
print("[")
print(list.mkString(", "))
print("]")
}
private def decodeUPC(input: String): Unit = {
val candidate = input.trim
val (isValid, digits) = decode(candidate)
if (isValid) {
printList(digits)
println()
} else {
// Try upside down
val reversed = candidate.reverse
val (isValidReversed, digitsReversed) = decode(reversed)
if (isValidReversed) {
printList(digitsReversed)
println(" Upside down")
} else if (digitsReversed.length == 12) {
println("Invalid checksum")
} else {
println("Invalid digit(s)")
}
}
}
def main(args: Array[String]): Unit = {
val barcodes = List(
" # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ",
" # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ",
" # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ",
" # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ",
" # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ",
" # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ",
" # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ",
" # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ",
" # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ",
" # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # # "
)
barcodes.foreach(decodeUPC)
}
}