have started on day 2, might need some help with ranges?

This commit is contained in:
Tabby 2025-12-02 05:43:15 +00:00
parent 48e7da6d65
commit d835e286f7
5 changed files with 98 additions and 0 deletions

1
aoc-2/src/example.txt Normal file
View file

@ -0,0 +1 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124

1
aoc-2/src/input.txt Normal file
View file

@ -0,0 +1 @@
5959566378-5959623425,946263-1041590,7777713106-7777870316,35289387-35394603,400-605,9398763-9592164,74280544-74442206,85684682-85865536,90493-179243,202820-342465,872920-935940,76905692-76973065,822774704-822842541,642605-677786,3759067960-3759239836,1284-3164,755464-833196,52-128,3-14,30481-55388,844722790-844967944,83826709-83860070,9595933151-9595993435,4216-9667,529939-579900,1077949-1151438,394508-486310,794-1154,10159-17642,5471119-5683923,16-36,17797-29079,187-382

35
aoc-2/src/main.rs Normal file
View file

@ -0,0 +1,35 @@
// within certain rnages, find numbers with a pattern repeated exactly twice
// eg "55" ("5" twice), "6464" ("64" twice), "123123"
// cannot be matches on numebrs with an odd number of digits
// compare the first half of the didgits to the second half
// save all the matches (invalid id's) to a list and add them up to produce the solution
// each rnage can have 0 or more invalid id's
// need to parse each range (probably regex), get start and end value for each
use std::fs;
use regex::Regex;
fn main() {
println!("Lets begin, day 2, part 1");
let file_path = "src/input.txt";
// let file_path = "src/example.txt";
// split up input by "," and "-"
// (?<start_range>\d*)-(?<end_range>\d*)
let re = Regex::new(r"(?<start_range>\d*)-(?<end_range>\d*)").unwrap();
let line = fs::read_to_string(file_path).unwrap();
println!("{line}");
// let mut results = vec![];
// parse the provided ranges into paits
let ranges: Vec<(i32, i32)> = re.captures_iter(&line).map(|caps| {
let start: i32 = caps.name("start_range").unwrap().as_str().parse::<i32>().unwrap();
let end: i32 = caps.name("end_range").unwrap().as_str().parse::<i32>().unwrap();
(start, end)
}).collect();
println!("{ranges:?}");
}