Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use regex::Regex;
- use std::collections::HashSet;
- use std::io::{self, BufRead};
- fn main() {
- let re_tokens = Regex::new(r"([0-9]+)|([^0-9.]+)").unwrap();
- let stdin = io::stdin();
- let mut res: i32 = 0;
- let mut number_positions: Vec<(i32, (i32, i32))> = Vec::new();
- let mut position_parts: HashSet<(i32, i32)> = HashSet::new();
- for (line_no, line) in stdin.lock().lines().enumerate() {
- let haystack = &line.unwrap();
- let tokens = re_tokens.find_iter(haystack);
- for token in tokens {
- // println!("{}", token.as_str());
- match token.as_str().parse::<i32>() {
- Ok(ok) => {
- number_positions.push((
- ok,
- (
- line_no.try_into().unwrap(),
- token.start().try_into().unwrap(),
- ),
- ));
- }
- Err(_e) => {
- let _ = position_parts.insert((
- line_no.try_into().unwrap(),
- token.start().try_into().unwrap(),
- ));
- }
- }
- }
- }
- for (value, (row, col)) in number_positions {
- let len: i32 = value.to_string().len().try_into().unwrap();
- for i in col - 1..col + len + 1 {
- if position_parts.contains(&(row - 1, i))
- || position_parts.contains(&(row, i))
- || position_parts.contains(&(row + 1, i))
- {
- res += value;
- break;
- }
- }
- }
- print!("{}", res);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement