Skip to content

Commit 936d516

Browse files
committed
add solutions 20,21,22
1 parent a3b8137 commit 936d516

File tree

10 files changed

+570
-0
lines changed

10 files changed

+570
-0
lines changed

2022/challenge-20/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## 🔢 Instructions
2+
Santa Claus has realized that even with the collaboration of all the elves he will not be able to deliver all the gifts in time. That's why he will ask for help from his friends at Autentia.
3+
4+
From Autentia they have indicated that they need a program to know which team of reindeer to send to each country. There are different types of reindeer and each one of them can carry a weight of gifts. For example:
5+
6+
const reindeerTypes = [
7+
{ type: 'Nuclear', weightCapacity: 50 },
8+
{ type: 'Electric', weightCapacity: 10 },
9+
{ type: 'Gasoline', weightCapacity: 5 },
10+
{ type: 'Diesel', weightCapacity: 1 }
11+
]
12+
13+
In Santa Claus' list of gifts, the weight of each gift and its destination country are expressed. The weight of the gifts is always a natural number. For example:
14+
15+
const gifts = [
16+
{ country: 'Spain', weight: 30 },
17+
{ country: 'France', weight: 17 },
18+
{ country: 'Italy', weight: 50 }
19+
]
20+
21+
Autentia tells us that, for the team of reindeer to send to each country to be optimal, we should:
22+
23+
- Send the maximum number of reindeer possible of greater load capacity
24+
- Make the most of the weight that each reindeer can carry.
25+
- The reindeer have a rather strange character and do not accept that in the team there are more reindeer of a type than reindeer of the next type in descending order of load capacity.
26+
- For example. To France (17) you would not send seventeen diesel reindeer (17 * 1). There are reindeer with greater load capacity, but you would not send a nuclear reindeer (50), since you would be wasting its capacity. An electric reindeer (10) would be sent, one gasoline (5) and two diesel (2 * 1).
27+
- To Spain (37) you could not send a team made up of three electric reindeer (3 * 10), one gasoline (5) and two diesel (2 * 1), since there cannot be more electric reindeer than gasoline. Nor two electric reindeer (2 * 10), three gasoline (3 * 5) and two diesel (2 * 1), since there cannot be more gasoline than diesel. You would have to send two electric reindeer (2 * 10), two gasoline (2 * 5) and seven diesel (7 * 1).
28+
29+
const reindeerTypes = [
30+
{ type: 'Nuclear', weightCapacity: 50 },
31+
{ type: 'Electric', weightCapacity: 10 },
32+
{ type: 'Gasoline', weightCapacity: 5 },
33+
{ type: 'Diesel', weightCapacity: 1 }
34+
]
35+
36+
const gifts = [
37+
{ country: 'Spain', weight: 30 },
38+
{ country: 'France', weight: 17 },
39+
{ country: 'Italy', weight: 50 }
40+
]
41+
42+
howManyReindeers(reindeerTypes, gifts)
43+
// [{
44+
// country: 'Spain',
45+
// reindeers: [
46+
// { type: 'Electric', num: 1 },
47+
// { type: 'Gasoline', num: 3 },
48+
// { type: 'Diesel', num: 5 }
49+
// ]
50+
// }, {
51+
// country: 'France',
52+
// reindeers: [
53+
// { type: 'Electric', num: 1 },
54+
// { type: 'Gasoline', num: 1 },
55+
// { type: 'Diesel', num: 2 }
56+
// ]
57+
// }, {
58+
// country: 'Italy',
59+
// reindeers: [
60+
// { type: 'Electric', num: 3 },
61+
// { type: 'Gasoline', num: 3 },
62+
// { type: 'Diesel', num: 5 }
63+
// ]
64+
// }]
65+
66+
To take into account:
67+
- There will always be a reindeer type with a load capacity of 1.
68+
- There will always be at least two types of reindeer available.
69+
- There is no limit to the number of reindeer of the same type to send as long as it complies with the restrictions previously stated.
70+
- The reindeer types are always ordered by descending load capacity.

2022/challenge-20/solution.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {{type: string, weightCapacity: number}[]} reindeerTypes
3+
* @param {{country: string, weight: number}[]} gifts
4+
* @returns {{country: string, reindeers: { type: string, num: number }[]}[]}
5+
*/
6+
const howManyReindeers = (reindeerTypes, gifts) => {
7+
const getReindeers = (/** @type {number} */ weight) => {
8+
const allowedReindeersTypes = reindeerTypes.filter(
9+
reindeerType => reindeerType.weightCapacity < weight
10+
)
11+
12+
let totalWeightCapacity = allowedReindeersTypes.reduce(
13+
(acc, reindeerType) => acc + reindeerType.weightCapacity,
14+
0
15+
)
16+
17+
return allowedReindeersTypes
18+
.map(reindeerType => {
19+
const num = Math.floor(weight / totalWeightCapacity)
20+
21+
weight -= num * reindeerType.weightCapacity
22+
totalWeightCapacity -= reindeerType.weightCapacity
23+
24+
return {
25+
type: reindeerType.type,
26+
num,
27+
}
28+
})
29+
.filter(reindeerType => reindeerType.num > 0)
30+
}
31+
32+
reindeerTypes.sort((a, b) => b.weightCapacity - a.weightCapacity)
33+
34+
return gifts.map(gift => ({
35+
country: gift.country,
36+
reindeers: getReindeers(gift.weight),
37+
}))
38+
}
39+
40+
export {howManyReindeers}

2022/challenge-20/solution.spec.js

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
import {howManyReindeers} from './solution'
2+
3+
const cases = [
4+
{
5+
reindeerTypes: [
6+
{type: 'Nuclear', weightCapacity: 50},
7+
{type: 'Electric', weightCapacity: 10},
8+
{type: 'Gasoline', weightCapacity: 5},
9+
{type: 'Diesel', weightCapacity: 1},
10+
],
11+
gifts: [
12+
{country: 'Spain', weight: 30},
13+
{country: 'France', weight: 17},
14+
{country: 'Italy', weight: 50},
15+
],
16+
expected: [
17+
{
18+
country: 'Spain',
19+
reindeers: [
20+
{
21+
type: 'Electric',
22+
num: 1,
23+
},
24+
{
25+
type: 'Gasoline',
26+
num: 3,
27+
},
28+
{
29+
type: 'Diesel',
30+
num: 5,
31+
},
32+
],
33+
},
34+
{
35+
country: 'France',
36+
reindeers: [
37+
{
38+
type: 'Electric',
39+
num: 1,
40+
},
41+
{
42+
type: 'Gasoline',
43+
num: 1,
44+
},
45+
{
46+
type: 'Diesel',
47+
num: 2,
48+
},
49+
],
50+
},
51+
{
52+
country: 'Italy',
53+
reindeers: [
54+
{
55+
type: 'Electric',
56+
num: 3,
57+
},
58+
{
59+
type: 'Gasoline',
60+
num: 3,
61+
},
62+
{
63+
type: 'Diesel',
64+
num: 5,
65+
},
66+
],
67+
},
68+
],
69+
},
70+
{
71+
reindeerTypes: [
72+
{type: 'Electric', weightCapacity: 10},
73+
{type: 'Gasoline', weightCapacity: 5},
74+
{type: 'Diesel', weightCapacity: 1},
75+
],
76+
gifts: [{country: 'Spain', weight: 37}],
77+
expected: [
78+
{
79+
country: 'Spain',
80+
reindeers: [
81+
{
82+
type: 'Electric',
83+
num: 2,
84+
},
85+
{
86+
type: 'Gasoline',
87+
num: 2,
88+
},
89+
{
90+
type: 'Diesel',
91+
num: 7,
92+
},
93+
],
94+
},
95+
],
96+
},
97+
{
98+
reindeerTypes: [
99+
{type: 'Nuclear', weightCapacity: 50},
100+
{type: 'Electric', weightCapacity: 10},
101+
{type: 'Gasoline', weightCapacity: 5},
102+
{type: 'Diesel', weightCapacity: 1},
103+
],
104+
gifts: [
105+
{country: 'Spain', weight: 30},
106+
{country: 'Germany', weight: 7},
107+
{country: 'France', weight: 17},
108+
{country: 'Italy', weight: 50},
109+
],
110+
expected: [
111+
{
112+
country: 'Spain',
113+
reindeers: [
114+
{
115+
type: 'Electric',
116+
num: 1,
117+
},
118+
{
119+
type: 'Gasoline',
120+
num: 3,
121+
},
122+
{
123+
type: 'Diesel',
124+
num: 5,
125+
},
126+
],
127+
},
128+
{
129+
country: 'Germany',
130+
reindeers: [
131+
{
132+
type: 'Gasoline',
133+
num: 1,
134+
},
135+
{
136+
type: 'Diesel',
137+
num: 2,
138+
},
139+
],
140+
},
141+
{
142+
country: 'France',
143+
reindeers: [
144+
{
145+
type: 'Electric',
146+
num: 1,
147+
},
148+
{
149+
type: 'Gasoline',
150+
num: 1,
151+
},
152+
{
153+
type: 'Diesel',
154+
num: 2,
155+
},
156+
],
157+
},
158+
{
159+
country: 'Italy',
160+
reindeers: [
161+
{
162+
type: 'Electric',
163+
num: 3,
164+
},
165+
{
166+
type: 'Gasoline',
167+
num: 3,
168+
},
169+
{
170+
type: 'Diesel',
171+
num: 5,
172+
},
173+
],
174+
},
175+
],
176+
},
177+
{
178+
reindeerTypes: [
179+
{type: 'Diesel', weightCapacity: 1},
180+
{type: 'Gasoline', weightCapacity: 5},
181+
],
182+
gifts: [
183+
{country: 'Spain', weight: 30},
184+
{country: 'Germany', weight: 7},
185+
],
186+
expected: [
187+
{
188+
country: 'Spain',
189+
reindeers: [
190+
{
191+
type: 'Gasoline',
192+
num: 5,
193+
},
194+
{
195+
type: 'Diesel',
196+
num: 5,
197+
},
198+
],
199+
},
200+
{
201+
country: 'Germany',
202+
reindeers: [
203+
{
204+
type: 'Gasoline',
205+
num: 1,
206+
},
207+
{
208+
type: 'Diesel',
209+
num: 2,
210+
},
211+
],
212+
},
213+
],
214+
},
215+
]
216+
217+
describe('Day 20 challenge', () => {
218+
it.each(cases)(
219+
'organizes gifts in the adequate reindeers types',
220+
({reindeerTypes, gifts, expected}) => {
221+
expect(howManyReindeers(reindeerTypes, gifts)).toEqual(expected)
222+
}
223+
)
224+
})

