summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2024-12-03 10:23:57 +0100
committernekineki <nekineki@nekineki.net>2024-12-03 10:23:57 +0100
commitea583b8c165d9678e75c881e62beb828dac5dd5f (patch)
tree9b5326772a4c803e70cad48d9e21e33b6f199735
parentb503d9bdd545232501ca9a34623edb8a324d49ae (diff)
update day02, day03
-rw-r--r--2024/Cargo.lock16
-rw-r--r--2024/Cargo.toml1
-rw-r--r--2024/day02.rs6
-rw-r--r--2024/day03.rs39
4 files changed, 38 insertions, 24 deletions
diff --git a/2024/Cargo.lock b/2024/Cargo.lock
index 40500a8..60f63f7 100644
--- a/2024/Cargo.lock
+++ b/2024/Cargo.lock
@@ -15,6 +15,7 @@ dependencies = [
name = "aoc2024"
version = "0.1.0"
dependencies = [
+ "itertools",
"num",
"regex",
]
@@ -26,6 +27,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
+name = "either"
+version = "1.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
+
+[[package]]
+name = "itertools"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
+dependencies = [
+ "either",
+]
+
+[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/2024/Cargo.toml b/2024/Cargo.toml
index 4692fdf..0994cbc 100644
--- a/2024/Cargo.toml
+++ b/2024/Cargo.toml
@@ -9,6 +9,7 @@ edition = "2021"
opt-level = 3
[dependencies]
+itertools = "0.13.0"
num = "0.4"
regex = "1.11.1"
# nalgebra = "0.32.3"
diff --git a/2024/day02.rs b/2024/day02.rs
index b573d5c..798df97 100644
--- a/2024/day02.rs
+++ b/2024/day02.rs
@@ -4,10 +4,8 @@
use std::env;
use std::fs::File;
use std::io::Read;
+use itertools::Itertools;
-fn is_all_same(arr: &[bool]) -> bool {
- arr.windows(2).all(|w| w[0] == w[1])
-}
fn is_safe(arr: &[i32]) -> bool {
let diff: Vec<i32> = arr.windows(2).map(|w| w[0] - w[1]).collect();
@@ -15,7 +13,7 @@ fn is_safe(arr: &[i32]) -> bool {
.iter()
.map(|a| (3 >= a.abs()) && (a.abs() >= 1))
.all(|a| a);
- let sign_ok = is_all_same(&diff.iter().map(|a| *a > 0).collect::<Vec<_>>());
+ let sign_ok = diff.iter().map(|a| *a > 0).all_equal();
inc_ok && sign_ok
}
diff --git a/2024/day03.rs b/2024/day03.rs
index 926329c..427ff74 100644
--- a/2024/day03.rs
+++ b/2024/day03.rs
@@ -18,33 +18,32 @@ fn main() {
f.read_to_string(&mut content).expect("cannot read file");
let lines = content.trim_end().split("\n");
- let re = Regex::new(r"mul\((?<a>[0-9]+),(?<b>[0-9]+)\)").unwrap();
- let mut res1 = 0;
- for line in lines.clone() {
- res1 += re
- .captures_iter(line)
- .map(|caps| {
- let p = |name| caps.name(name).unwrap().as_str().parse::<i32>().unwrap();
- p("a") * p("b")
- })
- .fold(0, |a, b| a + b);
- }
+ let re = Regex::new(r"mul\(([0-9]+),([0-9]+)\)").unwrap();
+ let res1 = lines
+ .clone()
+ .map(|line| {
+ re.captures_iter(line)
+ .map(|caps| {
+ let p = |n| caps.get(n).unwrap().as_str().parse::<i32>().unwrap();
+ p(1) * p(2)
+ })
+ .sum::<i32>()
+ })
+ .sum::<i32>();
- let re_m = Regex::new(r"^mul\((?<a>[0-9]+),(?<b>[0-9]+)\)").unwrap();
- let re_do = Regex::new(r"^do\(\)").unwrap();
- let re_dont = Regex::new(r"^don't\(\)").unwrap();
+ let re2 = Regex::new(r"^mul\(([0-9]+),([0-9]+)\)").unwrap();
let mut res2 = 0;
let mut valid = true;
for line in lines {
for i in 0..line.len() {
- if re_do.is_match(&line[i..]) {
+ if line[i..].starts_with("do()") {
valid = true;
- } else if re_dont.is_match(&line[i..]) {
+ } else if line[i..].starts_with("don't()") {
valid = false;
- } else if valid && re_m.is_match(&line[i..]) {
- let cap = re_m.captures(&line[i..]).unwrap();
- let p = |name| cap.name(name).unwrap().as_str().parse::<i32>().unwrap();
- res2 += p("a") * p("b");
+ } else if valid && re2.is_match(&line[i..]) {
+ let cap = re2.captures(&line[i..]).unwrap();
+ let p = |n| cap.get(n).unwrap().as_str().parse::<i32>().unwrap();
+ res2 += p(1) * p(2);
}
}
}