From aa1bd8cec7e22ff3792ba12384108cc2569e9c0d Mon Sep 17 00:00:00 2001 From: femsci Date: Sat, 11 May 2024 23:30:10 +0200 Subject: [PATCH] task 6 --- README.md | 2 +- dev/meowmeow/Match.java | 80 +++++++++++++++++++++++++++++++ dev/meowmeow/MatchCollection.java | 58 ++++++++++++++++++++++ dev/meowmeow/Meuczki.java | 42 ++++++++++++++++ 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 dev/meowmeow/Match.java create mode 100644 dev/meowmeow/MatchCollection.java create mode 100644 dev/meowmeow/Meuczki.java diff --git a/README.md b/README.md index a4b6575..6a43f9f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # PPJ -This branch contains the solution for the **nth task**. +This branch contains the solution for the **6th task**. [Go back to main](/femsci/ppj/src/branch/nya) diff --git a/dev/meowmeow/Match.java b/dev/meowmeow/Match.java new file mode 100644 index 0000000..4681495 --- /dev/null +++ b/dev/meowmeow/Match.java @@ -0,0 +1,80 @@ +/* + * TASK 6 + * archived at https://git.femboy.science/femsci/ppj/src/branch/task/6 + * by femsci + */ + +package dev.meowmeow; + +// no time to refactor ;w; + +public class Match { + public Match(String cA, String cB, int sA, int sB) { + if (Math.max(sA, sB) == sA) { + countryWinner = cA; + scoreWinner = sA; + countryLoser = cB; + scoreLoser = sB; + } else { + countryWinner = cB; + scoreWinner = sB; + countryLoser = cA; + scoreLoser = sA; + } + } + + private String countryWinner, countryLoser; + private int scoreWinner, scoreLoser; + + public String[] getParticipants() { + return new String[] { countryWinner, countryLoser }; + } + + public boolean isDraw() { + return scoreLoser == scoreWinner; + } + + public String getWinner() { + if (isDraw()) { + throw new IllegalAccessError("draw"); + } + + return countryWinner; + } + + public int getWinnerScore() { + if (isDraw()) { + throw new IllegalAccessError("draw"); + } + + return scoreWinner; + } + + public String getLoser() { + if (isDraw()) { + throw new IllegalAccessError("draw"); + } + + return countryLoser; + } + + public int getLoserScore() { + if (isDraw()) { + throw new IllegalAccessError("draw"); + } + + return scoreLoser; + } + + public int getPoints() { + return isDraw() ? 1 : 3; + } + + public static Match Parse(String[] data) { + if (data.length != 4) { + throw new IllegalArgumentException("Invalid data"); + } + + return new Match(data[0], data[2], Integer.parseInt(data[1]), Integer.parseInt(data[3])); + } +} diff --git a/dev/meowmeow/MatchCollection.java b/dev/meowmeow/MatchCollection.java new file mode 100644 index 0000000..f7bafaf --- /dev/null +++ b/dev/meowmeow/MatchCollection.java @@ -0,0 +1,58 @@ +/* + * TASK 6 + * archived at https://git.femboy.science/femsci/ppj/src/branch/task/6 + * by femsci + */ + +package dev.meowmeow; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Function; + +public class MatchCollection { + + public MatchCollection(List matches) { + _matches = matches; + } + + private List _matches; + + public List getMatches() { + return _matches; + } + + public Map getPointsDistribution() { + Map pointMap = new HashMap<>(); + + Function> incrementBy = c -> i -> pointMap.put(c, pointMap.getOrDefault(c, 0) + i); + + // calculate scores from each match + for (Match match : _matches) { + if (match.isDraw()) { + // increment by one for each participant during draw + incrementBy.apply(match.getParticipants()[0]).accept(1); + incrementBy.apply(match.getParticipants()[1]).accept(1); + continue; + } + + incrementBy.apply(match.getWinner()).accept(3); + // ensure that the loser is embedded in the map + pointMap.putIfAbsent(match.getLoser(), 0); + } + + return pointMap; + } + + // parse this silly structure + public static MatchCollection Parse(String[][] data) { + return new MatchCollection( + Arrays.asList(data) + .stream() + .map(Match::Parse) + .toList()); + } +} diff --git a/dev/meowmeow/Meuczki.java b/dev/meowmeow/Meuczki.java new file mode 100644 index 0000000..b6fd8bf --- /dev/null +++ b/dev/meowmeow/Meuczki.java @@ -0,0 +1,42 @@ +/* + * TASK 6 + * archived at https://git.femboy.science/femsci/ppj/src/branch/task/6 + * by femsci + */ + +package dev.meowmeow; + +import java.util.Arrays; +import java.util.Map; + +public class Meuczki { + public static void main(String[] args) { + // this cute lil copypasted structure, very stringified <3 + String[][] arr = { { "Germany", "2", "Scotland", "1" }, + { "Poland", "2", "Germany", "0" }, + { "Germany", "1", "Ireland", "1" }, + { "Poland", "2", "Scotland", "2" }, + { "Scotland", "1", "Ireland", "0" }, + { "Ireland", "1", "Poland", "1" }, + { "Ireland", "1", "Scotland", "1" }, + { "Germany", "3", "Poland", "1" }, + { "Scotland", "2", "Germany", "3" }, + { "Ireland", "1", "Germany", "0" }, + { "Scotland", "2", "Poland", "2" }, + { "Poland", "2", "Ireland", "1" } }; + + // twoliner 🥺🥺 + MatchCollection tournament = MatchCollection.Parse(arr); + Map points = tournament.getPointsDistribution(); + + int[] pointVector = new int[] { + points.get("Germany"), + points.get("Ireland"), + points.get("Poland"), + points.get("Scotland") + }; + + // :3 + System.out.println(Arrays.toString(pointVector)); + } +}