2022/challenge-21/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## 🔢 Instructions
2+
3+
There are too many letters from children asking for gifts and it is very difficult that we can inventory all of them. That's why we have decided to create a program that draws us a table with the gifts we ask for and their quantities.
4+
5+
For this we are given an array of objects with the names of the gifts and their quantities. Write a function that receives this array and returns a string with the drawn table.
6+
7+
```javascript
8+
printTable([
9+
{ name: 'Game', quantity: 2 },
10+
{ name: 'Bike', quantity: 1 },
11+
{ name: 'Book', quantity: 3 }
12+
])
13+
14+
+++++++++++++++++++
15+
| Gift | Quantity |
16+
| ---- | -------- |
17+
| Game | 2 |
18+
| Bike | 1 |
19+
| Book | 7 |
20+
*******************
21+
```
22+
23+
Another example where you can see that the table always uses only the exact space depending on the length of the names of the gifts and the quantities.
24+
25+
```javascript
26+
printTable([
27+
{ name: 'PlayStation 5', quantity: 9234782374892 },
28+
{ name: 'Book Learn Web Dev', quantity: 23531 }
29+
])
30+
31+
++++++++++++++++++++++++++++++++++++++
32+
| Gift | Quantity |
33+
| ------------------ | ------------- |
34+
| PlayStation 5 | 9234782374892 |
35+
| Book Learn Web Dev | 23531 |
36+
**************************************
37+
```
38+
39+
As you can see, the size of the cells depends on the length of the names of the gifts and the quantities, although they will at least have to be the space of the titles Gift and Quantity respectively.
40+
41+
The table uses the symbols: + for the top border, \* for the bottom border, - for the horizontal lines and | for the vertical lines.
42+
43+
Keep in mind:
44+
45+
- Use only the space you need to draw the table.
46+
- Adapt the table to the length of the names of the gifts and the quantities or the titles of the columns.
47+
- There is no need to order the results.
48+
- The table does not end with a line break.

0 commit comments

Comments
 (0)