2023-01: Working part 1
This commit is contained in:
parent
68e6963aa9
commit
d6382ce487
|
@ -0,0 +1,14 @@
|
|||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
|
@ -1,3 +1,6 @@
|
|||
mod part_one;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
part_one::part_one();
|
||||
//part_two();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
// https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
|
||||
|
||||
pub fn part_one() {
|
||||
if let Ok(lines) = read_lines("./input.txt") {
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
for line in lines {
|
||||
result += parse_line(&line.unwrap());
|
||||
}
|
||||
|
||||
println!("{}", result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
|
||||
fn parse_line(s: &str) -> u32{
|
||||
|
||||
const RADIX: u32 = 10;
|
||||
|
||||
let mut first_digit = 0;
|
||||
let mut last_digit = 0;
|
||||
|
||||
for c in s.chars() {
|
||||
if c.is_digit(RADIX) {
|
||||
|
||||
let digit = c.to_digit(RADIX).unwrap();
|
||||
|
||||
if first_digit == 0 {
|
||||
first_digit = digit;
|
||||
}
|
||||
last_digit = digit;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return first_digit * 10 + last_digit
|
||||
}
|
Loading…
Reference in New Issue