diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2017-12-15 07:16:22 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2017-12-15 07:16:22 +0200 |
commit | 01e4a859a8f515fd670fd94d3817cb8bffc6dcd9 (patch) | |
tree | 670a401e307f49a160f21b24114a734050b47a02 /src/bin | |
parent | 7a9ac1e71d92988411e54bdf7bb2c20f99177bdb (diff) |
Day 15: Bitwise and, and the generators
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/day_15.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/bin/day_15.rs b/src/bin/day_15.rs new file mode 100644 index 0000000..29b63ad --- /dev/null +++ b/src/bin/day_15.rs @@ -0,0 +1,40 @@ +extern crate advent_of_code_2017; +use advent_of_code_2017::*; + +fn main() { + let args = AdventArgs::init(); + + let mut a: u64 = 591; + let mut b: u64 = 393; + + let mut matches: u64 = 0; + + let comparisons = if args.part == 1 { + 40_000_000 + } else { + 5_000_000 + }; + + for _ in 0..comparisons { + a = (a * 16807) % 2147483647; + b = (b * 48271) % 2147483647; + + while args.part != 1 && a % 4 != 0 { + a = (a * 16807) % 2147483647; + } + while args.part != 1 && b % 8 != 0 { + b = (b * 48271) % 2147483647; + } + + if lower_16_match(a, b) { + matches += 1; + } + } + + println!("There were {} matches", matches); +} + +fn lower_16_match(a: u64, b: u64) -> bool { + let mask = 65535; //2^16-1, ie 16 ones + (a & mask) == (b & mask) +} |