Skip to content

Commit 73cc05a

Browse files
committed
Day 3
1 parent 3ebbd2a commit 73cc05a

File tree

7 files changed

+106
-7
lines changed

7 files changed

+106
-7
lines changed

.devcontainer/devcontainer.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

Cargo.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ heapless = "0.8.0"
2828
itertools = "0.13.0"
2929
pico-args = "0.5.0"
3030
rayon = "1.10.0"
31+
regex = "1.11.1"
3132
tinyjson = "2.5.1"
3233

3334
# Solution dependencies

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1313
| :---: | :---: | :---: |
1414
| [Day 1](./src/bin/01.rs) | `66.9µs` | `82.5µs` |
1515
| [Day 2](./src/bin/02.rs) | `128.2µs` | `224.5µs` |
16+
| [Day 3](./src/bin/03.rs) | `688.6µs` | `810.4µs` |
1617

17-
**Total: 0.50ms**
18+
**Total: 2.00ms**
1819
<!--- benchmarking table --->
1920

2021
---

data/examples/03-1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

data/examples/03-2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

src/bin/03.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
advent_of_code::solution!(3);
2+
3+
use regex::Regex;
4+
5+
pub fn part_one(input: &str) -> Option<u32> {
6+
let regex = Regex::new(r"mul\((?P<x>\d{1,3}),(?P<y>\d{1,3})\)").unwrap();
7+
let sum = regex
8+
.captures_iter(input)
9+
.map(|captures| {
10+
let x = &captures["x"].parse::<u32>().unwrap();
11+
let y = &captures["y"].parse::<u32>().unwrap();
12+
x * y
13+
})
14+
.sum();
15+
Some(sum)
16+
}
17+
18+
pub fn part_two(input: &str) -> Option<u32> {
19+
let regex =
20+
Regex::new(r"((?P<op>do\(\)|don't\(\))|(mul\((?P<x>\d{1,3}),(?P<y>\d{1,3})\)))").unwrap();
21+
let (_, sum) = regex
22+
.captures_iter(input)
23+
.fold((true, 0), |(on, sum), captures| {
24+
if let Some(op) = captures.name("op") {
25+
if op.as_str() == "don't()" {
26+
(false, sum)
27+
} else if op.as_str() == "do()" {
28+
(true, sum)
29+
} else {
30+
panic!("Unknown op: {}", op.as_str());
31+
}
32+
} else if on {
33+
let x = &captures["x"].parse::<u32>().unwrap();
34+
let y = &captures["y"].parse::<u32>().unwrap();
35+
(true, sum + x * y)
36+
} else {
37+
(false, sum)
38+
}
39+
});
40+
Some(sum)
41+
}
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
47+
#[test]
48+
fn test_part_one() {
49+
let result = part_one(&advent_of_code::template::read_file_part(
50+
"examples", DAY, 1,
51+
));
52+
assert_eq!(result, Some(161));
53+
}
54+
55+
#[test]
56+
fn test_part_two() {
57+
let result = part_two(&advent_of_code::template::read_file_part(
58+
"examples", DAY, 2,
59+
));
60+
assert_eq!(result, Some(48));
61+
}
62+
}

0 commit comments

Comments
 (0)