-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
70 lines (62 loc) · 1.84 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::fs::File;
use std::io::prelude::*;
use std::error::Error;
pub fn part1() -> Result<i64, Box<dyn Error>>
{
let mut file = File::open("src/day1/input.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let mut total = 0;
for line in contents.lines()
{
let digit1 = line.chars().find(|c| c.is_digit(10)).unwrap();
let digit2 = line.chars().rev().find(|c| c.is_digit(10)).unwrap();
total += format!("{}{}", digit1, digit2).parse::<i32>()?;
}
return Ok(total as i64);
}
pub fn part2() -> Result<i64, Box<dyn Error>>
{
let mut file = File::open("src/day1/input.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let mut total = 0;
let digits = [
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"1", "2", "3", "4", "5", "6", "7", "8", "9"
];
for line in contents.lines()
{
// first indexes of each digit
let pattern_i = digits
.iter()
.enumerate()
.filter_map(|(i, &p)| line.find(p).map(|mi| (p, mi, i)));
// minimum index pattern
let mins = pattern_i
.min_by_key(|&(_, mi, _)| mi)
.unwrap();
// last indexes of each digit
let pattern_i = digits
.iter()
.enumerate()
.filter_map(|(i, &p)| line.rfind(p).map(|mi| (p, mi, i)));
// maximum index pattern
let maxs = pattern_i
.max_by_key(|&(_, mi, _)| mi)
.unwrap();
total += ((((mins.2 % 9) + 1) * 10) + ((maxs.2 % 9) + 1)) as i32;
}
return Ok(total as i64);
}
#[cfg(test)]
mod tests
{
use super::*;
#[test]
fn test_parts()
{
assert_eq!(part1().ok(), Some(253));
assert_eq!(part2().ok(), Some(281));
}
}