day 2b solved!
This commit is contained in:
parent
b9dc620b95
commit
8e0b080f3b
6 changed files with 213 additions and 2 deletions
54
aoc-2b/Cargo.lock
generated
Normal file
54
aoc-2b/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-2b"
|
||||
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-2b/Cargo.toml
Normal file
7
aoc-2b/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "aoc-2b"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.12.2"
|
||||
1
aoc-2b/src/example.txt
Normal file
1
aoc-2b/src/example.txt
Normal 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-2b/src/input.txt
Normal file
1
aoc-2b/src/input.txt
Normal 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
|
||||
148
aoc-2b/src/main.rs
Normal file
148
aoc-2b/src/main.rs
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
// 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 invalids: Vec<i64> = Vec::new();
|
||||
|
||||
// parse the provided ranges into paits
|
||||
let ranges: Vec<(&str, &str)> = re.captures_iter(&line).map(|caps| {
|
||||
let start = caps.name("start_range").unwrap().as_str();
|
||||
let end = caps.name("end_range").unwrap().as_str();
|
||||
(start, end)
|
||||
}).collect();
|
||||
|
||||
println!("{ranges:?}");
|
||||
for range in ranges{
|
||||
println!("working on: {range:?}");
|
||||
let start = range.0.parse::<i64>().unwrap();
|
||||
let end = range.1.parse::<i64>().unwrap();
|
||||
// println!("{range.0:?} --> {end}");
|
||||
let mut count :i64 = start;
|
||||
while count <= end{
|
||||
// if odd number of didgits, then no match, skip to next loop
|
||||
// if even number, then compare the first and second half
|
||||
let current = count.to_string();
|
||||
|
||||
if find_invalid(current){
|
||||
invalids.push(count);
|
||||
}
|
||||
|
||||
// if current.len() % 2 != 1{
|
||||
// let half_len = current.len()/2;
|
||||
// let half_a = ¤t[..half_len];
|
||||
// let half_b = ¤t[half_len..];
|
||||
|
||||
// let current_str = current.to_string();
|
||||
// println!("{current_str} = {half_a} + {half_b}");
|
||||
|
||||
// if half_a == half_b{
|
||||
// invalids.push(count);
|
||||
// }
|
||||
// }
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("{invalids:?}");
|
||||
let mut solution: i64 = 0;
|
||||
for result in invalids{
|
||||
solution += result;
|
||||
}
|
||||
println!("Puzzle Solution: {solution}")
|
||||
// Correct :3
|
||||
|
||||
}
|
||||
|
||||
fn find_invalid(text : String) -> bool{
|
||||
// let mut invalid = false;
|
||||
// 1x matches
|
||||
if text.len() > 1{
|
||||
let one = text.chars().nth(0).unwrap();
|
||||
let mut ones_match = true;
|
||||
for char in text.chars(){
|
||||
if char == one{
|
||||
// good
|
||||
}
|
||||
else{
|
||||
ones_match = false;
|
||||
}
|
||||
}
|
||||
if ones_match{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 2x matches
|
||||
if text.len() == 4{
|
||||
if &text[0..2] == &text[2..4]{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if text.len() == 6{
|
||||
if &text[0..2] == &text[2..4] && &text[0..2] == &text[4..6]{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if text.len() == 8{
|
||||
if &text[0..2] == &text[2..4] && &text[0..2] == &text[4..6] && &text[0..2] == &text[6..8]{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if text.len() == 10{
|
||||
if &text[0..2] == &text[2..4] && &text[0..2] == &text[4..6] && &text[0..2] == &text[6..8] && &text[0..2] == &text[8..10]{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 3x matches
|
||||
if text.len() == 6 {
|
||||
if &text[0..3] == &text[3..6]{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if text.len() == 9 {
|
||||
if &text[0..3] == &text[3..6] && &text[0..3] == &text[6..9]{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 4x matches
|
||||
if text.len() == 8 {
|
||||
if &text[0..4] == &text[4..8]{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 5x matches
|
||||
if text.len() == 10{
|
||||
if &text[0..5] == &text[5..10]{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false; // represents no match
|
||||
}
|
||||
|
||||
// tried: 36862281460 - too high
|
||||
// solved! second attempt
|
||||
|
|
@ -4,10 +4,10 @@ This year I'll be trying to do it in Rust
|
|||
### 2026 Calendar
|
||||
```
|
||||
1 2 3 4 5 6
|
||||
[**] [ ] [ ] [ ] [ ] [ ]
|
||||
[**] [**] [ ] [ ] [ ] [ ]
|
||||
|
||||
7 8 9 10 11 12
|
||||
[ ] [ ] [ ] [ ] [ ] [ ]
|
||||
|
||||
~~ < 2/24 Stars Collected > ~~
|
||||
~~ < 4/24 Stars Collected > ~~
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue