Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let animals = {};
- let areas = {}
- for (let parts of input) {
- if (parts === 'Last Info') {
- break;
- }
- let [command, name, limit, area] = parts.split(':');
- limit = Number(limit);
- if (command === 'Add') {
- if (!animals.hasOwnProperty(name)) {
- animals[name] = limit;
- if (!areas.hasOwnProperty(area)) {
- areas[area] = 1;
- } else {
- areas[area]++;
- }
- }
- else {
- animals[name] += limit;
- }
- }
- else if (command === 'Feed') {
- if (animals.hasOwnProperty(name)) {
- animals[name] -= limit;
- if (animals[name] <= 0) {
- console.log(`${name} was successfully fed`);
- delete animals[name];
- areas[area]--
- }
- if (areas[area] <= 0){
- delete areas[area]
- }
- }
- }
- }
- console.log(`Animals:`);
- let sorted = Object.entries(animals)
- .sort((a, b) => {
- let result = b[1] - a[1]
- if (result === 0) {
- result = a[0].localeCompare(b[0]);
- }
- return result
- })
- for (let [name, limit] of sorted) {
- console.log(`${name} -> ${limit}g`)
- }
- console.log(`Areas with hungry animals:`);
- let sortedAreas = Object.entries(areas)
- .sort((a, b) => {
- let result = b[1] - a[1]
- return result
- });
- for (let [name, count] of sortedAreas) {
- console.log(`${name} : ${count}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement