Advent of Code solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

day 2

Rain 24dd6755 4f52bc4d

+107
+7
2025/2/Cargo.lock
··· 1 + # This file is automatically @generated by Cargo. 2 + # It is not intended for manual editing. 3 + version = 4 4 + 5 + [[package]] 6 + name = "day2" 7 + version = "0.1.0"
+6
2025/2/Cargo.toml
··· 1 + [package] 2 + name = "day2" 3 + version = "0.1.0" 4 + edition = "2024" 5 + 6 + [dependencies]
+1
2025/2/input.txt
··· 1 + 6161588270-6161664791,128091420-128157776,306-494,510-1079,10977-20613,64552-123011,33-46,28076-52796,371150-418737,691122-766624,115-221,7426210-7504719,819350-954677,7713444-7877541,63622006-63661895,1370-1981,538116-596342,5371-8580,8850407-8965070,156363-325896,47-86,452615-473272,2012-4265,73181182-73335464,1102265-1119187,3343315615-3343342551,8388258268-8388317065,632952-689504,3-22,988344-1007943
+93
2025/2/src/main.rs
··· 1 + fn part1(ranges: Vec<(usize, usize)>) -> usize { 2 + let mut counter = 0; 3 + 4 + for (start, end) in ranges { 5 + for num in start..=end { 6 + let num_string = num.to_string(); 7 + let (fhalf, lhalf) = num_string.split_at(num_string.len() / 2); 8 + 9 + if fhalf == lhalf { 10 + counter += num 11 + } 12 + } 13 + } 14 + 15 + counter 16 + } 17 + 18 + fn part2(ranges: Vec<(usize, usize)>) -> usize { 19 + let mut counter = 0; 20 + 21 + for (start, end) in ranges { 22 + 'outer: for num in start..=end { 23 + let num_string = num.to_string(); 24 + let len = num_string.len(); 25 + 26 + // we'll only check up to the half way mark since after that it cant possibly repeat 27 + for i in 0..=(len / 2) { 28 + let substring_len = i + 1; 29 + // i kinda wanted to get rid of the second check but cant bc of nums with 2 digits 30 + if len % substring_len == 0 && len != substring_len { 31 + if num_string == num_string.get(0..=i).unwrap().repeat(len / substring_len) { 32 + counter += num; 33 + continue 'outer; 34 + } 35 + } 36 + } 37 + } 38 + } 39 + 40 + counter 41 + } 42 + 43 + fn main() { 44 + let file_contents = std::fs::read_to_string("./input.txt").expect("unable to read file"); 45 + let ranges: Vec<(usize, usize)> = file_contents 46 + .split(',') 47 + .map(|r| r.split_once('-').unwrap()) 48 + .map(|(lhs, rhs)| (lhs.parse::<usize>().unwrap(), rhs.parse::<usize>().unwrap())) 49 + .collect(); 50 + 51 + println!("Part 1 Results: {}", part1(ranges.clone())); 52 + println!("Part 2 Results: {}", part2(ranges)); 53 + } 54 + 55 + mod tests { 56 + #[test] 57 + fn part1() { 58 + let ranges = vec![ 59 + (11, 22), 60 + (95, 115), 61 + (998, 1012), 62 + (1188511880, 1188511890), 63 + (222220, 222224), 64 + (1698522, 1698528), 65 + (446443, 446449), 66 + (38593856, 38593862), 67 + (565653, 565659), 68 + (824824821, 824824827), 69 + (2121212118, 2121212124), 70 + ]; 71 + 72 + assert_eq!(super::part1(ranges), 1227775554); 73 + } 74 + 75 + #[test] 76 + fn part2() { 77 + let ranges = vec![ 78 + (11, 22), 79 + (95, 115), 80 + (998, 1012), 81 + (1188511880, 1188511890), 82 + (222220, 222224), 83 + (1698522, 1698528), 84 + (446443, 446449), 85 + (38593856, 38593862), 86 + (565653, 565659), 87 + (824824821, 824824827), 88 + (2121212118, 2121212124), 89 + ]; 90 + 91 + assert_eq!(super::part2(ranges), 4174379265); 92 + } 93 + }