-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.rs
147 lines (122 loc) · 3.6 KB
/
day3.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! [Day 3: Gear Ratios](https://adventofcode.com/2023/day/3)
use std::collections::HashMap;
use std::time::{Duration, Instant};
struct Puzzle {
sx: i32,
sy: i32,
grid: Vec<String>,
sum_parts: u64,
gears: HashMap<[i32; 2], Vec<u64>>,
}
impl Puzzle {
fn new() -> Self {
Self {
sx: 0,
sy: 0,
grid: vec![],
sum_parts: 0,
gears: HashMap::new(),
}
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
self.grid = data.lines().map(String::from).collect::<Vec<_>>();
self.sx = self.grid[0].len() as i32;
self.sy = self.grid.len() as i32;
}
/// Access the engine schematic
fn g(&self, x: i32, y: i32) -> char {
if 0 <= x && x < self.sx && 0 <= y && y < self.sy {
self.grid[y as usize].chars().nth(x as usize).unwrap()
} else {
'.'
}
}
/// Read the schematic to find part numbers and gears
fn parse(&mut self) {
for y in 0..self.sy {
let mut x = 0;
while x < self.sx {
let mut symbol = false;
let mut gear = [0, 0];
let mut n = 0;
while let Some(d) = self.g(x, y).to_digit(10) {
n = n * 10 + u64::from(d);
for (ix, iy) in [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
] {
let c = self.g(x + ix, y + iy);
if c != '.' && !c.is_ascii_digit() {
symbol = true;
if c == '*' {
// assert we have only one gear near a part number
assert!(!(gear != [0, 0] && gear != [x + ix, y + iy]),);
gear = [x + ix, y + iy];
}
}
}
x += 1;
}
if symbol {
self.sum_parts += n;
}
if gear != [0, 0] {
self.gears.entry(gear).or_default().push(n);
}
x += 1;
}
}
}
/// Solve part one.
const fn part1(&self) -> u64 {
self.sum_parts
}
/// Solve part two.
fn part2(&self) -> u64 {
let mut gear_ratios = 0;
for parts in self.gears.values() {
if parts.len() == 2 {
gear_ratios += parts[0] * parts[1];
}
}
gear_ratios
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
let start = Instant::now();
puzzle.configure(args.path.as_str());
puzzle.parse();
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
let duration: Duration = start.elapsed();
eprintln!("Time elapsed: {duration:?}");
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
puzzle.parse();
assert_eq!(puzzle.part1(), 4361);
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
puzzle.parse();
assert_eq!(puzzle.part2(), 467835);
}
}