nina-dk / nina-dk.github.io

Tech blog, built using Hexo.
https://nina-dk.github.io
0 stars 0 forks source link

New Year's Drinking (Coding) Challenge | antonina kallinteri #2

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

New Year's Drinking (Coding) Challenge | antonina kallinteri

Antonina Kallinteri's tech blog.

https://nina-dk.github.io/2022/01/02/new-years-drinking-challenge/

mabbason commented 2 years ago
let friends = {
      1: ['1-6', '1-6', '1-6', '1-6'],
      2: ['6', '1,6', '6', '6'],
      3: ['6', '1,6', '6', '6'],
      4: ['1-6', '1,6', '1-6', '1-6'],
      5: ['1', '1,6', '1', '1'],
      6: ['1', '1,6', '1', '1'],
      7: ['1-6', '1-6', '1-6', '1-6']
  };

function drinkingTimeline(ppl) {
  for (let person in ppl) {
    let drinkingTimeline = ppl[person].map(round => {
      if (round.includes('-')) return getShotsRange(round);
      return getShots(round);
    }).join('      ');

    console.log(drinkingTimeline);
  }
}

function getShotsRange(str) {
  let [ start, end ] = [...str.split('-').map(idx => idx - 1)];
  end += 1;
  return new Array(end - start).fill('*', start, end).join('');;
}

function getShots(str) {
  let round = new Array(6).fill(' ');
  let indices = str.match(/[0-9]/g).map(idx => idx - 1);
  return round.map((_, idx) => indices.includes(idx) ? '*': _).join('');
}

drinkingTimeline(friends);
nina-dk commented 2 years ago
function intervalsOfSix(timeline) {
  return timeline.match(/\s{6}/g);
}

function drankAllShots(timeline) {
  return intervalsOfSix(timeline).map((sixMins, idx) => {
    return idx % 2 === 0 ? "*".repeat(6) : sixMins;
  }).join("");
}

function everyFirstShot(timeline) {
  return intervalsOfSix(timeline).map((sixMins, idx) => {
    return (idx % 2 === 0 ? "*" + sixMins.slice(1) : sixMins);
  }).join("");
} 

function everyLastShot(timeline) {
  return intervalsOfSix(timeline).map((sixMins, idx) => {
    return idx % 2 === 0 ? sixMins.slice(0, 5) + "*" : sixMins;
  }).join("");
}

function secAndThirdPal(timeline) {
  timeline = everyLastShot(timeline);
  return timeline.slice(0, 12) + "*" + timeline.slice(13);
}

function fourthPal(timeline) {
  timeline = drankAllShots(timeline);
  return timeline.slice(0, 13) + " ".repeat(4) + timeline.slice(17);
}

function fifthAndSixthPal(timeline) {
  timeline = everyFirstShot(timeline);
  return timeline.slice(0, 17) + "*" + timeline.slice(18);
}

function drinkingChallenge() {
  let timelines = Array(7).fill(" ".repeat(42));
  for (let idx = 0; idx < timelines.length; idx += 1) {
    switch (idx) {
      case 0:
      case 6: console.log(drankAllShots(timelines[idx])); break;
      case 1:
      case 2: console.log(secAndThirdPal(timelines[idx])); break;
      case 3: console.log(fourthPal(timelines[idx])); break;
      case 4:
      case 5: console.log(fifthAndSixthPal(timelines[idx])); break;
    }
  }
}

drinkingChallenge();