Skip to content

Instantly share code, notes, and snippets.

@tresabhi
Last active June 23, 2024 18:51
Show Gist options
  • Select an option

  • Save tresabhi/21037e3b5eaae14bf2a6496933f04399 to your computer and use it in GitHub Desktop.

Select an option

Save tresabhi/21037e3b5eaae14bf2a6496933f04399 to your computer and use it in GitHub Desktop.
import { parse } from 'csv-parse/sync';
import { times } from 'lodash';
const raw = await Bun.file('temp/raw.csv').text();
const parsed = parse(raw) as [string, string, string][];
const count: Record<number, number> = {};
let discarded = 0;
times(50, (index) => (count[index + 1] = 0));
parsed.forEach(([_, rawContent, schema], index) => {
if (schema === 'd') {
discarded++;
} else if (schema === 's') {
const pattern = /\d+/g;
const numbers = rawContent.match(pattern)?.map(Number);
const invalid =
numbers === undefined ||
numbers.some(
(number) =>
isNaN(number) || number < 1 || number > 50 || number % 1 !== 0,
);
if (invalid) return discarded++;
numbers.forEach((number) => {
count[number]++;
});
} else if (schema.startsWith('mf')) {
let separator = schema.slice(2);
separator = separator === '' ? 'x' : separator;
separator = separator.replaceAll('*', '\\*').replaceAll('(', '\\(');
const pattern = new RegExp(`(\\d+${separator})?\\d+`, 'g');
const numbers = rawContent.match(pattern)?.map((match) => {
return match.includes(separator)
? match.split(separator).map(Number)
: [1, Number(match)];
});
const invalid =
numbers === undefined ||
numbers.some(
([coefficient, number]) =>
isNaN(number) ||
number < 1 ||
number > 50 ||
number % 1 !== 0 ||
isNaN(coefficient) ||
coefficient < 1 ||
coefficient % 1 !== 0,
);
if (invalid) return discarded++;
numbers?.forEach(([coefficient, number]) => {
count[number] += coefficient;
});
} else if (schema.startsWith('mb')) {
let separator = schema.slice(2);
separator = separator === '' ? 'x' : separator;
separator = separator.replaceAll('*', '\\*').replaceAll('(', '\\(');
const pattern = new RegExp(`(\\d+${separator})?\\d+`, 'g');
const numbers = rawContent.match(pattern)?.map((match) => {
return match.includes(separator)
? match.split(separator).map(Number)
: [Number(match), 1];
});
const invalid =
numbers === undefined ||
numbers.some(
([number, coefficient]) =>
isNaN(number) ||
number < 1 ||
number > 50 ||
number % 1 !== 0 ||
isNaN(coefficient) ||
coefficient < 1 ||
coefficient % 1 !== 0,
);
if (invalid) return discarded++;
numbers?.forEach(([number, coefficient]) => {
count[number] += coefficient;
});
}
});
Bun.write(
'temp/output.csv',
Object.entries(count)
.map((entry) => entry.join(','))
.join('\n'),
);
console.log(discarded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment