From c3e72963ed6c77c8c3bdcc2deb86f895b5221a9c Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sun, 24 Dec 2017 11:08:00 -0500 Subject: Day 15, part 2 --- 2017/src/bin/day15.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to '2017/src/bin') diff --git a/2017/src/bin/day15.rs b/2017/src/bin/day15.rs index dbf041a..9816c55 100644 --- a/2017/src/bin/day15.rs +++ b/2017/src/bin/day15.rs @@ -22,18 +22,62 @@ impl Iterator for GenB { } } -fn judge(a: u64, b: u64) -> usize { +struct GenA2(u64); + +impl Iterator for GenA2 { + type Item = u64; + + fn next(&mut self) -> Option { + loop { + self.0 = self.0.wrapping_mul(16807) % 2147483647; + if self.0 % 4 == 0 { + return Some(self.0); + } + } + } +} + +struct GenB2(u64); + +impl Iterator for GenB2 { + type Item = u64; + + fn next(&mut self) -> Option { + loop { + self.0 = self.0.wrapping_mul(48271) % 2147483647; + if self.0 % 8 == 0 { + return Some(self.0); + } + } + } +} + +fn judge1(a: u64, b: u64) -> usize { GenA(a).zip(GenB(b)) .take(40_000_000) .filter(|&(a, b)| a & 0xFFFF == b & 0xFFFF) .count() } +fn judge2(a: u64, b: u64) -> usize { + GenA2(a).zip(GenB2(b)) + .take(5_000_000) + .filter(|&(a, b)| a & 0xFFFF == b & 0xFFFF) + .count() +} + fn solve1(input: &str) -> usize { let mut iter = input.split_whitespace(); let a = iter.clone().nth(4).unwrap().parse().unwrap(); let b = iter.nth(9).unwrap().parse().unwrap(); - judge(a, b) + judge1(a, b) +} + +fn solve2(input: &str) -> usize { + let mut iter = input.split_whitespace(); + let a = iter.clone().nth(4).unwrap().parse().unwrap(); + let b = iter.nth(9).unwrap().parse().unwrap(); + judge2(a, b) } fn main() { @@ -41,9 +85,15 @@ fn main() { io::stdin().read_to_string(&mut input).unwrap(); println!("Part 1: {}", solve1(input.trim())); + println!("Part 2: {}", solve2(input.trim())); } #[test] fn part1() { - assert_eq!(588, judge(65, 8921)); + assert_eq!(588, judge1(65, 8921)); +} + +#[test] +fn part2() { + assert_eq!(309, judge2(65, 8921)); } -- cgit 1.4.1 Commit message (Expand)Author 2020-12-20Emit tags for section headings without trailing dotsJune McEnroe 2020-12-20Depend on rfc-index.txt.gz in MakefileJune McEnroe 2020-12-19Convert :RFC number to numberJune McEnroe 2020-12-19Move :RFC to plugin scriptJune McEnroe 2020-12-19Sort RFC tags fileJune McEnroe 2020-12-19Open rfc-index.txt for :RFC without argumentJune McEnroe 2020-12-19Include rfc-index.txt in RFC syncJune McEnroe 2020-12-18Add :RFC vim command to view IETF RFCsJune McEnroe 2020-12-18Add scripts to download, compress and tag IETF RFCsJune McEnroe 2020-12-18Fix bibsort name sorting for middle names, trailing titlesJune McEnroe 2020-12-15Add bibsortJune McEnroe 2020-12-08Add modem -r flag to set baud rateJune McEnroe 2020-12-07Clean up variable expansions in c.shJune McEnroe 2020-12-06Add I Feel It AllJune McEnroe 2020-12-05Handle const strings in c -eJune McEnroe 2020-12-05Disable mouse in htopJune McEnroe 2020-12-03Note lack of macro expansions in Q(...)June McEnroe 2020-12-02Fix missing "to" in "hot tips"June McEnroe 2020-12-02Publish "hot tips"June McEnroe 2020-11-26Publish "Inability"June McEnroe 2020-11-26Render content into atom feedJune McEnroe 2020-11-26Update plan with different things I won't doJune McEnroe 2020-11-19Add "Come On Petunia"June McEnroe 2020-11-13Add x4 to LESSJune McEnroe 2020-11-04Remove modified sensitivity settingsJune McEnroe 2020-10-29Remove editJune McEnroe 2020-10-27Switch gr alias back to git rebaseJune McEnroe 2020-10-27Allow cd host: to cd to same path over sshJune McEnroe 2020-10-27Use SendEnv for cd host:pathJune McEnroe 2020-10-27Allow cd host:path over sshJune McEnroe 2020-10-07Use mandoc -T utf8 for text.June McEnroe 2020-09-20Add The Awakened KingdomJune McEnroe 2020-09-12Move /opt/local back, cheat port select to use system manJune McEnroe 2020-09-12Move /opt/local behind /usr againJune McEnroe 2020-09-12Enable toc in cgit renderings of man pagesJune McEnroe 2020-09-11Install mandoc on macOSJune McEnroe 2020-09-11Rewrite install script yet againJune McEnroe 2020-09-11Remove NetBSD from install scriptJune McEnroe 2020-09-11Use MacPorts rather than pkgsrcJune McEnroe 2020-09-11Add debian VM name to sshJune McEnroe 2020-09-11Add influencer tweetJune McEnroe 2020-09-10Add The Kingdom of GodsJune McEnroe 2020-09-07Add SunglassesJune McEnroe 2020-09-06Add Between the BreathsJune McEnroe 2020-09-04Open /dev/tty in nudgeJune McEnroe 2020-09-04Add nudgeJune McEnroe 2020-09-03Build fbclock with -lzJune McEnroe 2020-08-29Add tweets from retweetsJune McEnroe