Advertisement
Guest User

03. Legendary Farming

a guest
Mar 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Comparator;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7.  
  8. public class Count {
  9. public static void main(String[] args) throws IOException {
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11. LinkedHashMap<String, Integer> keyMaterials = new LinkedHashMap<>();
  12. keyMaterials.put("shards", 0);
  13. keyMaterials.put("fragments", 0);
  14. keyMaterials.put("motes", 0);
  15. LinkedHashMap<String, Integer> junks = new LinkedHashMap<>();
  16. String winner = "";
  17. boolean flag = false;
  18. do {
  19. String[] data = reader.readLine().split("\\s+");
  20. for (int i = 0; i < data.length; i += 2) {
  21. int quantity = Integer.parseInt(data[i]);
  22. String material = data[i + 1];
  23. if (!keyMaterials.containsKey(material)) {
  24. if (!junks.containsKey(material)) {
  25. junks.put(material, quantity);
  26. } else {
  27. junks.put(material, junks.get(material) + quantity);
  28. }
  29. } else {
  30. keyMaterials.put(material, keyMaterials.get(material) + quantity);
  31. if (keyMaterials.get(material) >= 250) {
  32. keyMaterials.put(material, keyMaterials.get(material) - 250);
  33. winner = material;
  34. flag = true;
  35. break;
  36. }
  37. }
  38. }
  39. } while (!flag);
  40. if (winner.equals("shards")) {
  41. System.out.println("Shadowmourne obtained!");
  42. } else if (winner.equals("motes")) {
  43. System.out.println("Dragonwrath obtained!");
  44. } else if (winner.equals("fragments")) {
  45. System.out.println("Valanyr obtained!");
  46. }
  47. keyMaterials
  48. .entrySet()
  49. .stream()
  50. .sorted((e1, e2) ->
  51. {
  52. int sort = Integer.compare(e2.getValue(),
  53. e1.getValue());
  54. if (sort == 0) {
  55. sort = e1.getKey().compareTo(e2.getKey());
  56. }
  57. return sort;
  58. }).forEach(e -> {
  59. System.out.println(String.format("%s: %d", e.getKey(), e.getValue()));
  60. });
  61. junks.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
  62. .forEach(e -> {
  63. System.out.println(String.format("%s: %d", e.getKey(), e.getValue()));
  64. });
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement