Skip to content

Commit 8ccaf86

Browse files
committed
Year 2024 Day 3
1 parent 247b104 commit 8ccaf86

File tree

7 files changed

+100
-1
lines changed

7 files changed

+100
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
7979
| --- | --- | --- | --: |
8080
| 1 | [Historian Hysteria](https://adventofcode.com/2024/day/1) | [Source](src/year2024/day01.rs) | 21 |
8181
| 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [Source](src/year2024/day02.rs) | 43 |
82+
| 3 | [Mull It Over](https://adventofcode.com/2024/day/3) | [Source](src/year2024/day03.rs) | 13 |
8283

8384
## 2023
8485

benches/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,5 @@ mod year2023 {
294294
mod year2024 {
295295
benchmark!(year2024, day01);
296296
benchmark!(year2024, day02);
297+
benchmark!(year2024, day03);
297298
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,5 @@ pub mod year2023 {
293293
pub mod year2024 {
294294
pub mod day01;
295295
pub mod day02;
296+
pub mod day03;
296297
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,5 +360,5 @@ fn year2023() -> Vec<Solution> {
360360
}
361361

362362
fn year2024() -> Vec<Solution> {
363-
vec![solution!(year2024, day01), solution!(year2024, day02)]
363+
vec![solution!(year2024, day01), solution!(year2024, day02), solution!(year2024, day03)]
364364
}

src/year2024/day03.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
type Input = (u32, u32);
2+
3+
pub fn parse(input: &str) -> Input {
4+
let memory = input.as_bytes();
5+
let mut index = 0;
6+
let mut enabled = true;
7+
let mut part_one = 0;
8+
let mut part_two = 0;
9+
10+
while index < memory.len() {
11+
// Prefixes
12+
if memory[index..].starts_with(b"mul(") {
13+
index += 4;
14+
} else if memory[index..].starts_with(b"do()") {
15+
index += 4;
16+
enabled = true;
17+
continue;
18+
} else if memory[index..].starts_with(b"don't()") {
19+
index += 7;
20+
enabled = false;
21+
continue;
22+
} else {
23+
index += 1;
24+
continue;
25+
}
26+
27+
// First numnber
28+
let mut first = 0;
29+
let mut digits = 0;
30+
31+
while memory[index].is_ascii_digit() {
32+
first = 10 * first + (memory[index] - b'0') as u32;
33+
digits += 1;
34+
index += 1;
35+
}
36+
37+
// Middle
38+
if digits == 0 || memory[index] != b',' {
39+
continue;
40+
}
41+
index += 1;
42+
43+
// Second
44+
let mut second = 0;
45+
let mut digits = 0;
46+
47+
while memory[index].is_ascii_digit() {
48+
second = 10 * second + (memory[index] - b'0') as u32;
49+
digits += 1;
50+
index += 1;
51+
}
52+
53+
// Suffix
54+
if digits == 0 || memory[index] != b')' {
55+
continue;
56+
}
57+
index += 1;
58+
59+
// Multiply
60+
let product = first * second;
61+
part_one += product;
62+
if enabled {
63+
part_two += product;
64+
}
65+
}
66+
67+
(part_one, part_two)
68+
}
69+
70+
pub fn part1(input: &Input) -> u32 {
71+
input.0
72+
}
73+
74+
pub fn part2(input: &Input) -> u32 {
75+
input.1
76+
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,5 @@ mod year2023 {
283283
mod year2024 {
284284
mod day01_test;
285285
mod day02_test;
286+
mod day03_test;
286287
}

tests/year2024/day03_test.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use aoc::year2024::day03::*;
2+
3+
const FIRST_EXAMPLE: &str = "\
4+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
5+
6+
const SECOND_EXAMPLE: &str = "\
7+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
8+
9+
#[test]
10+
fn part1_test() {
11+
let input = parse(FIRST_EXAMPLE);
12+
assert_eq!(part1(&input), 161);
13+
}
14+
15+
#[test]
16+
fn part2_test() {
17+
let input = parse(SECOND_EXAMPLE);
18+
assert_eq!(part2(&input), 48);
19+
}

0 commit comments

Comments
 (0)