diff options
| author | Jonas Berlin <xkr47@outerspace.dyndns.org> | 2020-02-21 22:27:49 +0200 |
|---|---|---|
| committer | Jonas Berlin <xkr47@outerspace.dyndns.org> | 2020-02-21 22:29:59 +0200 |
| commit | 1cff3f97affac1bfa931453b6225edab47a4c206 (patch) | |
| tree | 2595a531decee84c649a464dd717d0e70571bbfc /challenge-048 | |
| parent | 5538168ffe1104ff3b90a97f0ba6b66eb2af8cd8 (diff) | |
| download | perlweeklychallenge-club-1cff3f97affac1bfa931453b6225edab47a4c206.tar.gz perlweeklychallenge-club-1cff3f97affac1bfa931453b6225edab47a4c206.tar.bz2 perlweeklychallenge-club-1cff3f97affac1bfa931453b6225edab47a4c206.zip | |
pwc048 / Rust version!
Diffstat (limited to 'challenge-048')
| -rw-r--r-- | challenge-048/xkr47/rust/Cargo.toml | 16 | ||||
| -rw-r--r-- | challenge-048/xkr47/rust/ch-1.rs | 27 | ||||
| -rw-r--r-- | challenge-048/xkr47/rust/ch-2.rs | 18 |
3 files changed, 61 insertions, 0 deletions
diff --git a/challenge-048/xkr47/rust/Cargo.toml b/challenge-048/xkr47/rust/Cargo.toml new file mode 100644 index 0000000000..297b1f0cd8 --- /dev/null +++ b/challenge-048/xkr47/rust/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "pwc-048" +version = "0.1.0" +authors = ["Jonas Berlin <xkr47@outerspace.dyndns.org>"] +edition = "2018" + +[dependencies] +chrono = "0.4.10" + +[[bin]] +name = "ch-1" +path = "ch-1.rs" + +[[bin]] +name = "ch-2" +path = "ch-2.rs" diff --git a/challenge-048/xkr47/rust/ch-1.rs b/challenge-048/xkr47/rust/ch-1.rs new file mode 100644 index 0000000000..f64641edef --- /dev/null +++ b/challenge-048/xkr47/rust/ch-1.rs @@ -0,0 +1,27 @@ +struct Pwc048_1 { + array: Vec<i32>, + index: usize, +} + +impl Iterator for Pwc048_1 { + type Item = i32; + fn next(&mut self) -> Option<i32> { + let val = self.array[self.index]; + self.index = (self.index + 1) % self.array.len(); + self.array.remove(self.index); + if !self.array.is_empty() { + self.index = self.index % self.array.len(); + Some(val) + } else { + None + } + } +} + +fn main() { + let arr = Pwc048_1 { + array: (1..=50).collect(), + index: 0, + }; + print!("Survivors, in order: {:?} ", arr.collect::<Vec<i32>>()); +} diff --git a/challenge-048/xkr47/rust/ch-2.rs b/challenge-048/xkr47/rust/ch-2.rs new file mode 100644 index 0000000000..9997cc1cb9 --- /dev/null +++ b/challenge-048/xkr47/rust/ch-2.rs @@ -0,0 +1,18 @@ +use chrono::prelude::*; +use std::iter::FromIterator; + +fn main() { + let dates: Vec<String> = (2000..=2999) + .map(|y| { + let ys = y.to_string(); + let (m, d): (Vec<(usize, char)>, Vec<(usize, char)>) = ys.chars().rev().enumerate().partition(|(i, _)| *i < 2usize); + let m = String::from_iter(m.iter().map(|(_, val)| val)).parse::<u32>().unwrap(); + let d = String::from_iter(d.iter().map(|(_, val)| val)).parse::<u32>().unwrap(); + chrono::Utc.ymd_opt(y, m, d) + }) + .flat_map(|date| date.earliest()) + .map(|date| format!("{}", date.format("%m%d%y"))) + .collect(); + + println!("{:?}", dates); +} |
