diff --git a/output.txt b/output.txt index 35d4cb97..8a83fa97 100644 --- a/output.txt +++ b/output.txt @@ -1,6 +1,6 @@ -Sidi_Slimane 487539,37 -Onion 1,01 -Turnip 1,01 -Salsify 1,04 -Butternut_Squash 1,06 -Clementine 1,06 \ No newline at end of file +Tetouan 48.50 +gaz 1.30 +potato 3.50 +tomato 3.50 +sugar 4.50 +flour 5.20 diff --git a/submissions/Ibourk-El/index.js b/submissions/Ibourk-El/index.js new file mode 100644 index 00000000..d0258bc2 --- /dev/null +++ b/submissions/Ibourk-El/index.js @@ -0,0 +1,79 @@ +const fs = require("fs"); +const readline = require("readline"); + +const inputFile = fs.createReadStream("input.txt"); +const outputFile = fs.createWriteStream("output.txt"); + +const bestCity = async () => { + try { + const rl = readline.createInterface({ + input: inputFile, + crlfDelay: Infinity, + }); + let cityData = {}; + for await (const line of rl) { + if (line !== "") { + const [city, product, priceStr] = line.split(","); + const price = parseFloat(priceStr); + if (city in cityData) { + // check if the product is in list + if (product in cityData[city].product) { + let ind = cityData[city].product[product]; + // change the value of the product to cheapes one + if (cityData[city].price[ind].price > price) { + cityData[city].price[ind].price = price; + } + } else { + // add new product + cityData[city].product[product] = cityData[city].index; + cityData[city].price.push({ name: product, price: price }); + cityData[city].index += 1; + } + cityData[city].sum += price; + } else { + // add new city + cityData[city] = { + product: {}, + price: [{ name: product, price: price }], + sum: price, + index: 1, + }; + cityData[city].product[product] = 0; + } + } + } + + let smallestSum = Infinity; + let bestCity = ""; + for (let city in cityData) { + if (cityData[city].sum < smallestSum) { + smallestSum = cityData[city].sum; + bestCity = city; + } + } + + cityData[bestCity].price.sort((a, b) => { + if (a.price === b.price) { + if (a.name > b.name) return 1; + else if (a.name < b.name) return -1; + else return 0; + } + return a.price - b.price; + }); + + let result = `${bestCity} ${smallestSum.toFixed(2)} \n`; + let i = 0; + while (i < 5) { + let t = cityData[bestCity].price[i].name; + result += t + " " + cityData[bestCity].price[i].price.toFixed(2) + "\n"; + i++; + } + + outputFile.write(result); + outputFile.end(); + } catch (e) { + console.log(e); + } +}; + +bestCity(); diff --git a/submissions/Touil-Ali/index.js b/submissions/Touil-Ali/index.js deleted file mode 100644 index 8e7190a3..00000000 --- a/submissions/Touil-Ali/index.js +++ /dev/null @@ -1,70 +0,0 @@ -const fs = require("fs"); -const readline = require("readline"); - -const input = fs.createReadStream("input.txt"); -const output = fs.createWriteStream("output.txt"); - -const calculateSumByCity = async () => { - try { - const rl = readline.createInterface({ - input: input, - crlfDelay: Infinity, - }); - - let smallestCity = null; - let smallestSum = Infinity; - const cityData = new Map(); - const productData = new Map(); - - for await (const line of rl) { - const [city, product, priceStr] = line.split(","); - const price = parseFloat(priceStr); - - if (!isNaN(price) && product !== "") { - cityData.set(city, (cityData.get(city) || 0) + parseFloat(price)); - - if (!productData.has(city)) { - productData.set(city, new Set()); - } - productData.get(city).add({ product, price }); - } - } - - for (const [city, sum] of cityData) { - if (sum < smallestSum) { - smallestSum = sum; - smallestCity = city; - } - } - - if (smallestCity) { - const sortedProducts = Array.from(productData.get(smallestCity)) - .sort((a, b) => a.price - b.price || a.product.localeCompare(b.product)) - .reduce((acc, current) => { - if (!acc.some((x) => x.product === current.product)) { - acc.push(current); - } - return acc; - }, []) - .slice(0, 6); - - const products = sortedProducts.map( - ({ product, price }) => `${product} ${price.toFixed(2)}`, - ); - - const outputArray = [ - `${smallestCity} ${smallestSum.toFixed(2)}`, - ...products, - ]; - - output.write(outputArray.join("\n")); - output.end(); - } - } catch (e) { - console.log(e); - } -}; - -console.time("benchmark"); -calculateSumByCity(); -console.timeEnd("benchmark"); diff --git a/submissions/aboullaite/Main.java b/submissions/aboullaite/Main.java deleted file mode 100644 index d4958643..00000000 --- a/submissions/aboullaite/Main.java +++ /dev/null @@ -1,65 +0,0 @@ - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.AbstractMap; -import java.util.Arrays; -import java.util.Comparator; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - - -public class Main { - - public static final String INPUT_TXT = "input.txt"; - public static final String OUTPUT_TXT = "./output.txt"; - - private record Product(String product, double price) {} - private record CityRecord(double total, Product[] products) { - CityRecord(double price, Product product) { - this(price, new Product[]{product}); - } - public static CityRecord combineWith(CityRecord c1, CityRecord c2) { - return new CityRecord( - c1.total() + c2.total(), - Stream.concat(Arrays.stream(c1.products()), Arrays.stream(c2.products)) - .sorted(Comparator.comparingDouble(Product::price).thenComparing(Product::product)) - .distinct() - .limit(5) - .toArray(Product[]::new) - ); - } - } - - public static void main(String[] args) throws IOException { - - try (Stream lines = java.nio.file.Files.lines(Paths.get(INPUT_TXT))) { - Map resultMap = lines.parallel().map(line -> { - String[] content = line.split(","); - Product product = new Product(content[1], Double.parseDouble(content[2])); - return new AbstractMap.SimpleEntry<>(content[0], product); - }).collect(Collectors.toConcurrentMap( - // Combine/reduce: - AbstractMap.SimpleEntry::getKey, - entry -> new CityRecord(entry.getValue().price(), entry.getValue()), - CityRecord::combineWith)); - Optional result = resultMap.entrySet().stream() - .sorted(Entry.comparingByValue(Comparator.comparingDouble(CityRecord::total))) - .findFirst() - .map(entry -> { - Product[] products = entry.getValue().products(); - String content = entry.getKey() + " " + String.format( "%.2f", entry.getValue().total()) + "\n"; - for (Product product : products) { - content += product.product() + " " + String.format( "%.2f", product.price())+ "\n"; - } - return content; - }); - - // Flush to output file - Files.write(Paths.get(OUTPUT_TXT), result.get().getBytes()); - } - } -} diff --git a/submissions/conan/Main.java b/submissions/conan/Main.java deleted file mode 100644 index 830ee929..00000000 --- a/submissions/conan/Main.java +++ /dev/null @@ -1,86 +0,0 @@ -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import java.io.FileWriter; - -import static java.util.stream.Collectors.toList; - -public class Main { - - private record CityData(String name, String product, BigDecimal price) {} - - public static void main(String[] args) { - processData(); - } - private static void processData(){ - Map map = new HashMap<>(); - List data = new ArrayList<>(); - try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) { - String line; - while ((line = br.readLine()) != null) { - String[] lineArr = line.split(","); - String city = lineArr[0]; - String name = lineArr[1]; - BigDecimal price = new BigDecimal(lineArr[2]); - if(map.keySet().contains(city)){ - BigDecimal totalPrice = map.get(city); - map.put(city, totalPrice.add(price)); - }else{ - map.put(city, price); - } - data.add(new CityData(city, name, price)); - data = filterCheapestProdList(data, city); - } - } catch (IOException e) { - e.printStackTrace(); - } - writer(map, data); - } - - private static List filterCheapestProdList(List cityData, String name){ - Stream collect1 = cityData.stream() - .filter(cd -> cd.name.equals(name)) - .sorted((p1, p2) -> p1.price.compareTo(p2.price)) - .collect(Collectors.toMap(CityData::product, obj -> obj, (first, second) -> first)) - .values() - .stream() - .sorted((p1, p2) -> p1.price.compareTo(p2.price)) - .limit(5); - - Stream collect2 = cityData.stream() - .filter(cd -> !cd.name.equals(name)); - - return Stream.concat(collect1, collect2).collect(toList()); - - } - - private static String formatResult(Map map, List data){ - Map.Entry min = Collections.min(map.entrySet(), - Map.Entry.comparingByValue()); - Stream fiveCheapestProd = data.stream() - .filter(cd -> cd.name.equals(min.getKey())) - .sorted((cd1, cd2) -> cd1.price.compareTo(cd2.price)); - - StringBuilder sb = new StringBuilder(); - sb.append(min.getKey() + " " + min.getValue().setScale(2, RoundingMode.FLOOR)); - fiveCheapestProd.forEach(cd -> sb.append("\n" + cd.product + " " + cd.price.setScale(2, RoundingMode.FLOOR)) ); - return sb.toString(); - } - - - private static void writer(Map map, List data){ - try { - FileWriter wr = new FileWriter("output.txt"); - wr.write(formatResult(map, data)); - wr.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } -} \ No newline at end of file