summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2024-12-14 17:53:57 +0100
committerGuillermo Ramos2024-12-14 17:53:57 +0100
commitb7bead38bfd32c0d179a7d68f362a8df82de0879 (patch)
tree754d950eafccd951e6aeaba4941b28da05180ec8
parent7f09510a3169c13a4f1181ff24f978d42fcf58f5 (diff)
downloadAoC-b7bead38bfd32c0d179a7d68f362a8df82de0879.tar.gz
Clippy
-rw-r--r--2024_rust/src/bin/day12.rs4
-rw-r--r--2024_rust/src/bin/day14.rs2
-rw-r--r--2024_rust/src/bin/day2.rs4
-rw-r--r--2024_rust/src/bin/day5.rs32
-rw-r--r--2024_rust/src/bin/day6.rs39
-rw-r--r--2024_rust/src/bin/day7.rs8
-rw-r--r--2024_rust/src/bin/day8.rs6
7 files changed, 42 insertions, 53 deletions
diff --git a/2024_rust/src/bin/day12.rs b/2024_rust/src/bin/day12.rs
index 4408980..d4a4cdf 100644
--- a/2024_rust/src/bin/day12.rs
+++ b/2024_rust/src/bin/day12.rs
@@ -38,7 +38,7 @@ fn region_v1(m: &Matrix, pos: matrix::Pos) -> (Price, HashSet<matrix::Pos>) {
let mut visited: HashSet<matrix::Pos> = HashSet::new();
let mut pending: HashSet<matrix::Pos> = [pos].into();
let mut new_pending: HashSet<matrix::Pos>;
- while pending.len() > 0 {
+ while !pending.is_empty() {
new_pending = HashSet::new();
for p in pending {
let adj = adj_same_region(m, p);
@@ -161,7 +161,7 @@ fn region(m: &Matrix, pos: matrix::Pos, version: u8) -> (Price, HashSet<matrix::
fn region_v2(m: &Matrix, pos: matrix::Pos) -> HashSet<matrix::Pos> {
let mut visited: HashSet<matrix::Pos> = HashSet::new();
let mut pending: HashSet<matrix::Pos> = [pos].into();
- while pending.len() > 0 {
+ while !pending.is_empty() {
let mut new_pending: HashSet<matrix::Pos> = HashSet::new();
for pos in pending {
// println!("Processing pos={pos:?}");
diff --git a/2024_rust/src/bin/day14.rs b/2024_rust/src/bin/day14.rs
index 55f986b..086ec04 100644
--- a/2024_rust/src/bin/day14.rs
+++ b/2024_rust/src/bin/day14.rs
@@ -89,7 +89,7 @@ impl Bathroom {
}
);
}
- println!("");
+ println!();
}
}
}
diff --git a/2024_rust/src/bin/day2.rs b/2024_rust/src/bin/day2.rs
index 92d288f..05d5b75 100644
--- a/2024_rust/src/bin/day2.rs
+++ b/2024_rust/src/bin/day2.rs
@@ -20,7 +20,7 @@ fn is_safe(levels: &[u32]) -> bool {
return false;
}
}
- return true;
+ true
}
fn p1(input: &str) -> String {
@@ -46,7 +46,7 @@ fn is_safe_with_dampener(levels: &[u32]) -> bool {
return true;
}
}
- return false;
+ false
}
fn p2(input: &str) -> String {
diff --git a/2024_rust/src/bin/day5.rs b/2024_rust/src/bin/day5.rs
index fe22295..9a97def 100644
--- a/2024_rust/src/bin/day5.rs
+++ b/2024_rust/src/bin/day5.rs
@@ -36,7 +36,7 @@ struct RuleStatus {
}
impl RuleStatus {
- fn new(rules: &Vec<Rule>) -> Self {
+ fn new(rules: &[Rule]) -> Self {
let mut previous = HashMap::new();
for &Rule { before, after } in rules.iter() {
match previous.get_mut(&after) {
@@ -80,8 +80,8 @@ fn update_from_str(s: &str) -> Update {
.collect()
}
-fn is_update_valid(update: &Update, rules: &Vec<Rule>) -> bool {
- let mut status = RuleStatus::new(&rules);
+fn is_update_valid(update: &Update, rules: &[Rule]) -> bool {
+ let mut status = RuleStatus::new(rules);
for page in update.iter() {
if !status.step(page) {
return false;
@@ -111,15 +111,15 @@ impl FromStr for Input {
};
// Read rules
- while let Some(l) = lines.next() {
- if l == "" {
+ for l in lines.by_ref() {
+ if l.is_empty() {
break;
}
input.rules.push(Rule::from_str(l).unwrap());
}
// Read updates
- while let Some(l) = lines.next() {
+ for l in lines.by_ref() {
input.updates.push(update_from_str(l));
}
@@ -130,8 +130,8 @@ impl FromStr for Input {
fn sum_valid_updates(Input { rules, updates }: &Input) -> u32 {
updates
.iter()
- .filter(|u| is_update_valid(u, &rules))
- .map(|u| middle_page(u))
+ .filter(|u| is_update_valid(u, rules))
+ .map(middle_page)
.sum()
}
@@ -141,16 +141,16 @@ fn p1(input: &str) -> String {
}
// Remove useless rules for this particular update
-fn filter_rules(rules: &Vec<Rule>, update: &Update) -> Vec<Rule> {
+fn filter_rules(rules: &[Rule], update: &Update) -> Vec<Rule> {
let updated: HashSet<&Page> = update.iter().collect();
rules
.iter()
- .cloned()
.filter(|r| updated.contains(&r.before))
+ .cloned()
.collect()
}
-fn sort_rules(rules: &Vec<Rule>) -> Vec<Rule> {
+fn sort_rules(rules: &[Rule]) -> Vec<Rule> {
let mut sorted: Vec<Rule> = vec![];
let mut afters: HashMap<Page, u8> = HashMap::new();
for a in rules.iter().map(|r| r.after) {
@@ -158,11 +158,11 @@ fn sort_rules(rules: &Vec<Rule>) -> Vec<Rule> {
}
// Allocate new "droppable" rules by wrapping the originals into Options
- let mut rules: Vec<Option<&Rule>> = rules.iter().map(|r| Some(r)).collect();
+ let mut rules: Vec<Option<&Rule>> = rules.iter().map(Some).collect();
while rules.iter().any(|x| x.is_some()) {
for rule in rules.iter_mut() {
let Some(r) = *rule else { continue };
- if let None = afters.get(&r.before) {
+ if !afters.contains_key(&r.before) {
sorted.push(r.clone());
match afters.get_mut(&r.after) {
None => {
@@ -183,7 +183,7 @@ fn sort_rules(rules: &Vec<Rule>) -> Vec<Rule> {
sorted
}
-fn update_reorder(update: &Update, rules: &Vec<Rule>) -> Option<Update> {
+fn update_reorder(update: &Update, rules: &[Rule]) -> Option<Update> {
let filtered_rules = filter_rules(rules, update);
let sorted_rules = sort_rules(&filtered_rules);
let mut result: Update = vec![];
@@ -208,8 +208,8 @@ fn update_reorder(update: &Update, rules: &Vec<Rule>) -> Option<Update> {
fn sum_reordered_updates(Input { rules, updates }: &Input) -> u32 {
updates
.iter()
- .filter(|u| !is_update_valid(u, &rules)) // drop valid updates
- .filter_map(|u| update_reorder(u, &rules)) // reorder updates
+ .filter(|u| !is_update_valid(u, rules)) // drop valid updates
+ .filter_map(|u| update_reorder(u, rules)) // reorder updates
.map(|u| middle_page(&u))
.sum()
}
diff --git a/2024_rust/src/bin/day6.rs b/2024_rust/src/bin/day6.rs
index 5c78781..d8a4580 100644
--- a/2024_rust/src/bin/day6.rs
+++ b/2024_rust/src/bin/day6.rs
@@ -75,14 +75,11 @@ impl M {
let mut visited = HashMap::new();
for i in 0..matrix.limit.0 {
for j in 0..matrix.limit.1 {
- match matrix.get((i, j)) {
- Guard(dir) => {
- let mut dirhs = HashSet::new();
- dirhs.insert(*dir);
- visited.insert(guard, dirhs);
- guard = (i, j);
- }
- _ => (),
+ if let Guard(dir) = matrix.get((i, j)) {
+ let mut dirhs = HashSet::new();
+ dirhs.insert(*dir);
+ visited.insert(guard, dirhs);
+ guard = (i, j);
}
}
}
@@ -140,10 +137,7 @@ impl M {
fn step_forever(&mut self) -> SimResult {
loop {
- match self.step() {
- Some(res) => return res,
- None => (),
- }
+ if let Some(res) = self.step() { return res }
}
}
@@ -165,7 +159,7 @@ impl M {
impl fmt::Display for M {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "{}[2J", 27 as char)?;
- write!(f, "{}", self.matrix.to_string())?;
+ write!(f, "{}", self.matrix)?;
write!(f, "[Guard position: {:?}]", self.guard)
}
}
@@ -184,18 +178,15 @@ fn count_loops(m: M) -> u32 {
// let mut simulations = 0;
for i in 0..m.matrix.limit.0 {
for j in 0..m.matrix.limit.1 {
- match m.matrix.get((i, j)) {
- Empty => {
- // simulations += 1;
- let mut m1 = m.clone();
- m1.matrix.set((i, j), Obstacle);
- // println!("Simulation {simulations} @ ({i},{j}) ({result})...");
- // println!("{m1}");
- if let SimResult::Loop = m1.step_forever() {
- result += 1;
- }
+ if let Empty = m.matrix.get((i, j)) {
+ // simulations += 1;
+ let mut m1 = m.clone();
+ m1.matrix.set((i, j), Obstacle);
+ // println!("Simulation {simulations} @ ({i},{j}) ({result})...");
+ // println!("{m1}");
+ if let SimResult::Loop = m1.step_forever() {
+ result += 1;
}
- _ => (),
}
}
}
diff --git a/2024_rust/src/bin/day7.rs b/2024_rust/src/bin/day7.rs
index d81ad1e..c8e0d17 100644
--- a/2024_rust/src/bin/day7.rs
+++ b/2024_rust/src/bin/day7.rs
@@ -77,11 +77,11 @@ impl Equation {
}
fn sum_solutions(input: &str, ops: &[Operator]) -> u64 {
- let equations: Vec<Equation> = input.lines().map(|l| Equation::parse(l)).collect();
+ let equations: Vec<Equation> = input.lines().map(Equation::parse).collect();
// println!("Equations: {equations:?}");
equations
.iter()
- .filter(|e| e.has_solution(&ops))
+ .filter(|e| e.has_solution(ops))
.map(|e| e.result)
.sum()
}
@@ -114,9 +114,7 @@ fn p2(input: &str) -> String {
Operator {
name: '|',
f: Box::new(|x, y| {
- format!("{}{}", x.to_string(), y.to_string())
- .parse()
- .unwrap()
+ format!("{}{}", x, y).parse().unwrap()
}),
},
];
diff --git a/2024_rust/src/bin/day8.rs b/2024_rust/src/bin/day8.rs
index cbdfb77..1b18907 100644
--- a/2024_rust/src/bin/day8.rs
+++ b/2024_rust/src/bin/day8.rs
@@ -61,7 +61,7 @@ impl M {
for (_freq, antennas) in self.antennas.iter() {
// println!("Freq {freq}: {antennas:?}");
for (a1, a2) in antennas
- .into_iter()
+ .iter()
.permutations(2)
.map(|x| x.into_iter().collect_tuple().unwrap())
{
@@ -91,7 +91,7 @@ fn antinodes(a1: &matrix::Pos, a2: &matrix::Pos, limit: &matrix::Pos) -> Vec<mat
let mut a: matrix::Pos = result[0];
let mut b: matrix::Pos = result[1];
while let Some(an) = antinode(&a, &b, limit) {
- result.push(an.clone());
+ result.push(an);
a = result[result.len() - 2];
b = result[result.len() - 1];
}
@@ -113,7 +113,7 @@ fn antinode(a1: &matrix::Pos, a2: &matrix::Pos, limit: &matrix::Pos) -> Option<m
impl fmt::Display for M {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "{}[2J", 27 as char)?;
- write!(f, "{}", self.matrix.to_string())
+ write!(f, "{}", self.matrix)
}
}