Compare commits
No commits in common. "49311e363769ec6840cd391c5adb20214b1eabaf" and "0c9f8224ec779fcc6aebbb3d47666b2945f134fa" have entirely different histories.
49311e3637
...
0c9f8224ec
17 changed files with 7 additions and 562 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -1,6 +0,0 @@
|
||||||
.idea
|
|
||||||
target
|
|
||||||
l1.json
|
|
||||||
l2.json
|
|
||||||
list.txt
|
|
||||||
input.txt
|
|
|
@ -1,14 +1,14 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut list1: Vec<i32> =
|
|
||||||
serde_json::from_reader(std::fs::File::open("l1.json").unwrap()).unwrap();
|
|
||||||
let mut list2: Vec<i32> =
|
let mut list1: Vec<i32> = serde_json::from_reader(std::fs::File::open("l1.json").unwrap()).unwrap();
|
||||||
serde_json::from_reader(std::fs::File::open("l2.json").unwrap()).unwrap();
|
let mut list2: Vec<i32> = serde_json::from_reader(std::fs::File::open("l2.json").unwrap()).unwrap();
|
||||||
|
|
||||||
list1.sort();
|
list1.sort();
|
||||||
list2.sort();
|
list2.sort();
|
||||||
|
|
||||||
if list1.len() != list2.len() {
|
if list1.len() != list2.len() {
|
||||||
panic!("your lists are broken. The length is not equal.")
|
panic!("your lists are fucked")
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut totaldiff: i64 = 0;
|
let mut totaldiff: i64 = 0;
|
||||||
|
@ -17,7 +17,8 @@ fn main() {
|
||||||
let int2 = list2[i];
|
let int2 = list2[i];
|
||||||
if int1 > int2 {
|
if int1 > int2 {
|
||||||
totaldiff += (int1 - int2) as i64;
|
totaldiff += (int1 - int2) as i64;
|
||||||
} else if int1 < int2 {
|
}
|
||||||
|
else if int1 < int2 {
|
||||||
totaldiff += (int2 - int1) as i64;
|
totaldiff += (int2 - int1) as i64;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
96
2024/day1/part2/Cargo.lock
generated
96
2024/day1/part2/Cargo.lock
generated
|
@ -1,96 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "itoa"
|
|
||||||
version = "1.0.14"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "memchr"
|
|
||||||
version = "2.7.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "part2"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
"serde_json",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro2"
|
|
||||||
version = "1.0.92"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
|
|
||||||
dependencies = [
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "quote"
|
|
||||||
version = "1.0.37"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ryu"
|
|
||||||
version = "1.0.18"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde"
|
|
||||||
version = "1.0.215"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f"
|
|
||||||
dependencies = [
|
|
||||||
"serde_derive",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde_derive"
|
|
||||||
version = "1.0.215"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde_json"
|
|
||||||
version = "1.0.133"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377"
|
|
||||||
dependencies = [
|
|
||||||
"itoa",
|
|
||||||
"memchr",
|
|
||||||
"ryu",
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "syn"
|
|
||||||
version = "2.0.90"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-ident"
|
|
||||||
version = "1.0.14"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
|
|
@ -1,8 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "part2"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
serde = "1.0.215"
|
|
||||||
serde_json = "1.0.133"
|
|
|
@ -1,19 +0,0 @@
|
||||||
fn main() {
|
|
||||||
let mut list1: Vec<i32> =
|
|
||||||
serde_json::from_reader(std::fs::File::open("l1.json").unwrap()).unwrap();
|
|
||||||
let mut list2: Vec<i32> =
|
|
||||||
serde_json::from_reader(std::fs::File::open("l2.json").unwrap()).unwrap();
|
|
||||||
|
|
||||||
let mut simScoreTotal: i64 = 0;
|
|
||||||
|
|
||||||
for i in &list1 {
|
|
||||||
let mut totalsRight: i32 = 0;
|
|
||||||
for j in &list2 {
|
|
||||||
if i == j {
|
|
||||||
totalsRight += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
simScoreTotal += (totalsRight * i) as i64;
|
|
||||||
}
|
|
||||||
println!("{simScoreTotal}")
|
|
||||||
}
|
|
7
2024/day2/part1/Cargo.lock
generated
7
2024/day2/part1/Cargo.lock
generated
|
@ -1,7 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "part1"
|
|
||||||
version = "0.1.0"
|
|
|
@ -1,6 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "part1"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
|
@ -1,78 +0,0 @@
|
||||||
use std::fs::read_to_string;
|
|
||||||
use std::thread::current;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// read file input
|
|
||||||
let lines: Vec<String> = read_to_string("input.txt")
|
|
||||||
.unwrap()
|
|
||||||
.lines()
|
|
||||||
.map(String::from)
|
|
||||||
.collect();
|
|
||||||
// current safe total count
|
|
||||||
let mut safecount = 0;
|
|
||||||
// for each line
|
|
||||||
for i in lines {
|
|
||||||
println!("{i}");
|
|
||||||
// for each line, split into a Vec<String> of the values
|
|
||||||
let mut frfrstr: Vec<String> = i.split_whitespace().map(|s| s.parse().unwrap()).collect();
|
|
||||||
// convert that Vec<String> into Vec<Int>
|
|
||||||
let mut frfr: Vec<i64> = Vec::new();
|
|
||||||
for j1 in frfrstr {
|
|
||||||
let j2: i64 = j1.parse::<i64>().unwrap();
|
|
||||||
frfr.push(j2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// for each line, check if safe. Start false. If it ends up being fine then it'll be set to true.
|
|
||||||
let mut safemini = false;
|
|
||||||
|
|
||||||
// sorted regular and reverse to check condition 1
|
|
||||||
let mut sort1 = frfr.clone();
|
|
||||||
sort1.sort();
|
|
||||||
let mut sort2 = sort1.clone();
|
|
||||||
sort2.reverse();
|
|
||||||
if frfr == sort1 || frfr == sort2 {
|
|
||||||
let mut mode = 0;
|
|
||||||
let tempval0 = frfr[*&0];
|
|
||||||
let tempval1 = frfr[*&1];
|
|
||||||
if tempval0 < tempval1 {
|
|
||||||
println!("increasing mode");
|
|
||||||
mode = 1;
|
|
||||||
} else if tempval0 > tempval1 {
|
|
||||||
println!("decreasing mode");
|
|
||||||
mode = -1;
|
|
||||||
} else if tempval0 == tempval1 {
|
|
||||||
println!("its equal and so its discarded");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// condition 2 but only if cond 1 passes
|
|
||||||
// starts true, becomes invalid
|
|
||||||
let mut currentcheckstate = true;
|
|
||||||
'fr1: for i2 in 1..frfr.len() {
|
|
||||||
let realdiff = frfr[*&i2 - 1] - (frfr[*&i2]);
|
|
||||||
if mode == 1 {
|
|
||||||
if realdiff > -1 || realdiff < -3 {
|
|
||||||
println!("realdiff {realdiff} plus");
|
|
||||||
currentcheckstate = false;
|
|
||||||
println!("abs diff averted");
|
|
||||||
}
|
|
||||||
} else if mode == -1 {
|
|
||||||
if realdiff > 3 || realdiff < 1 {
|
|
||||||
println!("realdiff {realdiff} minus");
|
|
||||||
currentcheckstate = false;
|
|
||||||
println!("abs diff averted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if currentcheckstate {
|
|
||||||
safemini = true;
|
|
||||||
println!("allowed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if safemini {
|
|
||||||
safecount += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println!("{safecount}")
|
|
||||||
}
|
|
7
2024/day2/part2/Cargo.lock
generated
7
2024/day2/part2/Cargo.lock
generated
|
@ -1,7 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "part1"
|
|
||||||
version = "0.1.0"
|
|
|
@ -1,6 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "part1"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
|
@ -1,84 +0,0 @@
|
||||||
use std::fs::read_to_string;
|
|
||||||
use std::thread::current;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// read file input
|
|
||||||
let lines: Vec<String> = read_to_string("input.txt")
|
|
||||||
.unwrap()
|
|
||||||
.lines()
|
|
||||||
.map(String::from)
|
|
||||||
.collect();
|
|
||||||
// current safe total count
|
|
||||||
let mut safecount = 0;
|
|
||||||
// for each line
|
|
||||||
for i in lines {
|
|
||||||
println!("{i}");
|
|
||||||
// for each line, split into a Vec<String> of the values
|
|
||||||
let mut frfrstr: Vec<String> = i.split_whitespace().map(|s| s.parse().unwrap()).collect();
|
|
||||||
// convert that Vec<String> into Vec<Int>
|
|
||||||
let mut frfrfr: Vec<i64> = Vec::new();
|
|
||||||
for j1 in frfrstr {
|
|
||||||
let j2: i64 = j1.parse::<i64>().unwrap();
|
|
||||||
frfrfr.push(j2);
|
|
||||||
}
|
|
||||||
// for each line, check if safe. Start false. If it ends up being fine then it'll be set to true.
|
|
||||||
let mut safemini = false;
|
|
||||||
|
|
||||||
for i in 0..frfrfr.len() {
|
|
||||||
let mut frfr: Vec<i64> = frfrfr.clone();
|
|
||||||
if i != frfr.len() {
|
|
||||||
frfr.remove(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
// sorted regular and reverse to check condition 1
|
|
||||||
let mut sort1 = frfr.clone();
|
|
||||||
sort1.sort();
|
|
||||||
let mut sort2 = sort1.clone();
|
|
||||||
sort2.reverse();
|
|
||||||
if frfr == sort1 || frfr == sort2 {
|
|
||||||
let mut mode = 0;
|
|
||||||
let tempval0 = frfr[*&0];
|
|
||||||
let tempval1 = frfr[*&1];
|
|
||||||
if tempval0 < tempval1 {
|
|
||||||
println!("increasing mode");
|
|
||||||
mode = 1;
|
|
||||||
} else if tempval0 > tempval1 {
|
|
||||||
println!("decreasing mode");
|
|
||||||
mode = -1;
|
|
||||||
} else if tempval0 == tempval1 {
|
|
||||||
println!("its equal and so its discarded");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// condition 2 but only if cond 1 passes
|
|
||||||
// starts true, becomes invalid
|
|
||||||
let mut currentcheckstate = true;
|
|
||||||
'fr1: for i2 in 1..frfr.len() {
|
|
||||||
let realdiff = frfr[*&i2 - 1] - (frfr[*&i2]);
|
|
||||||
if mode == 1 {
|
|
||||||
if realdiff > -1 || realdiff < -3 {
|
|
||||||
println!("realdiff {realdiff} plus");
|
|
||||||
currentcheckstate = false;
|
|
||||||
println!("abs diff averted");
|
|
||||||
}
|
|
||||||
} else if mode == -1 {
|
|
||||||
if realdiff > 3 || realdiff < 1 {
|
|
||||||
println!("realdiff {realdiff} minus");
|
|
||||||
currentcheckstate = false;
|
|
||||||
println!("abs diff averted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if currentcheckstate {
|
|
||||||
safemini = true;
|
|
||||||
println!("allowed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if safemini {
|
|
||||||
safecount += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println!("{safecount}")
|
|
||||||
}
|
|
54
2024/day3/part1/Cargo.lock
generated
54
2024/day3/part1/Cargo.lock
generated
|
@ -1,54 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "aho-corasick"
|
|
||||||
version = "1.1.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
|
||||||
dependencies = [
|
|
||||||
"memchr",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "day3"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"regex",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "memchr"
|
|
||||||
version = "2.7.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex"
|
|
||||||
version = "1.11.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
|
||||||
dependencies = [
|
|
||||||
"aho-corasick",
|
|
||||||
"memchr",
|
|
||||||
"regex-automata",
|
|
||||||
"regex-syntax",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex-automata"
|
|
||||||
version = "0.4.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
|
||||||
dependencies = [
|
|
||||||
"aho-corasick",
|
|
||||||
"memchr",
|
|
||||||
"regex-syntax",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex-syntax"
|
|
||||||
version = "0.8.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
|
|
@ -1,7 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "day3"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
regex = "1.11.1"
|
|
|
@ -1,48 +0,0 @@
|
||||||
use regex::Regex;
|
|
||||||
use std::fs::read_to_string;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let lines: Vec<String> = read_to_string("input.txt")
|
|
||||||
.unwrap()
|
|
||||||
.lines()
|
|
||||||
.map(String::from)
|
|
||||||
.collect();
|
|
||||||
let pattern1 = Regex::new(r"mul\((\d+,\d+)\)").unwrap();
|
|
||||||
let pattern2 = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap();
|
|
||||||
|
|
||||||
let mut finalvec1 = mul_parse(lines, pattern1);
|
|
||||||
let finalint = mul_process(finalvec1,pattern2);
|
|
||||||
println!("{:?}",finalint)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn mul_parse(thelines: Vec<String>, pattern: Regex) -> Vec<String>{
|
|
||||||
let mut mulvec: Vec<String> = Vec::new();
|
|
||||||
for i in thelines {
|
|
||||||
let tempvec: Vec<String> =
|
|
||||||
pattern.find_iter(&i)
|
|
||||||
.filter_map(|fr| fr.as_str().parse::<String>().ok()).collect();
|
|
||||||
|
|
||||||
for i2 in tempvec {
|
|
||||||
mulvec.push(i2);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
mulvec
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
|
|
||||||
let mut mulint: i64 = 0;
|
|
||||||
for i in mulvec {
|
|
||||||
let captures = pattern.captures(&i).unwrap();
|
|
||||||
let capture1 = captures.get(1).unwrap().as_str().parse::<i32>().unwrap();
|
|
||||||
|
|
||||||
let capture2 = captures.get(2).unwrap().as_str().parse::<i32>().unwrap();
|
|
||||||
mulint += (capture1 * capture2) as i64;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mulint
|
|
||||||
|
|
||||||
}
|
|
54
2024/day3/part2/Cargo.lock
generated
54
2024/day3/part2/Cargo.lock
generated
|
@ -1,54 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "aho-corasick"
|
|
||||||
version = "1.1.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
|
||||||
dependencies = [
|
|
||||||
"memchr",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "day3"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"regex",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "memchr"
|
|
||||||
version = "2.7.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex"
|
|
||||||
version = "1.11.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
|
||||||
dependencies = [
|
|
||||||
"aho-corasick",
|
|
||||||
"memchr",
|
|
||||||
"regex-automata",
|
|
||||||
"regex-syntax",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex-automata"
|
|
||||||
version = "0.4.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
|
||||||
dependencies = [
|
|
||||||
"aho-corasick",
|
|
||||||
"memchr",
|
|
||||||
"regex-syntax",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex-syntax"
|
|
||||||
version = "0.8.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
|
|
@ -1,7 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "day3"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
regex = "1.11.1"
|
|
|
@ -1,69 +0,0 @@
|
||||||
use regex::Regex;
|
|
||||||
use std::fs::read_to_string;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let lines: Vec<String> = read_to_string("input.txt")
|
|
||||||
.unwrap()
|
|
||||||
.lines()
|
|
||||||
.map(String::from)
|
|
||||||
.collect();
|
|
||||||
let pattern1 = Regex::new(r"(mul\((\d+)\,(\d+)\))|(do\(\))|(don\'t\(\))").unwrap();
|
|
||||||
let finalvec1 = mul_parse(lines, pattern1.clone());
|
|
||||||
let finalint = mul_process(finalvec1,pattern1.clone());
|
|
||||||
// println!("{:?}",finalint)
|
|
||||||
println!("{finalint}")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn mul_parse(thelines: Vec<String>, pattern: Regex) -> Vec<String>{
|
|
||||||
let mut mulvec: Vec<String> = Vec::new();
|
|
||||||
for i in thelines {
|
|
||||||
let tempvec: Vec<String> =
|
|
||||||
pattern.find_iter(&i)
|
|
||||||
.filter_map(|fr| fr.as_str().parse::<String>().ok()).collect();
|
|
||||||
|
|
||||||
for i2 in tempvec {
|
|
||||||
mulvec.push(i2);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
println!("mulvec is {:?}",mulvec);
|
|
||||||
mulvec
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
|
|
||||||
let mut enabled = true;
|
|
||||||
let mut mulint: i64 = 0;
|
|
||||||
let matchmul = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap();
|
|
||||||
let matchdo = Regex::new(r"do\(\)").unwrap();
|
|
||||||
let matchdont = Regex::new(r"don\'t\(\)").unwrap();
|
|
||||||
for i in mulvec {
|
|
||||||
if let Some(t) = matchdo.find(&i) {
|
|
||||||
println!("matchdo");
|
|
||||||
enabled = true;
|
|
||||||
}
|
|
||||||
else if let Some(t) = matchdont.find(&i) {
|
|
||||||
println!("matchdont");
|
|
||||||
enabled = false;
|
|
||||||
}
|
|
||||||
else if let Some(t) = matchmul.find(&i) {
|
|
||||||
if enabled {
|
|
||||||
println!("matchmul");
|
|
||||||
let captures = pattern.captures(&i).unwrap();
|
|
||||||
let capture1 = captures.get(2).unwrap().as_str().parse::<i32>().unwrap();
|
|
||||||
let capture2 = captures.get(3).unwrap().as_str().parse::<i32>().unwrap();
|
|
||||||
mulint += (capture1 * capture2) as i64;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mulint
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in a new issue