Skip to content

Commit 84498af

Browse files
committed
solve: day2
1 parent 5d5c709 commit 84498af

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
resolver = "2"
33
members = [
44
"aoc_lib/aoc_derive",
5-
"aoc_lib/utils", "day1",
5+
"aoc_lib/utils", "day1", "day2",
66
]
77

88
[workspace.dependencies]

aoc_lib

Submodule aoc_lib updated 1 file

day2/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "day2"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
aoc_derive.path = '../aoc_lib/aoc_derive'
8+
utils.path = '../aoc_lib/utils'
9+
derive_more.workspace = true
10+
itertools.workspace = true
11+
lazy-regex.workspace = true
12+
parse-display.workspace = true
13+
rayon.workspace = true
14+
regex.workspace = true
15+
num.workspace = true
16+
17+
[dev-dependencies]
18+
pretty_assertions.workspace = true

day2/src/main.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use aoc_derive::aoc_main;
2+
use itertools::Itertools;
3+
use num::Integer;
4+
use utils::*;
5+
6+
fn invalid(id: usize) -> bool {
7+
let s = id.to_string();
8+
s.len().is_even() && s[..s.len() / 2] == s[s.len() / 2..]
9+
}
10+
11+
fn invalid2(id: usize) -> bool {
12+
let chars = id.to_string().chars().collect_vec();
13+
(1..=(chars.len() / 2))
14+
.filter(|&count| chars.len().is_multiple_of(count))
15+
.any(|count| chars.iter().skip(count).enumerate().all(|(i, &c)| chars[i % count] == c))
16+
}
17+
18+
#[aoc_main]
19+
fn solve(input: Input) -> impl Into<Solution> {
20+
let ranges = input.as_str().trim().split(',').map(|s| {
21+
let (start, end) = s.split_once('-').unwrap();
22+
start.parse::<usize>().unwrap()..=end.parse::<usize>().unwrap()
23+
});
24+
25+
(
26+
ranges.clone().flat_map(|r| r.filter(|&i| invalid(i))).sum_usize(),
27+
ranges.flat_map(|r| r.filter(|&i| invalid2(i))).sum_usize(),
28+
)
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
35+
#[test]
36+
fn test_examples() {
37+
use utils::assert_example;
38+
assert_example!(
39+
r#"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124"#,
40+
1227775554,
41+
4174379265_usize
42+
);
43+
44+
assert!(!invalid2(10));
45+
assert!(invalid2(11));
46+
assert!(invalid2(1111));
47+
assert!(!invalid2(11110));
48+
assert!(invalid2(10101010));
49+
assert!(invalid2(123123));
50+
assert!(!invalid2(1231231));
51+
assert!(!invalid2(12312));
52+
assert!(!invalid2(1231));
53+
assert!(!invalid2(2));
54+
assert!(!invalid2(1230123));
55+
}
56+
}

0 commit comments

Comments
 (0)