Skip to content

Instantly share code, notes, and snippets.

@stelf
Created July 19, 2021 13:34
Show Gist options
  • Select an option

  • Save stelf/7d4a861451ad17956ec29da8a09185dd to your computer and use it in GitHub Desktop.

Select an option

Save stelf/7d4a861451ad17956ec29da8a09185dd to your computer and use it in GitHub Desktop.
several attepts to some informatics assignemnt
// -- this GIST is available under the conditions of
// -- https://creativecommons.org/licenses/by/4.0/
//
// this are four approaches to solution to one trivial task from
// Bulgaria's softuni's curriculum that i was asked to help with
//
//
// the input is basically 4 integers K, L, M, N
//
// then we have a integer ranges for the digits A, B, C, D
// that are bound by these K, L, M, N and defined as
// [ K..8 ], [ L..9 ], [ M..8 ], [ N..9 ]
//
// it is stated tthe values for A and C grow starting with K and M up to 8,
// and the values for B and D should go down to L and N respectively
//
// each time the first digit (A, C) is even and the second (B, D) is odd
// a number is to be output in the form "AB - CD", but not when AB and CD
// are the same numbers, which should result in some text printed
//
// no more than 6 results are to be prented
// trivial solution with 4 loops
function substitute(input) {
let [k, l, m, n]= input.map(Number);
let cnt = 6; // number of res
k += k & 1; m += m&1; // prepare bounds
for (let a = k; a<=8 && cnt; a+=2) {
for (let b = 9; b>=l && cnt; b-=2) {
for (let c = m; c<=8 && cnt; c+=2) {
for(let d = 9; d>=n && cnt; d-=2) {
// console.log([a,b,c,d], [num1, num2], [ a!==c ], [ b !== d])
if(a!==c || b!==d) {
console.log(`${a}${b} - ${c}${d}`);
cnt--;
} else {
console.log('Cannot change the same player.');
}
}
}
}
}
}
// two loops solution that not the A, B, C, D but the AB and CD values
function substitute2(input) {
let [k, l, m, n]= input.map(Number);
let cnt = 6; // number of reuslts
k += k&1; m += m&1; // prepare bounds
for (let a = k * 10 + 9; cnt && a < 100; ) {
for (let b = m * 10 + 9; cnt && b < 100; ) {
// console.log([a, b],[ a !== b], cnt)
if(a!==b) {
console.log(`${a} - ${b}`);
cnt--;
} else {
console.log('Cannot change the same player.');
}
b -= 2;
if ( b%10 < n ) b = (Math.floor(b/10) + 2)*10 + 9
}
a -= 2;
if ( a%10 < l ) a = (Math.floor(a/10) + 2)*10 + 9
}
}
// a generic solution which assumes that the number ABCD is a number
// in some weird radix where there are different number of characters
// depending on the position of the character in the representation
//
// this is a generic solution that can be expanded to arbitrary number
// of character positions, arbitrary number of chars for each digits,
// arbitrary change/movement of the values for each position
// (considering the digits are enumerated by means of positive integers)
//
function substitute3(input) {
let [k, l, m, n] = input.map(Number); // console.log('arg', [k,l,m,n])
k += k&1; m += m&1; l += (l+1)&1; n+= (n+1)&1; // console.log('fix', [k,l,m,n])
let ini = [ k, 9, m, 9]; // console.log('ini', ini)
let bnd = [ k + 2, l - 2, m + 2, n - 2]; // console.log('bnd', bnd)
let dir = [2, -2, 2, -2]; // console.log('dir', dir)
let cur = [...ini];
let [cnt, pos] = [ 6, ini.length - 1 ];
while (cnt) { // console.log(cur)
if( cur[0]!==cur[2] || cur[1] !== cur[3]) {
console.log(`${cur[0]}${cur[1]} - ${cur[2]}${cur[3]}`);
cnt--;
} else {
console.log("Cannot change the same player.")
}
cur[pos] += dir[pos];
while (cur[pos] === bnd[pos] && cnt) {
cur[pos] = ini[pos];
!pos-- && (cnt = 0);
cur[pos]+=dir[pos];
}
pos = ini.length - 1;
}
}
// another attempt to generic solution which starts by calculating
// the maximum number of different state sthat each character position
// and thus approaching them as some sort of counter wheels
function substituteX(input) {
let [k, l, m, n] = input.map(Number); // console.log('arg', [k,l,m,n])
k += k&1; m += m&1; l += (l+1)&1; n+= (n+1)&1;
let cnt = 6;
let end = [8, l, 8, n];
let dir = [-1, 1, -1, 1];
let card = [ (8 - k)/2 , (9 - l)/2, (8 - m)/2, (9 - n)/2]
let init = [...card]
while (cnt) {
let [ a, b, c, d ] = dir.map( (d, i) => end[i] + card[i] * 2 * d) ;
if(a!==c || b!==d) {
console.log(`${a}${b} - ${c}${d}`); cnt--;
} else {
console.log('Cannot change the same player.');
}
for (let l = card.length-1; l >= 0;l--) {
card[l] && card[l]-- && (l = -1);
cnt = l == 0 ? l : cnt;
l > 0 && !card[l] && (card[l] = init[l]);
}
}
}
// eventually there should be a one-loop algebraic solution,
// but it may be inevitable to have a nested loop to 'mimic'
// the overflow of
// substitute3([3,4,8,4])
// substitute2([7,6,8,5])
// substitute3([7,6,8,5])
// substitute3([3,6,8,5])
substitute([7,6,8,5])
console.log('------------------------------')
substitute3([7,6,8,5])
console.log('------------------------------')
substituteX([7,6,8,5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment