Skip to content

Commit cd19d06

Browse files
committed
style: format
1 parent 800e0f0 commit cd19d06

File tree

7 files changed

+242
-93
lines changed

7 files changed

+242
-93
lines changed

2024/day-1/src/main.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,31 @@ fn main() {
1111

1212
input.lines().for_each(|line| {
1313
let (left_part, right_part) = line.split_once(" ").expect("An unexpected error occurred");
14-
left_numbers.push(left_part.trim().parse::<usize>().expect("An unexpected error occurred while coercing a string to a number"));
15-
right_numbers.push(right_part.trim().parse::<usize>().expect("An unexpected error occurred while coercing a string to a number"));
14+
left_numbers.push(
15+
left_part
16+
.trim()
17+
.parse::<usize>()
18+
.expect("An unexpected error occurred while coercing a string to a number"),
19+
);
20+
right_numbers.push(
21+
right_part
22+
.trim()
23+
.parse::<usize>()
24+
.expect("An unexpected error occurred while coercing a string to a number"),
25+
);
1626
});
1727

1828
left_numbers.sort();
1929
right_numbers.sort();
2030

2131
let mut differences: usize = 0;
2232

23-
for(&left, &right) in left_numbers.iter().zip(right_numbers.iter()) {
24-
differences += if left > right { left - right } else { right - left };
33+
for (&left, &right) in left_numbers.iter().zip(right_numbers.iter()) {
34+
differences += if left > right {
35+
left - right
36+
} else {
37+
right - left
38+
};
2539
}
2640

2741
println!("Sum of differences: {}", differences);

2024/day-2/src/main.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,30 @@ fn main() {
99
let mut safe_count: u32 = 0;
1010

1111
input.lines().for_each(|line| {
12-
let levels = line.split(" ").map(|level| level.parse::<u32>()).collect::<Result<Vec<_>, _>>().expect("Invalid input");
12+
let levels = line
13+
.split(" ")
14+
.map(|level| level.parse::<u32>())
15+
.collect::<Result<Vec<_>, _>>()
16+
.expect("Invalid input");
1317

14-
if is_safe(&levels) { safe_count += 1; }
18+
if is_safe(&levels) {
19+
safe_count += 1;
20+
}
1521
});
1622
println!("Safe count: {}", safe_count);
1723

1824
// ── Part 2 ──────────────────────────────────────────────────────────
1925
let mut dampener_safe_count: u32 = 0;
2026
input.lines().for_each(|line| {
21-
let levels = line.split(" ").map(|level| level.parse::<u32>()).collect::<Result<Vec<_>, _>>().expect("Invalid input");
27+
let levels = line
28+
.split(" ")
29+
.map(|level| level.parse::<u32>())
30+
.collect::<Result<Vec<_>, _>>()
31+
.expect("Invalid input");
2232

23-
if is_safe(&levels) { dampener_safe_count += 1; }
24-
else {
33+
if is_safe(&levels) {
34+
dampener_safe_count += 1;
35+
} else {
2536
for i in 0..levels.len() {
2637
let mut new_levels = levels.clone();
2738
new_levels.remove(i);
@@ -42,13 +53,21 @@ fn is_safe(levels: &Vec<u32>) -> bool {
4253
let mut is_safe = false;
4354

4455
for (i, level) in levels.iter().enumerate() {
45-
if i == 0 { continue; }
46-
if !(1..=3).contains(&level.abs_diff(levels[i - 1])) { break; }
56+
if i == 0 {
57+
continue;
58+
}
59+
if !(1..=3).contains(&level.abs_diff(levels[i - 1])) {
60+
break;
61+
}
4762
if *level > levels[i - 1] {
48-
if has_downwards_pattern { break; }
63+
if has_downwards_pattern {
64+
break;
65+
}
4966
has_upwards_pattern = true;
5067
} else if *level < levels[i - 1] {
51-
if has_upwards_pattern { break; }
68+
if has_upwards_pattern {
69+
break;
70+
}
5271
has_downwards_pattern = true;
5372
}
5473
if levels.len() - 1 == i {

2024/day-3/src/main.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,32 @@ fn main() {
1414

1515
// ── Part 2 ──────────────────────────────────────────────────────────
1616
for (i, unparsed_excluded_part) in input.split("don't()").enumerate() {
17-
if i == 0 { continue; }
17+
if i == 0 {
18+
continue;
19+
}
1820
let excluded_part = unparsed_excluded_part.split_once("do()");
1921
if excluded_part.is_none() {
2022
if i != input.split("dont'()").collect::<Vec<_>>().len() {
21-
sum -= parse_and_multiply(unparsed_excluded_part);
23+
sum -= parse_and_multiply(unparsed_excluded_part);
2224
}
2325
} else {
2426
sum -= parse_and_multiply(excluded_part.expect("Error parsing excluded part").0);
2527
}
2628
}
27-
println!("Sum of all multiplications excluding the excluded parts: {}", sum);
29+
println!(
30+
"Sum of all multiplications excluding the excluded parts: {}",
31+
sum
32+
);
2833
}
2934

3035
fn parse_and_multiply(input: &str) -> u32 {
3136
let regex = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").expect("Invalid regex");
3237
let mut multiplications: Vec<(u32, u32)> = vec![];
3338
for (_, [first_value, second_value]) in regex.captures_iter(input).map(|c| c.extract()) {
34-
multiplications.push((first_value.parse::<u32>().expect("Error parsing"), second_value.parse::<u32>().expect("Error parsing")));
39+
multiplications.push((
40+
first_value.parse::<u32>().expect("Error parsing"),
41+
second_value.parse::<u32>().expect("Error parsing"),
42+
));
3543
}
3644
multiplications.iter().map(|(a, b)| a * b).sum::<u32>()
3745
}

2024/day-4/src/main.rs

+76-30
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,55 @@
22
// │ Advent of Code 2024 - Day 4 │
33
// ╰─────────────────────────────────────────────────────────╯
44

5-
use glam::{ ivec2, IVec2 };
5+
use glam::{ivec2, IVec2};
66

77
fn main() {
88
// ── Part 1 ──────────────────────────────────────────────────────────
99
let input = include_str!("../input.txt");
1010

11-
let map = input.lines().map(|line| line.chars().collect::<Vec<_>>()).collect::<Vec<_>>();
11+
let map = input
12+
.lines()
13+
.map(|line| line.chars().collect::<Vec<_>>())
14+
.collect::<Vec<_>>();
1215

13-
let result_xmas = map.iter().enumerate().map(|(y, line)| {
14-
line.iter().enumerate().filter_map(|(x, c)| if *c == 'X' { Some(ivec2(x as i32, y as i32)) } else { None }).map(|x_vec| {
15-
count_xmas(&map, x_vec)
16-
}).sum::<usize>()
17-
}).sum::<usize>();
16+
let result_xmas = map
17+
.iter()
18+
.enumerate()
19+
.map(|(y, line)| {
20+
line.iter()
21+
.enumerate()
22+
.filter_map(|(x, c)| {
23+
if *c == 'X' {
24+
Some(ivec2(x as i32, y as i32))
25+
} else {
26+
None
27+
}
28+
})
29+
.map(|x_vec| count_xmas(&map, x_vec))
30+
.sum::<usize>()
31+
})
32+
.sum::<usize>();
1833

1934
println!("Number of XMAS: {result_xmas}");
2035

2136
// ── Part 2 ──────────────────────────────────────────────────────────
22-
let result_mas = map.iter().enumerate().map(|(y, line)| {
23-
line.iter().enumerate().filter_map(|(x, c)| if *c == 'A' { Some(ivec2(x as i32, y as i32)) } else { None }).filter(|a_vec| {
24-
count_mas(&map, *a_vec)
25-
}).count()
26-
}).sum::<usize>();
37+
let result_mas = map
38+
.iter()
39+
.enumerate()
40+
.map(|(y, line)| {
41+
line.iter()
42+
.enumerate()
43+
.filter_map(|(x, c)| {
44+
if *c == 'A' {
45+
Some(ivec2(x as i32, y as i32))
46+
} else {
47+
None
48+
}
49+
})
50+
.filter(|a_vec| count_mas(&map, *a_vec))
51+
.count()
52+
})
53+
.sum::<usize>();
2754

2855
println!("Number of X-MAS: {result_mas}");
2956
}
@@ -34,12 +61,11 @@ fn count_mas(map: &Vec<Vec<char>>, a_vec: IVec2) -> bool {
3461
let bottom_left = a_vec + ivec2(-1, 1);
3562
let bottom_right = a_vec + ivec2(1, 1);
3663

37-
if !(
38-
is_in_bounds(map, top_left)
64+
if !(is_in_bounds(map, top_left)
3965
&& is_in_bounds(map, top_right)
4066
&& is_in_bounds(map, bottom_left)
41-
&& is_in_bounds(map, bottom_right)
42-
) {
67+
&& is_in_bounds(map, bottom_right))
68+
{
4369
return false;
4470
}
4571

@@ -48,26 +74,46 @@ fn count_mas(map: &Vec<Vec<char>>, a_vec: IVec2) -> bool {
4874
let bottom_left_char = get_in_map(map, bottom_left);
4975
let bottom_right_char = get_in_map(map, bottom_right);
5076

51-
top_left_char == 'M' && bottom_right_char == 'S' && top_right_char == 'S' && bottom_left_char == 'M'
52-
|| top_left_char == 'M' && bottom_right_char == 'S' && top_right_char == 'M' && bottom_left_char == 'S'
53-
|| top_left_char == 'S' && bottom_right_char == 'M' && top_right_char == 'M' && bottom_left_char == 'S'
54-
|| top_left_char == 'S' && bottom_right_char == 'M' && top_right_char == 'S' && bottom_left_char == 'M'
77+
top_left_char == 'M'
78+
&& bottom_right_char == 'S'
79+
&& top_right_char == 'S'
80+
&& bottom_left_char == 'M'
81+
|| top_left_char == 'M'
82+
&& bottom_right_char == 'S'
83+
&& top_right_char == 'M'
84+
&& bottom_left_char == 'S'
85+
|| top_left_char == 'S'
86+
&& bottom_right_char == 'M'
87+
&& top_right_char == 'M'
88+
&& bottom_left_char == 'S'
89+
|| top_left_char == 'S'
90+
&& bottom_right_char == 'M'
91+
&& top_right_char == 'S'
92+
&& bottom_left_char == 'M'
5593
}
5694

5795
fn count_xmas(map: &Vec<Vec<char>>, x_vec: IVec2) -> usize {
5896
const DIRECTIONS: [IVec2; 8] = [
59-
ivec2(-1, -1), ivec2(0, -1), ivec2(1, -1),
60-
ivec2(-1, 0), ivec2(1, 0),
61-
ivec2(-1, 1), ivec2(0, 1), ivec2(1, 1)
97+
ivec2(-1, -1),
98+
ivec2(0, -1),
99+
ivec2(1, -1),
100+
ivec2(-1, 0),
101+
ivec2(1, 0),
102+
ivec2(-1, 1),
103+
ivec2(0, 1),
104+
ivec2(1, 1),
62105
];
63106

64-
DIRECTIONS.iter().filter(|&&direction| {
65-
let s = x_vec + 3 * direction;
66-
is_in_bounds(map, s)
67-
&& get_in_map(map, x_vec + direction) == 'M'
68-
&& get_in_map(map, x_vec + 2 * direction) == 'A'
69-
&& get_in_map(map, s) == 'S'
70-
}).count()
107+
DIRECTIONS
108+
.iter()
109+
.filter(|&&direction| {
110+
let s = x_vec + 3 * direction;
111+
is_in_bounds(map, s)
112+
&& get_in_map(map, x_vec + direction) == 'M'
113+
&& get_in_map(map, x_vec + 2 * direction) == 'A'
114+
&& get_in_map(map, s) == 'S'
115+
})
116+
.count()
71117
}
72118

73119
fn get_in_map(map: &Vec<Vec<char>>, pos: IVec2) -> char {

2024/day-5/src/main.rs

+29-12
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,49 @@
55
use std::cmp::Ordering;
66

77
fn main() {
8-
let (unparsed_rules, unparsed_updates) = include_str!("../input.txt").split_once("\n\n").expect("Invalid input");
9-
let rules = unparsed_rules.lines()
8+
let (unparsed_rules, unparsed_updates) = include_str!("../input.txt")
9+
.split_once("\n\n")
10+
.expect("Invalid input");
11+
let rules = unparsed_rules
12+
.lines()
1013
.map(|line| line.split_once("|").unwrap())
1114
.map(|(a, b)| (a.parse::<u32>().unwrap(), b.parse::<u32>().unwrap()))
1215
.collect::<Vec<_>>();
13-
let updates = unparsed_updates.lines()
14-
.map(|line| line.split(",").map(|page| page.parse::<u32>().unwrap()).collect::<Vec<_>>())
16+
let updates = unparsed_updates
17+
.lines()
18+
.map(|line| {
19+
line.split(",")
20+
.map(|page| page.parse::<u32>().unwrap())
21+
.collect::<Vec<_>>()
22+
})
1523
.collect::<Vec<_>>();
1624
// ── Part 1 ──────────────────────────────────────────────────────────
17-
let result_part1 = updates.iter()
25+
let result_part1 = updates
26+
.iter()
1827
.filter(|update| rules.iter().all(|rule| complies_with_rule(rule, update)))
1928
.map(|update| update[update.len() / 2])
2029
.sum::<u32>();
2130

2231
println!("Result: {}", result_part1);
2332
// ── Part 2 ──────────────────────────────────────────────────────────
24-
let result_part2 = updates.iter()
33+
let result_part2 = updates
34+
.iter()
2535
.filter(|update| !rules.iter().all(|rule| complies_with_rule(rule, update)))
2636
.map(|update| {
2737
let mut sorted_update = update.clone();
2838
sorted_update.sort_unstable_by(|a, b| {
29-
match rules.iter().find(|&(x, y)| (x == a || x == b) && (y == a || y == b)) {
39+
match rules
40+
.iter()
41+
.find(|&(x, y)| (x == a || x == b) && (y == a || y == b))
42+
{
3043
Some((x, y)) => {
31-
if a == x && b == y { Ordering::Less }
32-
else { Ordering::Greater }
33-
},
34-
None => Ordering::Equal
44+
if a == x && b == y {
45+
Ordering::Less
46+
} else {
47+
Ordering::Greater
48+
}
49+
}
50+
None => Ordering::Equal,
3551
}
3652
});
3753
sorted_update
@@ -46,5 +62,6 @@ fn complies_with_rule(rule: &(u32, u32), update: &Vec<u32>) -> bool {
4662

4763
!update.contains(&a)
4864
|| !update.contains(&b)
49-
|| update.iter().position(|page| page == a).unwrap() < update.iter().position(|page| page == b).unwrap()
65+
|| update.iter().position(|page| page == a).unwrap()
66+
< update.iter().position(|page| page == b).unwrap()
5067
}

0 commit comments

Comments
 (0)