tangled
alpha
login
or
join now
tymek.me
/
aoc25
1
fork
atom
this repo has no description
1
fork
atom
overview
issues
pulls
pipelines
day2: first impl
Tim Chmielecki
3 months ago
f2d07424
97aedc02
+67
-3
5 changed files
expand all
collapse all
unified
split
inputs
2.test
2.txt
src
main.rs
solutions
day2.rs
mod.rs
+1
inputs/2.test
···
1
1
+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
+1
inputs/2.txt
···
1
1
+
18623-26004,226779-293422,65855-88510,868-1423,248115026-248337139,903911-926580,97-121,67636417-67796062,24-47,6968-10197,193-242,3769-5052,5140337-5233474,2894097247-2894150301,979582-1016336,502-646,9132195-9191022,266-378,58-91,736828-868857,622792-694076,6767592127-6767717303,2920-3656,8811329-8931031,107384-147042,941220-969217,3-17,360063-562672,7979763615-7979843972,1890-2660,23170346-23308802
+5
-3
src/main.rs
···
1
1
mod solutions;
2
2
3
3
-
use solutions::Day1;
3
3
+
use solutions::Day2;
4
4
5
5
fn main() {
6
6
-
let day1 = Day1::new("inputs/1.txt");
7
7
-
println!("Part 1: {}, Part 2: {}", day1.part1(), day1.part2());
6
6
+
//let day1 = Day1::new("inputs/1.txt");
7
7
+
let day = Day2::new("inputs/2.txt");
8
8
+
println!("Part 1: {}", day.part1());
9
9
+
println!("Part 2: {}", day.part2());
8
10
}
+58
src/solutions/day2.rs
···
1
1
+
use std::fs;
2
2
+
3
3
+
pub struct Day2 {
4
4
+
data: String,
5
5
+
}
6
6
+
7
7
+
fn divisors_desc(n: usize) -> impl Iterator<Item = usize> {
8
8
+
(1..n).rev().filter(move |&i| n % i == 0)
9
9
+
}
10
10
+
11
11
+
fn invalid_id(id: &i64) -> bool {
12
12
+
let str_id = id.to_string();
13
13
+
if str_id.len() % 2 != 0 {
14
14
+
return false;
15
15
+
}
16
16
+
let (left, right) = str_id.split_at(str_id.len() / 2);
17
17
+
left == right
18
18
+
}
19
19
+
20
20
+
fn new_invalid_id(id: &i64) -> bool {
21
21
+
let str_id = id.to_string();
22
22
+
for div in divisors_desc(str_id.len()) {
23
23
+
let chars = str_id.chars().collect::<Vec<char>>();
24
24
+
let seqs = chars.chunks(div).collect::<Vec<_>>();
25
25
+
if seqs.iter().all(|&seq| seq == seqs[0]) {
26
26
+
return true;
27
27
+
}
28
28
+
}
29
29
+
false
30
30
+
}
31
31
+
32
32
+
impl Day2 {
33
33
+
pub fn new(path: &str) -> Self {
34
34
+
Self {
35
35
+
data: fs::read_to_string(path).expect("failed to read input file"),
36
36
+
}
37
37
+
}
38
38
+
39
39
+
pub fn part1(&self) -> i64 {
40
40
+
self.data.split(',').fold(0, |acc, range| {
41
41
+
let (n1, n2) = range.split_once('-').unwrap();
42
42
+
println!("Range: {}-{}", n1, n2);
43
43
+
let (n1, n2) = (n1.parse::<i64>().unwrap(), n2.parse::<i64>().unwrap());
44
44
+
println!("Range: {}-{}", n1, n2);
45
45
+
(n1..=n2).filter(|x| invalid_id(x)).sum::<i64>() + acc
46
46
+
})
47
47
+
}
48
48
+
49
49
+
pub fn part2(&self) -> i64 {
50
50
+
self.data.split(',').fold(0, |acc, range| {
51
51
+
let (n1, n2) = range.split_once('-').unwrap();
52
52
+
println!("Range: {}-{}", n1, n2);
53
53
+
let (n1, n2) = (n1.parse::<i64>().unwrap(), n2.parse::<i64>().unwrap());
54
54
+
println!("Range: {}-{}", n1, n2);
55
55
+
(n1..=n2).filter(|x| new_invalid_id(x)).sum::<i64>() + acc
56
56
+
})
57
57
+
}
58
58
+
}
+2
src/solutions/mod.rs
···
1
1
pub mod day1;
2
2
+
pub mod day2;
2
3
pub use day1::Day1;
4
4
+
pub use day2::Day2;