My 1billion row challenge solutions in various languages
at main 3.3 kB view raw
1/* 2 * Copyright 2023 The original authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import java.nio.file.Files; 18import java.nio.file.Paths; 19import java.util.ArrayList; 20import java.util.Comparator; 21import java.util.List; 22import java.util.stream.Collectors; 23import java.time.Duration; 24 25public class process_output { 26 27 public static void main(String... args) throws Exception { 28 String expectedFile = args[0]; 29 String actualFile = args[1]; 30 31 String expected = new String(Files.readAllBytes(Paths.get(expectedFile))); 32 List<String> times = new ArrayList<>(); 33 34 var outputLines = Files.lines(Paths.get(actualFile)) 35 .collect(Collectors.toList()); 36 37 int matched = 0; 38 39 for (String line : outputLines) { 40 if (line.contains("Hamburg")) { 41 if (!line.equals(expected)) { 42 System.err.println("FAILURE Unexpected output"); 43 System.err.println(line); 44 } 45 else { 46 matched++; 47 } 48 } 49 else if (line.startsWith("real")) { 50 times.add(line); 51 } 52 } 53 54 if (matched == 5) { 55 System.out.println("OK Output matched"); 56 } 57 else { 58 System.err.println("FAILURE Output didn't match"); 59 } 60 61 System.out.println(); 62 System.out.println(actualFile); 63 64 System.out.println(times.stream() 65 .map(t -> t.substring(5)) 66 .map(t -> t.replace("s", "").replace("m", ":")) 67 .collect(Collectors.joining(System.lineSeparator()))); 68 69 var asDurations = times.stream() 70 .map(t -> t.substring(5)) 71 .map(t -> t.replace("s", "S").replace("m", "M")) 72 .map(t -> "PT" + t) 73 .map(Duration::parse) 74 .collect(Collectors.toList()); 75 76 var min = asDurations.stream().min(Comparator.naturalOrder()).get(); 77 var max = asDurations.stream().max(Comparator.naturalOrder()).get(); 78 79 var evaluated = asDurations.stream() 80 .filter(d -> d != min && d != max) 81 .collect(Collectors.toList()); 82 83 var mean = evaluated.get(0).plus(evaluated.get(1)).plus(evaluated.get(2)).dividedBy(3); 84 var result = String.format("%02d:%02d.%.0f", mean.toMinutesPart(), mean.toSecondsPart(), (double) mean.toNanosPart() / 1_000_000); 85 var author = actualFile.replace(".out", ""); 86 87 System.out.println(String.format("\n| | %s| [link](https://github.com/gunnarmorling/1brc/blob/main/src/main/java/dev/morling/onebrc/CalculateAverage_%s.java)| 21.0.1-open | [%s](https://github.com/%s)|", result, author, author, author)); 88 } 89}