-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.rs
112 lines (99 loc) · 2.68 KB
/
day1.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! [Day 1: Trebuchet?!](https://adventofcode.com/2023/day/1)
struct Puzzle {
data: String,
}
impl Puzzle {
fn new() -> Puzzle {
Puzzle {
data: String::new(),
}
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
self.data = data;
}
/// Solve part one.
fn part1(&self) -> u32 {
let mut sum = 0;
for line in self.data.split_terminator('\n') {
let digits = line
.chars()
.filter_map(|d| d.to_digit(10))
.collect::<Vec<_>>();
sum += digits.first().unwrap() * 10 + digits.last().unwrap();
}
sum
}
/// Return the value of the digit at the beginning of s or None
fn valid_digit(s: &str) -> Option<u32> {
let d = s.chars().nth(0).unwrap();
let d = d.to_digit(10);
if d.is_some() {
d
} else if s.starts_with("one") {
Some(1)
} else if s.starts_with("two") {
Some(2)
} else if s.starts_with("three") {
Some(3)
} else if s.starts_with("four") {
Some(4)
} else if s.starts_with("five") {
Some(5)
} else if s.starts_with("six") {
Some(6)
} else if s.starts_with("seven") {
Some(7)
} else if s.starts_with("eight") {
Some(8)
} else if s.starts_with("nine") {
Some(9)
} else {
None
}
}
/// Solve part two.
fn part2(&self) -> u32 {
let mut sum = 0;
for line in self.data.split_terminator('\n') {
for i in 0..line.len() {
if let Some(first) = Self::valid_digit(&line[i..]) {
sum += first * 10;
break;
}
}
for i in (0..line.len()).rev() {
if let Some(last) = Self::valid_digit(&line[i..]) {
sum += last;
break;
}
}
}
sum
}
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("test1.txt");
assert_eq!(puzzle.part1(), 142);
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure("test2.txt");
assert_eq!(puzzle.part2(), 281);
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(args.path.as_str());
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}