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.

Parsing numbers from a text file

+3
−0

This is my solution to the first task of the Advent of Code 2023.

The task description is:

On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.

For example:

1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.

Consider your entire calibration document. What is the sum of all of the calibration values?

Here is my solution, which provides the correct sum (at least I passed the task):

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

fn main() {
    let re = Regex::new(r"^[\D]*(\d)(|.*(\d))\D*$").unwrap();
    let mut sum: u32 = 0;

    for line in read_to_string("calibration_data.txt").unwrap().lines() {
        println!("{line}");
        let Some(caps) = re.captures(line) else {
            panic!("no match! in line {line}");
        };
        let first: &str = &caps.get(1).map_or("0", |m| m.as_str());
        let second: &str = &caps.get(3).map_or(first, |m| m.as_str());
        let mut compound: String = String::new();
        compound.push_str(first);
        compound.push_str(second);
        println!("{first} + {second} = {compound}");
        let compound: u32 = compound.parse().unwrap();
        sum += compound;
    }

    println!("***********************");
    println!("Calibration sum: {sum}");
    println!("***********************");
}

Is there any way I could have done this better, especially regarding the extraction of the numbers from the text lines?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Real problem size (3 comments)

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+0
−0

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 returns index, so better to call chars() first for functional-style.

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
    }
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−1

You don't need a regex for this. To find first you can simply iterate through the line until you find a digit. To find second you can do the same but in reverse. This is more efficient than running a regex.

For a problem as small as the example, it doesn't matter and arguably the regex adds readability. But I assume AoC makes you run this on a much bigger input as well.

You also don't need to construct the calibration number as a string. first*10 + second gives the calibration number.

When finding the digits, you can do a time-space tradeoff by creating a hashmap that maps each char 0-9 to the corresponding digit. Plugging the chars into this map is probably faster than parsing the string to an integer.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Not so sure about last paragraph. (3 comments)

Sign up to answer this question »