Skip to content

Commit c30d46b

Browse files
committed
24 04
1 parent 6cd3da1 commit c30d46b

File tree

10 files changed

+474
-46
lines changed

10 files changed

+474
-46
lines changed

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,27 @@ fn main() {
6363
}
6464
```
6565

66-
## Benchmarks 2024 | Updated 2024, december 5
66+
## Benchmarks 2024 | Updated 2024, december 7
6767
```
6868
== Benchmarks by Date and Part ==
69-
2024_01 part 1 .......... 130.714µs (ran 10000/10000)
70-
2024_01 part 2 .......... 157.652µs (ran 10000/10000)
71-
2024_02 part 1 .......... 243µs (ran 10000/10000)
72-
2024_02 part 2 .......... 308.167µs (ran 10000/10000)
69+
2024_01 part 1 .......... 132.348µs (ran 10000/10000)
70+
2024_01 part 2 .......... 163.88µs (ran 10000/10000)
71+
2024_02 part 1 .......... 243.884µs (ran 10000/10000)
72+
2024_02 part 2 .......... 309.05µs (ran 10000/10000)
73+
2024_03 part 1 .......... 34.239µs (ran 10000/10000)
74+
2024_03 part 2 .......... 442.076µs (ran 10000/10000)
75+
2024_04 part 1 .......... 352.786µs (ran 10000/10000)
76+
2024_04 part 2 .......... 144.523µs (ran 10000/10000)
7377
7478
== Benchmarks by Speed ==
75-
2024_01 part 1 .......... 130.714µs (ran 10000/10000)
76-
2024_01 part 2 .......... 157.652µs (ran 10000/10000)
77-
2024_02 part 1 .......... 243µs (ran 10000/10000)
78-
2024_02 part 2 .......... 308.167µs (ran 10000/10000)
79+
2024_03 part 1 .......... 34.239µs (ran 10000/10000)
80+
2024_01 part 1 .......... 132.348µs (ran 10000/10000)
81+
2024_04 part 2 .......... 144.523µs (ran 10000/10000)
82+
2024_01 part 2 .......... 163.88µs (ran 10000/10000)
83+
2024_02 part 1 .......... 243.884µs (ran 10000/10000)
84+
2024_02 part 2 .......... 309.05µs (ran 10000/10000)
85+
2024_04 part 1 .......... 352.786µs (ran 10000/10000)
86+
2024_03 part 2 .......... 442.076µs (ran 10000/10000)
7987
```
8088

8189
## Benchmarks 2023 | Updated 2023, december 8

bencher/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ pub fn bench_2024(output: &mut Vec<utils::BenchResult>, max_time: Option<std::ti
3434
output.push(utils::bench(part_1, 1, date(), max_time));
3535
output.push(utils::bench(part_2, 2, date(), max_time));
3636
}
37+
38+
{ pub use solutions::p2024_04::*;
39+
output.push(utils::bench(part_1, 1, date(), max_time));
40+
output.push(utils::bench(part_2, 2, date(), max_time));
41+
}
3742
}
3843

3944
#[rustfmt::skip]

solutions/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,7 @@ path = "src/p2024_02.rs"
163163
name = "2024_03"
164164
path = "src/p2024_03.rs"
165165
required-features = ["regex"]
166+
167+
[[bin]]
168+
name = "2024_04"
169+
path = "src/p2024_04.rs"

solutions/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
let now = chrono::Utc::now().date_naive();
88
let (year, day) = (now.year(), now.day());
99

10-
let day = 03;
10+
let day = 04;
1111

1212
let input_folder = format!("input");
1313
let date_folder = format!("{:04}_{:02}", year, day);

solutions/input/2024_04/input

Lines changed: 140 additions & 0 deletions
Large diffs are not rendered by default.

solutions/input/2024_04/input_53616

Lines changed: 140 additions & 0 deletions
Large diffs are not rendered by default.

solutions/input/2024_04/test_01

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MMMSXXMASM
2+
MSAMXMSMSA
3+
AMXSXMAAMM
4+
MSAMASMSMX
5+
XMASAMXAMM
6+
XXAMMXXAMA
7+
SMSMSASXSS
8+
SAXAMASAAA
9+
MAMMMXMMMM
10+
MXMXAXMASX

solutions/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ pub mod p2024_01;
4545
pub mod p2024_02;
4646
#[cfg(feature = "regex")]
4747
pub mod p2024_03;
48+
pub mod p2024_04;

solutions/src/p2024_04.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#![allow(dead_code, unreachable_code, unused)]
2+
3+
use utils::*;
4+
5+
pub type Data = Vec<Vec<char>>;
6+
7+
pub fn parse_data(input: utils::Input) -> Data {
8+
input
9+
.lines()
10+
.map(|line| line.trim().chars().collect())
11+
.collect()
12+
}
13+
14+
pub fn part_1(input: utils::Input) -> String {
15+
let data = parse_data(input);
16+
17+
let them = ['M', 'A', 'S'];
18+
19+
let (w, h) = (data[0].len(), data.len());
20+
let deltas = [
21+
(1, 1),
22+
(1, 0),
23+
(1, -1),
24+
(-1, 1),
25+
(-1, 0),
26+
(-1, -1),
27+
(0, 1),
28+
(0, -1),
29+
];
30+
31+
let mut result = 0;
32+
for y in 0..h {
33+
for x in 0..w {
34+
if data[y][x] != 'X' {
35+
continue;
36+
}
37+
38+
'delta: for (dx, dy) in deltas {
39+
for i in 1..=3 {
40+
let (nx, ny) = (x as i32 + dx * i, y as i32 + dy * i);
41+
if nx < 0 || ny < 0 || ny >= h as i32 || nx >= w as i32 {
42+
continue 'delta;
43+
}
44+
let (nx, ny) = (nx as usize, ny as usize);
45+
if data[ny][nx] != them[(i - 1) as usize] {
46+
continue 'delta;
47+
}
48+
}
49+
50+
result += 1;
51+
}
52+
}
53+
}
54+
55+
format!("{}", result)
56+
}
57+
58+
pub fn part_2(input: utils::Input) -> String {
59+
let data = parse_data(input);
60+
61+
let mut result = 0;
62+
63+
for y in 1..data.len() - 1 {
64+
for x in 1..data[y].len() - 1 {
65+
if data[y][x] != 'A' {
66+
continue;
67+
}
68+
69+
let mut num = 0;
70+
for (dx, dy) in [(-1, -1), (1, -1)] {
71+
let (nx, ny) = ((x as i32 + dx) as usize, (y as i32 + dy) as usize);
72+
let (nxx, nyy) = ((x as i32 - dx) as usize, (y as i32 - dy) as usize);
73+
let (a, b) = (data[ny][nx], data[nyy][nxx]);
74+
if (a == 'M' && b == 'S') || (a == 'S' && b == 'M') {
75+
num += 1;
76+
}
77+
}
78+
79+
if num == 2 {
80+
result += 1;
81+
}
82+
}
83+
}
84+
85+
format!("{}", result)
86+
}
87+
88+
pub fn run_1(date: utils::Date) {
89+
#[rustfmt::skip]
90+
let tests_1 = [
91+
(1, Some("18")),
92+
];
93+
94+
let all_correct_1 = utils::test(part_1, date, 1, &tests_1);
95+
if all_correct_1 {
96+
let answer = utils::run(part_1, 1, date);
97+
}
98+
}
99+
100+
pub fn run_2(date: utils::Date) {
101+
#[rustfmt::skip]
102+
let tests_2 = [
103+
(1, Some("9")),
104+
];
105+
106+
let all_correct_2 = utils::test(part_2, date, 2, &tests_2);
107+
if all_correct_2 {
108+
let answer = utils::run(part_2, 2, date);
109+
}
110+
}
111+
112+
pub fn date() -> utils::Date {
113+
utils::date_from_file_name(file!())
114+
}
115+
116+
fn main() {
117+
let date = date();
118+
run_1(date);
119+
run_2(date);
120+
}

utils/src/lib.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,33 @@ pub use std::{
44
io::{BufRead, BufReader},
55
};
66

7-
pub type Date = (usize, usize);
8-
pub type Input = String;
7+
#[inline]
8+
pub const fn gcd(a: usize, b: usize) -> usize {
9+
if b == 0 {
10+
a
11+
} else {
12+
gcd(b, a % b)
13+
}
14+
}
915

10-
pub fn date_from_file_name(name: &str) -> Date {
11-
let (_p, rest) = name.split_once("p").unwrap();
12-
let (year, rest) = rest.split_once("_").unwrap();
13-
let (day, _rest) = rest.split_once(".").unwrap();
16+
#[inline]
17+
pub const fn lcm(a: usize, b: usize) -> usize {
18+
if a == 0 || b == 0 {
19+
0
20+
} else {
21+
(a * b) / gcd(a, b)
22+
}
23+
}
1424

15-
(year.parse().unwrap(), day.parse().unwrap())
25+
#[inline]
26+
pub fn lcm_of_slice(numbers: &[usize]) -> usize {
27+
let mut result = numbers[0];
28+
29+
for &num in numbers.iter().skip(1) {
30+
result = lcm(result, num);
31+
}
32+
33+
result
1634
}
1735

1836
#[rustfmt::skip]
@@ -23,6 +41,17 @@ pub fn char_to_n(c: char) -> usize {
2341
}
2442
}
2543

44+
pub type Date = (usize, usize);
45+
pub type Input = String;
46+
47+
pub fn date_from_file_name(name: &str) -> Date {
48+
let (_p, rest) = name.split_once("p").unwrap();
49+
let (year, rest) = rest.split_once("_").unwrap();
50+
let (day, _rest) = rest.split_once(".").unwrap();
51+
52+
(year.parse().unwrap(), day.parse().unwrap())
53+
}
54+
2655
pub fn get_input(date: Date, suffix: &str) -> Option<Input> {
2756
let (year, day) = date;
2857

@@ -185,32 +214,3 @@ where
185214

186215
all_correct
187216
}
188-
189-
#[inline]
190-
pub const fn gcd(a: usize, b: usize) -> usize {
191-
if b == 0 {
192-
a
193-
} else {
194-
gcd(b, a % b)
195-
}
196-
}
197-
198-
#[inline]
199-
pub const fn lcm(a: usize, b: usize) -> usize {
200-
if a == 0 || b == 0 {
201-
0
202-
} else {
203-
(a * b) / gcd(a, b)
204-
}
205-
}
206-
207-
#[inline]
208-
pub fn lcm_of_slice(numbers: &[usize]) -> usize {
209-
let mut result = numbers[0];
210-
211-
for &num in numbers.iter().skip(1) {
212-
result = lcm(result, num);
213-
}
214-
215-
result
216-
}

0 commit comments

Comments
 (0)