Skip to content

Commit 90aafc4

Browse files
committed
Added space missions solution. Added sandbox crate.
1 parent 77bccec commit 90aafc4

File tree

9 files changed

+105142
-1
lines changed

9 files changed

+105142
-1
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[workspace]
2+
resolver = "2"
3+
members = ["sandbox"]
4+
15
[package]
26
name = "rust-challenges"
37
version = "0.1.0"

sandbox/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "sandbox"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
rust-challenges = { path = "../" }

sandbox/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rust Challenges Sandbox
2+
3+
Most of the rust-challenges code base is ran off of test cases and this fits most small easily testable solutions. But sometimes it's necessary to run solutions against large chunks of data with customizable output. The sandbox is perfect place to keep large datasets and execute solutions against those datasets with customizable output.

sandbox/data/space_missions.log

Lines changed: 105032 additions & 0 deletions
Large diffs are not rendered by default.

sandbox/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::{fs::File, io::BufReader};
2+
3+
use rust_challenges::other::space_missions::longest_mars_mission;
4+
5+
fn main() {
6+
let f = File::open("data/space_missions.log").expect("Could not open log file");
7+
let reader = BufReader::new(f);
8+
let (duration, code) = longest_mars_mission(reader);
9+
println!("Mission Duration (days): {duration}");
10+
println!("Security Code: {code}");
11+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod codewars;
22
mod dailyprogrammer;
33
mod leetcode;
4-
mod other;
4+
pub mod other;
55
mod util;

src/other/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
mod anagram;
44
mod readability;
5+
pub mod space_missions;

src/other/space_missions.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::io::BufRead;
2+
3+
pub fn longest_mars_mission<R>(reader: R) -> (u16, String)
4+
where
5+
R: BufRead,
6+
{
7+
let mut result = (0, "Unknown".to_string());
8+
for (idx, line) in reader.lines().enumerate() {
9+
let line = line.unwrap_or_else(|_| panic!("Could not read line: {idx}"));
10+
if line.starts_with('#') {
11+
continue;
12+
}
13+
14+
// When the line is able to be split into 8 exact pieces we know we're
15+
// currently consuming a mission record line.
16+
let pieces: Vec<_> = line.split("|").map(str::trim).collect();
17+
if pieces.len() == 8 && pieces[2] == "Mars" {
18+
let duration = pieces[5]
19+
.parse::<u16>()
20+
.unwrap_or_else(|_| panic!("Could not parse duration on line: {idx}"));
21+
if duration > result.0 {
22+
result = (duration, pieces[7].to_string());
23+
}
24+
}
25+
}
26+
27+
result
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::*;
33+
use std::io::Cursor;
34+
35+
#[test]
36+
fn ignores_comments() {
37+
assert_eq!(
38+
(0, "Unknown".to_string()),
39+
longest_mars_mission(Cursor::new(
40+
"# hello\n# more comments\n# 2048-09-07| WXI-0590|Mars | In Progress | 6 | 380 | 69.57 | ALP-780-GBT "
41+
))
42+
)
43+
}
44+
45+
#[test]
46+
fn recognizes_values_with_leading_and_trailing_whitespace() {
47+
assert_eq!(
48+
(380, "ALP-780-GBT".to_string()),
49+
longest_mars_mission(Cursor::new(
50+
"2048-09-07| WXI-0590| Mars | In Progress | 6 | 380 | 69.57 | ALP-780-GBT "
51+
))
52+
)
53+
}
54+
55+
#[test]
56+
fn longest_mars_mission_is_captured() {
57+
assert_eq!(
58+
(420, "KLL-001-TND".to_string()),
59+
longest_mars_mission(Cursor::new(
60+
"2048-09-07| WXI-0590| Mars | In Progress | 6 | 380 | 69.57 | ALP-780-GBT\n
61+
2041-09-13 | EYO-5723 | Moon| Partial Success |6 | 5000 |48.53 | HRV-950-OIS\n
62+
2041-09-13 | EYO-5723 | Mars| Partial Success | 6 | 420 |48.53 | KLL-001-TND"
63+
))
64+
)
65+
}
66+
67+
#[test]
68+
fn panics_at_invalid_duration() {
69+
let res = std::panic::catch_unwind(|| {
70+
longest_mars_mission(Cursor::new(
71+
"2048-09-07| WXI-0590| Mars | In Progress | 6 | abc | 69.57 | ALP-780-GBT",
72+
))
73+
});
74+
assert!(res.is_err());
75+
}
76+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy