Skip to content

Commit c8c67ed

Browse files
committed
[2025] Solution for Day 2
1 parent 487d6ad commit c8c67ed

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

2025/src/bin/day02.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use aoc2025::days::day02::{part1, part2};
2+
use std::env;
3+
use std::fs;
4+
use std::process;
5+
6+
fn main() {
7+
let args: Vec<String> = env::args().collect();
8+
if args.len() < 2 {
9+
println!("Missing required arguments");
10+
process::exit(1);
11+
}
12+
13+
let contents = fs::read_to_string(&args[1]).expect("Something went wrong");
14+
println!("Part 1: {}", part1(&contents));
15+
println!("Part 2: {}", part2(&contents));
16+
}

2025/src/days.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pub mod day01;
1+
pub mod day01;
2+
pub mod day02;

2025/src/days/day02.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::str::FromStr;
2+
3+
pub fn part1(contents: &str) -> i64 {
4+
let mut sum = 0;
5+
for (start, end) in parse_data(contents) {
6+
for i in start..=end {
7+
let num_str = i.to_string();
8+
let split = num_str.split_at(num_str.len() / 2);
9+
if split.0 == split.1 {
10+
sum += i;
11+
}
12+
}
13+
}
14+
sum
15+
}
16+
17+
pub fn part2(contents: &str) -> i64 {
18+
let mut sum = 0;
19+
for (start, end) in parse_data(contents) {
20+
for i in start..=end {
21+
let num_str = i.to_string();
22+
if match_pattern(&num_str) {
23+
sum += i;
24+
}
25+
}
26+
}
27+
sum
28+
}
29+
30+
fn parse_data(contents: &str) -> Vec<(i64, i64)> {
31+
contents
32+
.trim()
33+
.split(',')
34+
.map(|range| {
35+
let mut bounds = range.split('-').map(|n| i64::from_str(n).unwrap());
36+
let start = bounds.next().unwrap();
37+
let end = bounds.next().unwrap();
38+
(start, end)
39+
})
40+
.collect()
41+
}
42+
43+
fn match_pattern(s: &str) -> bool {
44+
for i in 1..=s.len() / 2 {
45+
if s.len() % i != 0 {
46+
continue;
47+
}
48+
49+
let comparison = &s[0..i];
50+
for j in (i..s.len()).step_by(i) {
51+
let end = j + i;
52+
if &s[j..end] != comparison {
53+
break;
54+
}
55+
if end == s.len() {
56+
return true;
57+
}
58+
}
59+
}
60+
false
61+
}
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
fn test_part1() {
69+
let content = "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124";
70+
71+
assert_eq!(part1(&content), 1227775554);
72+
}
73+
74+
#[test]
75+
fn test_part2() {
76+
let content = "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124";
77+
78+
assert_eq!(part2(&content), 4174379265);
79+
}
80+
}

2025/tests/day02.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use aoc2025::days::day02::{part1, part2};
2+
3+
#[test]
4+
fn test_part1() {
5+
let content = std::include_str!("../../private/inputs/2025/day02.txt");
6+
assert_eq!(part1(&content), 19605500130);
7+
}
8+
9+
#[test]
10+
fn test_part2() {
11+
let content = std::include_str!("../../private/inputs/2025/day02.txt");
12+
assert_eq!(part2(&content), 36862281418);
13+
}

private

0 commit comments

Comments
 (0)