day 5 part 1 done
This commit is contained in:
parent
2753263478
commit
11be4a7870
5 changed files with 1315 additions and 0 deletions
54
aoc-5/Cargo.lock
generated
Normal file
54
aoc-5/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aoc-5"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
|
||||
7
aoc-5/Cargo.toml
Normal file
7
aoc-5/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "aoc-5"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.12.2"
|
||||
11
aoc-5/src/example.txt
Normal file
11
aoc-5/src/example.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
1177
aoc-5/src/input.txt
Normal file
1177
aoc-5/src/input.txt
Normal file
File diff suppressed because it is too large
Load diff
66
aoc-5/src/main.rs
Normal file
66
aoc-5/src/main.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use std::fs;
|
||||
use regex::Regex;
|
||||
fn main() {
|
||||
println!("Lets begin, day 5");
|
||||
let file_path = "src/input.txt";
|
||||
// let file_path = "src/example.txt";
|
||||
|
||||
let re = Regex::new(r"(?<low>\d*)-(?<high>\d*)").unwrap();
|
||||
let mut ranges: Vec<(i64, i64)> = Vec::new();
|
||||
let mut fresh: Vec<i64> = Vec::new();
|
||||
let mut spoiled: Vec<i64> = Vec::new();
|
||||
let mut stage = 0;
|
||||
|
||||
for line in fs::read_to_string(file_path).unwrap().lines() {
|
||||
// create some sort of grd structure to hold all the symbols
|
||||
// also need to figure out how to deal with the edges
|
||||
|
||||
println!("{line}");
|
||||
if line == "" {
|
||||
//we've reached the gap betweent he ranges and the products list
|
||||
stage = 1;
|
||||
println!("Reading in ranges complete:");
|
||||
println!("{ranges:?}");
|
||||
}
|
||||
|
||||
if stage == 0{
|
||||
let mut range: Vec<(i64, i64)> = re.captures_iter(&line).map(|caps| {
|
||||
let start: i64 = caps.name("low").unwrap().as_str().parse::<i64>().unwrap();
|
||||
let end: i64 = caps.name("high").unwrap().as_str().parse::<i64>().unwrap();
|
||||
(start, end)
|
||||
}).collect();
|
||||
ranges.append(&mut range);
|
||||
}
|
||||
|
||||
if stage == 1 && line != "" {
|
||||
let product = line.parse::<i64>().unwrap();
|
||||
let mut checked = false;
|
||||
for check_range in &ranges{
|
||||
if product >= check_range.0 && product <= check_range.1 {
|
||||
// this one is fresh
|
||||
fresh.push(product);
|
||||
checked = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !checked {
|
||||
spoiled.push(product);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
println!("Fresh Products: {fresh:?}");
|
||||
println!("Spoiled Products: {spoiled:?}");
|
||||
let fresh_count = fresh.len();
|
||||
let spoiled_count = spoiled.len();
|
||||
println!("Fresh Count: {fresh_count:?}");
|
||||
println!("Spoiled Count: {spoiled_count:?}");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue