Skip to content

Commit 9d29787

Browse files
committed
day3
1 parent b458a37 commit 9d29787

File tree

9 files changed

+178
-13
lines changed

9 files changed

+178
-13
lines changed

2024/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Advent of Code in Rust 🦀
22

33
![AoC2024](https://img.shields.io/badge/Advent_of_Code-2024-8A2BE2)
4-
![Stars: 4](https://img.shields.io/badge/Stars-4⭐-blue)
5-
![Rust: 2](https://img.shields.io/badge/Rust-2-cyan?logo=Rust)
6-
![Python: 2](https://img.shields.io/badge/Python-2-cyan?logo=Python)
4+
![Stars: 6](https://img.shields.io/badge/Stars-6⭐-blue)
5+
![Rust: 3](https://img.shields.io/badge/Rust-3-cyan?logo=Rust)
6+
![Python: 3](https://img.shields.io/badge/Python-3-cyan?logo=Python)
77

8-
## 2024 ([Calendar](https://adventofcode.com/2024)) ([Solutions](../2024/)) : 4
8+
## 2024 ([Calendar](https://adventofcode.com/2024)) ([Solutions](../2024/)) : 6
99

1010
Puzzle | Stars | Languages
1111
---------------------------------------------------------------- | ----- | -----------
1212
[Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1) | ⭐⭐ | [Rust](../2024/day1/day1.rs) [Python](../2024/day1/day1.py) [C](../2024/day1/day1.c) [Go](../2024/day1/day1.go) [Ruby](../2024/day1/day1.rb) [JS](../2024/day1/day1.js) [Bash](../2024/day1/day1.sh) [Lua](../2024/day1/day1.lua) [C#](../2024/day1/day1.cs)
1313
[Day 2: Red-Nosed Reports](https://adventofcode.com/2024/day/2) | ⭐⭐ | [Rust](../2024/day2/day2.rs) [Python](../2024/day2/day2.py)
14+
[Day 3: Mull It Over](https://adventofcode.com/2024/day/3) | ⭐⭐ | [Rust](../2024/day3/day3.rs) [Python](../2024/day3/day3.py)

2024/day3/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "day3"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../../aoc" }
8+
regex = "*"
9+
10+
[[bin]]
11+
name = "day3"
12+
path = "day3.rs"

2024/day3/day3.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
# [Day 3: Mull It Over](https://adventofcode.com/2024/day/3)
3+
4+
from pathlib import Path
5+
import sys, re
6+
import re
7+
8+
9+
if verbose := "-v" in sys.argv:
10+
sys.argv.remove("-v")
11+
if self_tests := "-T" in sys.argv:
12+
sys.argv.remove("-T")
13+
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
14+
15+
16+
def solve(data: str, part1: bool):
17+
18+
enabled = True
19+
total_sum = 0
20+
i = 0
21+
22+
while i < len(data):
23+
24+
if data[i : i + 4] == "do()":
25+
enabled = True
26+
i += 4
27+
28+
elif data[i : i + 7] == "don't()":
29+
enabled = False
30+
i += 7
31+
32+
elif data[i : i + 4] == "mul(":
33+
if m := re.match(r"mul\((\d+),(\d+)\)", data[i:]):
34+
x, y = map(int, m.groups())
35+
if enabled or part1:
36+
total_sum += x * y
37+
i += len(m.group(0))
38+
else:
39+
i += 4
40+
41+
else:
42+
i += 1
43+
44+
return total_sum
45+
46+
47+
if self_tests:
48+
assert solve(Path("sample_1.txt").read_text(), True) == 161
49+
assert solve(Path("sample_2.txt").read_text(), False) == 48
50+
51+
else:
52+
data = Path(filename).read_text().strip()
53+
print(solve(data, True))
54+
print(solve(data, False))

2024/day3/day3.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//! [Day 3: Mull It Over](https://adventofcode.com/2024/day/3)
2+
3+
use regex::Regex;
4+
5+
struct Puzzle {
6+
data: String,
7+
}
8+
9+
impl Puzzle {
10+
fn new() -> Puzzle {
11+
Puzzle {
12+
data: String::new(),
13+
}
14+
}
15+
16+
/// Get the puzzle input.
17+
fn configure(&mut self, path: &str) {
18+
self.data = std::fs::read_to_string(path).unwrap();
19+
}
20+
21+
/// Compute valid mul() operations.
22+
/// if part1 is false, take care of do()/don't() statements.
23+
fn solve(data: &str, part1: bool) -> u64 {
24+
let mut enabled = true;
25+
let mut total_sum = 0;
26+
let mut i = 0;
27+
28+
let pattern = Regex::new(r"^mul\((\d+),(\d+)\).*").unwrap();
29+
30+
while i < data.len() {
31+
if i + 4 < data.len() && &data[i..i + 4] == "do()" {
32+
enabled = true;
33+
i += 4;
34+
} else if i + 7 < data.len() && &data[i..i + 7] == "don't()" {
35+
enabled = false;
36+
i += 7;
37+
} else if i + 4 < data.len() && &data[i..i + 4] == "mul(" {
38+
if let Some(caps) = pattern.captures(&data[i..]) {
39+
let x: u64 = caps[1].parse().unwrap();
40+
let y: u64 = caps[2].parse().unwrap();
41+
if enabled || part1 {
42+
total_sum += x * y;
43+
}
44+
i += caps.len();
45+
} else {
46+
i += 4;
47+
}
48+
} else {
49+
i += 1;
50+
}
51+
}
52+
53+
total_sum
54+
}
55+
56+
/// Solve part one.
57+
fn part1(&self) -> u64 {
58+
Self::solve(&self.data, true)
59+
}
60+
61+
/// Solve part two.
62+
fn part2(&self) -> u64 {
63+
Self::solve(&self.data, false)
64+
}
65+
}
66+
67+
fn main() {
68+
let args = aoc::parse_args();
69+
let mut puzzle = Puzzle::new();
70+
puzzle.configure(args.path.as_str());
71+
println!("{}", puzzle.part1());
72+
println!("{}", puzzle.part2());
73+
}
74+
75+
/// Test from puzzle input
76+
#[cfg(test)]
77+
mod test {
78+
use super::*;
79+
80+
#[test]
81+
fn test01() {
82+
let mut puzzle = Puzzle::new();
83+
puzzle.configure("sample_1.txt");
84+
assert_eq!(puzzle.part1(), 161);
85+
assert_eq!(puzzle.part2(), 161);
86+
}
87+
88+
#[test]
89+
fn test02() {
90+
let mut puzzle = Puzzle::new();
91+
puzzle.configure("sample_2.txt");
92+
assert_eq!(puzzle.part1(), 161);
93+
assert_eq!(puzzle.part2(), 48);
94+
}
95+
}

2024/day3/sample_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))

2024/day3/sample_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))

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
# Advent of Code in Rust 🦀
22

3-
![Stars: 454](https://img.shields.io/badge/Stars-454⭐-blue)
4-
![Rust: 173](https://img.shields.io/badge/Rust-173-cyan?logo=Rust)
5-
![Python: 115](https://img.shields.io/badge/Python-115-cyan?logo=Python)
3+
![Stars: 456](https://img.shields.io/badge/Stars-456⭐-blue)
4+
![Rust: 174](https://img.shields.io/badge/Rust-174-cyan?logo=Rust)
5+
![Python: 116](https://img.shields.io/badge/Python-116-cyan?logo=Python)
66

77
Solutions of [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org), and sometimes in [Python](https://www.python.org/).
88

99
Made for fun 😎 and to practice Rust. Many thanks to [Eric Wastl](https://twitter.com/ericwastl).
1010

11-
## 2024 (current event) ([Calendar](https://adventofcode.com/2024)) ([Solutions](2024/)) : 4
11+
## 2024 (current event) ([Calendar](https://adventofcode.com/2024)) ([Solutions](2024/)) : 6
1212

1313
Puzzle | Stars | Languages
1414
---------------------------------------------------------------- | ----- | -----------
1515
[Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1) | ⭐⭐ | [Rust](2024/day1/day1.rs) [Python](2024/day1/day1.py) [C](../2024/day1/day1.c) [Go](../2024/day1/day1.go)
1616
[Day 2: Red-Nosed Reports](https://adventofcode.com/2024/day/2) | ⭐⭐ | [Rust](2024/day2/day2.rs) [Python](2024/day2/day2.py)
17+
[Day 3: Mull It Over](https://adventofcode.com/2024/day/3) | ⭐⭐ | [Rust](2024/day3/day3.rs) [Python](2024/day3/day3.py)
1718

1819
## Paste years
1920

2021
Calendar | Solutions | Stars | Rust | Python
2122
-------- | --------- | ----- | ---- | ------
22-
[Advent of Code 2024](https://adventofcode.com/2024) | [Solutions](2024/README.md) | 4⭐ | 2 | 2
23+
[Advent of Code 2024](https://adventofcode.com/2024) | [Solutions](2024/README.md) | 6⭐ | 3 | 3
2324
[Advent of Code 2023](https://adventofcode.com/2023) | [Solutions](2023/README.md) | 50⭐ | 24 | 11
2425
[Advent of Code 2022](https://adventofcode.com/2022) | [Solutions](2022/README.md) | 50⭐ | 24 | 18
2526
[Advent of Code 2021](https://adventofcode.com/2021) | [Solutions](2021/README.md) | 50⭐ | 23 | 12

scripts/allpython.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ a()
3434
i 3.10.13
3535
i 3.11.8
3636
i 3.12.2
37-
i 3.13.0a3
37+
i 3.13.0
3838
}
3939

4040
if [ ${1-} ]; then

scripts/runall.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ def make(year: int, source: Path, dest: Path, language: str):
220220

221221
if language == "C":
222222
cmd = "cc -std=c11"
223-
cmdline = f"{cmd} -o {output} -Wall -Wextra -O3 -DSTANDALONE -I{source.parent} {source}"
223+
cmdline = f"{cmd} -o {output} -Wall -Wextra -Werror -O3 -DSTANDALONE -I{source.parent} {source}"
224224
elif language == "C++":
225-
cmd = "c++ -std=c++17"
226-
cmdline = f"{cmd} -o {output} -Wall -Wextra -O3 -DSTANDALONE -I{source.parent} {source}"
225+
cmd = "c++ -std=c++23"
226+
cmdline = f"{cmd} -o {output} -Wall -Wextra -Werror -O3 -DSTANDALONE -I{source.parent} {source}"
227227
elif language == "Java":
228228
cmdline = f"javac -d {build} {source}"
229229
elif language == "Go":

0 commit comments

Comments
 (0)