H2G2.spec.ts 3.52 KB
Newer Older
Anthony REY's avatar
Anthony REY committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const BOOK_PRICE = 8;
const DISCOUNT_TWO_BOOKS = 0.95;
const DISCOUNT_THREE_BOOKS = 0.9;
const DISCOUNT_FOUR_BOOKS = 0.8;
const DISCOUNT_FIVE_BOOKS = 0.75;

const DISCOUNT = new Map([
  [2, DISCOUNT_TWO_BOOKS],
  [3, DISCOUNT_THREE_BOOKS],
  [4, DISCOUNT_FOUR_BOOKS],
  [5, DISCOUNT_FIVE_BOOKS],
]);

enum Book {
  FIRST = 'FIRST',
  SECOND = 'SECOND',
  THIRD = 'THIRD',
  FOURTH = 'FOURTH',
  FIFTH = 'FIFTH',
}

const discountFor = (distinct: number): number => DISCOUNT.get(distinct) || 1;

const maxSerie = (occurences: Map<Book, number>) =>
  Array.from(occurences)
    .map(([, number]) => number)
    .filter((number) => number > 0).length;

function decrementOccurence(occurence: number) {
  if (occurence === 0) {
    return 0;
  }
  return occurence - 1;
}

const decrementSerie = (occurences: Map<Book, number>): Map<Book, number> =>
  new Map(Array.from(occurences).map(([book, occurence]) => [book, decrementOccurence(occurence)]));

const getOccurences = (books: Book[]) =>
  new Map(
    [Book.FIRST, Book.SECOND, Book.THIRD, Book.FOURTH, Book.FIFTH].map((book) => [
      book,
      books.filter((currentBook) => currentBook === book).length,
    ])
  );

const fill = (number: number, size: number): number[] => Array(number).fill(size);

const optimizePacks = (packs: number[]): number[] => {
  const one = packs.filter((size) => size === 1).length;
  const two = packs.filter((size) => size === 2).length;
  const three = packs.filter((size) => size === 3).length;
  const four = packs.filter((size) => size === 4).length;
  const five = packs.filter((size) => size === 5).length;

  const difference = Math.min(three, five);
  return [
    ...fill(one, 1),
    ...fill(two, 2),
    ...fill(three - difference, 3),
    ...fill(four + difference * 2, 4),
    ...fill(five - difference, 5),
  ];
};

const price = (books: Book[]): number => {
  let occurences = getOccurences(books);

  const packs: number[] = [];

  while (maxSerie(occurences) > 0) {
    packs.push(maxSerie(occurences));
    occurences = decrementSerie(occurences);
  }

  return optimizePacks(packs)
    .map((size) => size * BOOK_PRICE * discountFor(size))
    .reduce((accumulation, current) => accumulation + current, 0);
};

describe('H2G2', () => {
  it('Price of no book should be 0', () => {
    expect(price([])).toBe(0);
  });

  it('Price of one book is 8', () => {
    expect(price([Book.FIRST])).toBe(8);
  });

  it('Price of same book is 8 each', () => {
    expect(price([Book.SECOND, Book.SECOND, Book.SECOND, Book.SECOND])).toBe(32);
  });

  it('Price two different books should have 5% off', () => {
    expect(price([Book.THIRD, Book.FOURTH])).toBe(8 * 2 * 0.95);
  });

  it('Price three different books should have 10% off', () => {
    expect(price([Book.THIRD, Book.FOURTH, Book.FIFTH])).toBe(8 * 3 * 0.9);
  });

  it('Price four different books should have 20% off', () => {
    expect(price([Book.FIRST, Book.THIRD, Book.FOURTH, Book.FIFTH])).toBe(8 * 4 * 0.8);
  });

  it('Price five different books should have 25% off', () => {
    expect(price([Book.FIRST, Book.SECOND, Book.THIRD, Book.FOURTH, Book.FIFTH])).toBe(8 * 5 * 0.75);
  });

  it('Should discount of two different series be independent', () => {
    expect(price([Book.FIRST, Book.SECOND, Book.THIRD, Book.FOURTH, Book.FIFTH, Book.FIRST, Book.SECOND])).toBe(
      8 * 5 * 0.75 + 8 * 2 * 0.95
    );
  });

  it('Should treat price of edge case', () => {
    expect(price([Book.FIRST, Book.SECOND, Book.THIRD, Book.FOURTH, Book.FIFTH, Book.FIRST, Book.SECOND, Book.FOURTH])).toBe(
      8 * 4 * 2 * 0.8
    );
  });
});