#![no_std]
use core::fmt::Write;
use common::{print, read};
fn main() {
let buf = [0u8; 65000];
read(buf.as_ptr(), buf.len());
let mut input = str::from_utf8(&buf).unwrap();
input = input.trim_end_matches(char::MIN);
input = input.trim();
let result_one = one(input);
let result_two = two(input);
print!(64, "one: {}\ntwo: {}\n", result_one, result_two);
}
fn one(input: &str) -> usize {
input
.split("\n")
.map(|bank| {
let mut first = char::MIN;
let mut first_index = 0;
let mut second = char::MIN;
let mut second_index = 0;
for (i, c) in bank.chars().enumerate() {
if c > first && i != bank.len() - 1 {
first = c;
first_index = i;
second = char::MIN;
second_index = 0;
} else if c > second && i > first_index {
second = c;
second_index = i;
}
}
let a = &bank[first_index..first_index + 1];
let b = &bank[second_index..second_index + 1];
let mut buf = [0; 2];
let c = common::format_str(&mut buf, format_args!("{}{}", a, b));
c.parse::<usize>().unwrap()
})
.sum::<usize>()
}
#[cfg(test)]
mod test {
use super::*;
const EXAMPLE_INPUT: &str = r#"987654321111111
811111111111119
234234234234278
818181911112111"#;
#[test]
fn example() {
assert_eq!(one(EXAMPLE_INPUT), 357);
assert_eq!(two(EXAMPLE_INPUT), 3121910778619);
}
}