Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Code Reviews Parsing numbers from a text file

using rfind and find as str.chars().find(|c| "0123456789".contains(c)) and str.chars().find(|c| "0123456789".contains(c)), then a manual match block is the simplest solution I think: edit str.find...

posted 2mo ago by Feefladder‭  ·  edited 2mo ago by Feefladder‭

Answer
#3: Post edited by user avatar Feefladder‭ · 2024-10-25T17:13:19Z (about 2 months ago)
  • using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `&str.find(|c| "0123456789".contains(c))` and `&str.find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:
  • **edit** str.find returns index, so better to call `chars()` first for functional-style.
  • ```rust
  • fn char_to_int(c: char) -> u32 {
  • match c {
  • '0' => 0,
  • '1' => 1,
  • '2' => 2,
  • '3' => 3,
  • '4' => 4,
  • '5' => 5,
  • '6' => 6,
  • '7' => 7,
  • '8' => 8,
  • '9' => 9,
  • _ => u32::MAX, // make summing logic panic! for pleasure on invalid
  • }
  • }
  • #[test]
  • fn test_advent_2023() {
  • let data = [
  • "1abc2",
  • "pqr3stu8vwx",
  • "a1b2c3d4e5f",
  • "treb7uchet",
  • ];
  • let mut sum: u32 = 0;
  • for line in data {
  • let first: u32 = line.chars().find(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
  • let second: u32 = line.chars().rfind(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
  • sum += first*10 + second;
  • }
  • println!("{sum:?}");
  • assert_eq!(sum, 42) // make the test fail for output
  • }
  • ```
  • using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `str.chars().find(|c| "0123456789".contains(c))` and `str.chars().find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:
  • **edit** str.find returns index, so better to call `chars()` first for functional-style.
  • ```rust
  • fn char_to_int(c: char) -> u32 {
  • match c {
  • '0' => 0,
  • '1' => 1,
  • '2' => 2,
  • '3' => 3,
  • '4' => 4,
  • '5' => 5,
  • '6' => 6,
  • '7' => 7,
  • '8' => 8,
  • '9' => 9,
  • _ => u32::MAX, // make summing logic panic! for pleasure on invalid
  • }
  • }
  • #[test]
  • fn test_advent_2023() {
  • let data = [
  • "1abc2",
  • "pqr3stu8vwx",
  • "a1b2c3d4e5f",
  • "treb7uchet",
  • ];
  • let mut sum: u32 = 0;
  • for line in data {
  • let first: u32 = line.chars().find(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
  • let second: u32 = line.chars().rfind(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
  • sum += first*10 + second;
  • }
  • println!("{sum:?}");
  • assert_eq!(sum, 42) // make the test fail for output
  • }
  • ```
#2: Post edited by user avatar Feefladder‭ · 2024-10-25T17:00:28Z (about 2 months ago)
use `chars()` iterator.
  • using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `&str.find(|c| "0123456789".contains(c))` and `&str.find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:
  • ```rust
  • use std::fs::read_to_string;
  • use regex::Regex;
  • fn char_to_int<T>(c: char) -> T {
  • match c {
  • '0' => 0,
  • '1' => 1,
  • '2' => 2,
  • '3' => 3,
  • '4' => 4,
  • '5' => 5,
  • '6' => 6,
  • '7' => 7,
  • '8' => 8,
  • '9' => 9,
  • _ => T::MAX, // make summing logic panic! for pleasure on invalid
  • }
  • }
  • fn main() {
  • let mut sum: u32 = 0;
  • for line in read_to_string("calibration_data.txt").unwrap().lines() {
  • let first: u32 = line.find(|c| "0123456789".contains(c)).map(char_to_int);
  • let second: u32 = line.rfind(|c| "0123456789".contains(c)).map(char_to_int);
  • sum += first*10 + second;
  • }
  • println!("{sum:?}");
  • ```
  • using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `&str.find(|c| "0123456789".contains(c))` and `&str.find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:
  • **edit** str.find returns index, so better to call `chars()` first for functional-style.
  • ```rust
  • fn char_to_int(c: char) -> u32 {
  • match c {
  • '0' => 0,
  • '1' => 1,
  • '2' => 2,
  • '3' => 3,
  • '4' => 4,
  • '5' => 5,
  • '6' => 6,
  • '7' => 7,
  • '8' => 8,
  • '9' => 9,
  • _ => u32::MAX, // make summing logic panic! for pleasure on invalid
  • }
  • }
  • #[test]
  • fn test_advent_2023() {
  • let data = [
  • "1abc2",
  • "pqr3stu8vwx",
  • "a1b2c3d4e5f",
  • "treb7uchet",
  • ];
  • let mut sum: u32 = 0;
  • for line in data {
  • let first: u32 = line.chars().find(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
  • let second: u32 = line.chars().rfind(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
  • sum += first*10 + second;
  • }
  • println!("{sum:?}");
  • assert_eq!(sum, 42) // make the test fail for output
  • }
  • ```
#1: Initial revision by user avatar Feefladder‭ · 2024-10-25T16:48:31Z (about 2 months ago)
using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `&str.find(|c| "0123456789".contains(c))` and `&str.find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:

```rust
use std::fs::read_to_string;
use regex::Regex;

fn char_to_int<T>(c: char) -> T {
  match c {
    '0' => 0,
    '1' => 1,
    '2' => 2,
    '3' => 3,
    '4' => 4,
    '5' => 5,
    '6' => 6,
    '7' => 7,
    '8' => 8,
    '9' => 9,
     _  => T::MAX,  // make summing logic panic! for pleasure on invalid
  }
}

fn main() {
  let mut sum: u32 = 0;

  for line in read_to_string("calibration_data.txt").unwrap().lines() {
    let first: u32 = line.find(|c| "0123456789".contains(c)).map(char_to_int);
    let second: u32 = line.rfind(|c| "0123456789".contains(c)).map(char_to_int);
    sum += first*10 + second;
  }
  println!("{sum:?}");
```