Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
no-thread
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include <iostream> #include <vector> #include <filesystem> #include <chrono> #include <algorithm> #include <numeric> #include <thread> #include <future> namespace fs = std::experimental::filesystem; using std::cout; using std::cin; using std::endl; typedef std::pair<uint_fast64_t, uint_fast64_t> pair_64; int64_t F_Print_Time(std::string str, std::chrono::high_resolution_clock::time_point t1) { std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count(); if (str != "") { std::cout << str << duration << " us / " << duration / 1000 << " ms." << std::endl; } return duration / 1000; } int F_Part1(unsigned int pairs, pair_64 gen, std::pair<int, int> fac, int div) { int count = 0; for (unsigned int i = 0; i < pairs; i++) { gen.first = (gen.first * fac.first) % div; gen.second = (gen.second * fac.second) % div; if ((gen.first & 0xffff) == (gen.second & 0xffff)) { count++; } } //cout << "Day15-Part1: " << count << (count == 597 ? ". Correct." : ". Wrong.") << endl; //return 1; return count == 597; } int F_Part2(unsigned int pairs2, pair_64 gen, std::pair<int, int> fac, int div) { int count = 0; int A = -1; int B = -1; for (unsigned int p = 0; p < pairs2; ) { while (A < 0) // generate number for generator A if necessary { gen.first = (gen.first * fac.first) % div; if (gen.first % 4 == 0) { A = gen.first & 0xffff; } } while (B < 0) // generate number for generator B if necessary { gen.second = (gen.second * fac.second) % div; if (gen.second % 8 == 0) { B = gen.second & 0xffff; } } if (A >= 0 && B >= 0) // if both generators have a number ready, compare them { if (A == B) { count++; } A = -1; B = -1; p++; } } //cout << "Day15-Part2: " << count << (count == 303 ? ". Correct." : ". Wrong.") << endl; return 1; } int main(void) { std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); int res_p1 = 597; // result for part 1 int res_p2 = 303; // result for part 2 pair_64 test = pair_64(65, 8921); // test input pair_64 inp = pair_64(516, 190); // actual input pair_64 gen; gen = inp; unsigned int pairs = 40000000; // 40 million pairs for part 1 unsigned int pairs2 = 5000000; // 5 million for part 2 std::pair<int, int> fac(16807, 48271); int div = 2147483647; //F_Print_Time("Prep time: ", t1); cout << F_Part1(pairs, gen, fac, div) << endl; //F_Part2(pairs2, gen, fac, div); //std::thread th1(F_Part1, pairs, gen, fac, div); // removing the // here to run the thread makes it take 800ms instead of 140ms //th1.join(); //std::future<int> p1 = std::async(std::launch::async, F_Part1, pairs, gen, fac, div);//, t1); //std::future<int> p2 = std::async(std::launch::async, F_Part2, pairs2, gen, fac, div);//, t1); //cout << p1.get() << endl; //p2.get(); F_Print_Time("Duration: ", t1); return 0; }
cl.exe
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 3,26 sec, absolute running time: 1,32 sec, absolute service time: 4,59 sec
edit mode
|
history
|
discussion
1 Duration: 1151710 us / 1151 ms.