Skip to content

Commit 6a6b959

Browse files
committed
add 2024 to CI
1 parent 4424702 commit 6a6b959

File tree

5 files changed

+180
-1
lines changed

5 files changed

+180
-1
lines changed

Diff for: .github/workflows/rust.yml

+2
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ jobs:
3030
cargo test
3131
cd ../2023
3232
cargo test
33+
cd ../2024
34+
cargo test

Diff for: 2024/Cargo.lock

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: 2024/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ aoc-runner = "*"
99
aoc-runner-derive = "*"
1010
itertools = "*"
1111
lazy_static = "*"
12+
nom = "7.1.3"
1213
pathfinding = "*"
1314
strum = "*"
14-
strum_macros = "*"
15+
strum_macros = "*"

Diff for: 2024/src/day3.rs

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
use aoc_runner_derive::{aoc, aoc_generator};
2+
use nom::{branch::alt, bytes::complete::{tag, take_while_m_n}, combinator::{map, map_res}, sequence::tuple, IResult};
3+
4+
/*
5+
6+
--- Day 3: Mull It Over ---
7+
"Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to check the warehouse, though," says the mildly flustered shopkeeper at the North Pole Toboggan Rental Shop. The Historians head out to take a look.
8+
9+
The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?"
10+
11+
The computer appears to be trying to run a program, but its memory (your puzzle input) is corrupted. All of the instructions have been jumbled up!
12+
13+
It seems like the goal of the program is just to multiply some numbers. It does that with instructions like mul(X,Y), where X and Y are each 1-3 digit numbers. For instance, mul(44,46) multiplies 44 by 46 to get a result of 2024. Similarly, mul(123,4) would multiply 123 by 4.
14+
15+
However, because the program's memory has been corrupted, there are also many invalid characters that should be ignored, even if they look like part of a mul instruction. Sequences like mul(4*, mul(6,9!, ?(12,34), or mul ( 2 , 4 ) do nothing.
16+
17+
For example, consider the following section of corrupted memory:
18+
19+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
20+
Only the four highlighted sections are real mul instructions. Adding up the result of each instruction produces 161 (2*4 + 5*5 + 11*8 + 8*5).
21+
22+
Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
23+
24+
*/
25+
26+
#[derive(Debug)]
27+
struct Mul {
28+
x: u32,
29+
y: u32,
30+
}
31+
32+
fn number(input: &str) -> IResult<&str, u32> {
33+
use std::str::FromStr;
34+
map_res(take_while_m_n(1, 3, |c: char| c.is_digit(10)), u32::from_str)(input)
35+
}
36+
37+
fn parse_mul(input: &str) -> IResult<&str, Mul> {
38+
map(
39+
tuple((tag("mul("), number, tag(","), number, tag(")"))),
40+
|(_, x, _, y, _)| Mul { x, y },
41+
)(input)
42+
}
43+
44+
#[aoc_generator(day3)]
45+
fn parse_input(input: &str) -> Vec<Instruction> {
46+
let input = input.trim();
47+
let input = input.as_bytes();
48+
input
49+
.iter()
50+
.enumerate()
51+
.filter_map(|(i, _c)| {
52+
let s = std::str::from_utf8(&input[i..]).unwrap();
53+
if let Ok((_str, inst)) = parse_inst(s) {
54+
// dbg!("{} -> {:?}", s, &inst);
55+
Some(inst)
56+
} else {
57+
None
58+
}
59+
})
60+
.collect()
61+
}
62+
63+
#[aoc(day3, part1)]
64+
fn part1(ops: &Vec<Instruction>) -> u32 {
65+
ops.iter()
66+
.filter_map(|op| if let Instruction::Mul(mul) = op {
67+
Some(mul.x * mul.y)
68+
} else {
69+
None
70+
}).sum()
71+
}
72+
73+
/*
74+
--- Part Two ---
75+
As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more accurate result.
76+
77+
There are two new instructions you'll need to handle:
78+
79+
The do() instruction enables future mul instructions.
80+
The don't() instruction disables future mul instructions.
81+
Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled.
82+
83+
For example:
84+
85+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
86+
This corrupted memory is similar to the example from before, but this time the mul(5,5) and mul(11,8) instructions are disabled because there is a don't() instruction before them. The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction.
87+
88+
This time, the sum of the results is 48 (2*4 + 8*5).
89+
90+
Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
91+
*/
92+
93+
#[derive(Debug)]
94+
enum Instruction {
95+
Mul(Mul),
96+
Do,
97+
Dont,
98+
}
99+
100+
fn parse_inst(input: &str) -> IResult<&str, Instruction> {
101+
alt((
102+
map(parse_mul, |m| Instruction::Mul(m)),
103+
map(tag("do()"), |_| Instruction::Do),
104+
map(tag("don't()"), |_| Instruction::Dont),
105+
))(input)
106+
}
107+
108+
#[aoc(day3, part2)]
109+
fn part2(ops: &Vec<Instruction>) -> u32 {
110+
let mut enabled = true;
111+
ops.iter()
112+
.filter_map(|op| match op {
113+
Instruction::Mul(m) => {
114+
if enabled {
115+
Some(m)
116+
} else {
117+
None
118+
}
119+
}
120+
Instruction::Do => {
121+
enabled = true;
122+
None
123+
}
124+
Instruction::Dont => {
125+
enabled = false;
126+
None
127+
}
128+
})
129+
.map(|op| op.x * op.y)
130+
.sum()
131+
}
132+
133+
#[cfg(test)]
134+
mod tests {
135+
use super::*;
136+
137+
#[test]
138+
fn part1_example() {
139+
let input = parse_input(
140+
r#"
141+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
142+
"#
143+
.trim(),
144+
);
145+
assert_eq!(part1(&input), 161);
146+
}
147+
148+
#[test]
149+
fn part2_example() {
150+
let input = parse_input(
151+
r#"
152+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
153+
"#
154+
.trim(),
155+
);
156+
assert_eq!(part2(&input), 48);
157+
}
158+
}

Diff for: 2024/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use aoc_runner_derive::aoc_lib;
22

33
mod day1;
44
mod day2;
5+
mod day3;
56

67
aoc_lib! {
78
year = 2024, extra_alternatives = ["fnv"]

0 commit comments

Comments
 (0)