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
71
72
73
|
#![allow(dead_code)]
#![allow(unused_variables)]
use advent_of_code::prelude::*;
const NUMBERS: [&str; 10] = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine",
];
pub fn parse(fh: File) -> Result<impl Iterator<Item = String>> {
Ok(parse::lines(fh))
}
pub fn part1(lines: impl Iterator<Item = String>) -> Result<i64> {
let mut total = 0;
for line in lines {
let first = line.chars().find(char::is_ascii_digit).unwrap();
let last = line.chars().rev().find(char::is_ascii_digit).unwrap();
let val: i64 = format!("{}{}", first, last).parse().unwrap();
total += val;
}
Ok(total)
}
pub fn part2(lines: impl Iterator<Item = String>) -> Result<i64> {
let mut total = 0;
for line in lines {
let mut first = 0;
let mut last = 0;
'c: for idx in 0..line.len() {
let c = line.chars().nth(idx).unwrap();
if char::is_numeric(c) {
first = i64::from(u32::from(c) - u32::from('0'));
break 'c;
}
for (i, n) in NUMBERS.iter().enumerate() {
if line[idx..].starts_with(n) {
first = i64::try_from(i).unwrap();
break 'c;
}
}
}
'c: for idx in (0..line.len()).rev() {
let c = line.chars().nth(idx).unwrap();
if char::is_numeric(c) {
last = i64::from(u32::from(c) - u32::from('0'));
break 'c;
}
for (i, n) in NUMBERS.iter().enumerate() {
if line[..=idx].ends_with(n) {
last = i64::try_from(i).unwrap();
break 'c;
}
}
}
let val: i64 = format!("{}{}", first, last).parse().unwrap();
total += val;
}
Ok(total)
}
#[test]
fn test() {
assert_eq!(
part1(parse(parse::data(2023, 1).unwrap()).unwrap()).unwrap(),
54927
);
assert_eq!(
part2(parse(parse::data(2023, 1).unwrap()).unwrap()).unwrap(),
54581
);
}
|