Re: [閒聊] Rust聖誕解題馬拉松

作者: yam276 ('_')   2023-12-05 11:58:20
※ 引述《yam276 (史萊哲林的優等生)》之銘言:
: https://github.com/rust-tw/advent-of-code
: 每天兩題 要自己處理IO
Day1-1
use std::fs::File;
use std::io::{self, BufRead, Write};
use std::path::Path;
struct Solution;
impl Solution {
pub fn process_line(line: &str) -> String {
let first_digit = line.chars().
find(|c| c.is_digit(10)).unwrap_or('0');
let last_digit = line.chars().rev().
find(|c| c.is_digit(10)).unwrap_or('0');
format!("{}{}", first_digit, last_digit)
}
}
fn main() -> io::Result<()> {
let path = "input.txt";
let output_path = "Solution.txt";
let input = File::open(path)?;
let buffered = io::BufReader::new(input);
let mut output = File::create(output_path)?;
let mut result_sum = 0;
for line in buffered.lines() {
let line = line?;
let result = Solution::process_line(&line);
result_sum += result.parse::<i32>().unwrap();
}
writeln!(output, "{}", result_sum)?;
Ok(())
}
處理成leetcode形式大概長這樣
Repo說後面可能會用到前面的架構
走一步是一步

Links booklink

Contact Us: admin [ a t ] ucptt.com