[2017][ru(by|st)][15.x] speedrun

sorbet
Alpha Chen 7 years ago
parent 857b9c34be
commit fd07f77699

@ -0,0 +1,3 @@
Generator A starts with 679
Generator B starts with 771

@ -0,0 +1,28 @@
start_a, start_b = ARGF.read.strip.scan(/\d+/).map(&:to_i)
class Generator
def initialize(start, factor)
@value = start
@factor = factor
end
def each
return enum_for(__method__) unless block_given?
loop do
@value *= @factor
@value %= 2147483647
yield @value
end
end
end
# start_a, start_b = 65, 8921
a = Generator.new(start_a, 16807).each.lazy
b = Generator.new(start_b, 48271).each.lazy
p a.zip(b).take(40_000_000).with_index.count {|(a,b),i|
p i if i % 1_000_000 == 0
# a.to_s(2)[-16,16] == b.to_s(2)[-16,16]
a & 0b1111111111111111 == b & 0b1111111111111111
}

@ -14,12 +14,41 @@ mod day_03;
mod day_04; mod day_04;
mod day_05; mod day_05;
fn main() { struct Generator {
if let Err(e) = run() { value: usize,
eprintln!("{}", e); factor: usize,
}
impl Iterator for Generator {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
self.value *= self.factor;
self.value %= 2147483647;
Some(self.value)
} }
} }
fn main() {
let mut a = Generator{value: 679, factor: 16807};
let mut b = Generator{value: 771, factor: 48271};
let answer = a.filter(|a| a % 4 == 0).zip(b.filter(|b| b % 8 == 0))
.take(5_000_000)
.filter(|&(a,b)| a & 0b1111111111111111 == b & 0b1111111111111111)
.count();
println!("{}", answer);
// p a.zip(b).take(40_000_000).with_index.count {|(a,b),i|
// p i if i % 1_000_000 == 0
// a.to_s(2)[-16,16] == b.to_s(2)[-16,16]
// }
// if let Err(e) = run() {
// eprintln!("{}", e);
// }
}
fn run() -> Result<(), Error> { fn run() -> Result<(), Error> {
let mut input = String::new(); let mut input = String::new();
io::stdin().read_to_string(&mut input)?; io::stdin().read_to_string(&mut input)?;

Loading…
Cancel
Save