diff --git a/aoc2023/src/day01.rs b/aoc2023/src/day01.rs index 02e03e8..89634d7 100644 --- a/aoc2023/src/day01.rs +++ b/aoc2023/src/day01.rs @@ -5,7 +5,7 @@ pub struct Day01; impl Solution for Day01 { fn part_1(&self, input: &str) -> Answer { - let words: Vec<&str> = input.split("\n") + let words: Vec<&str> = input.split('\n') .collect(); let mut sum: u64 = 0; @@ -14,9 +14,9 @@ impl Solution for Day01 { let mut first: Option = None; let mut last: Option = None; for char in word.chars() { - if char.is_digit(10) { + if char.is_ascii_digit() { last = Some(char.to_digit(10).unwrap().into()); - if first == None { + if first.is_none() { first = Some(char.to_digit(10).unwrap().into()); } } @@ -29,7 +29,7 @@ impl Solution for Day01 { } fn part_2(&self, input: &str) -> Answer { - let words: Vec<&str> = input.split("\n") + let words: Vec<&str> = input.split('\n') .collect(); let first = Regex::new( @@ -40,7 +40,7 @@ impl Solution for Day01 { ).unwrap(); Answer::from(words.iter() - .map(|word| {10 * matches(&first.captures(&word).unwrap()) + matches(&last.captures(&word).unwrap())}) + .map(|word| {10 * matches(&first.captures(&word).unwrap()) + matches(&last.captures(word).unwrap())}) .collect::>() .iter() .sum::()) @@ -49,10 +49,7 @@ impl Solution for Day01 { fn matches(cap: ®ex::Captures) -> u64 { for i in 1..10 { - match cap.get(i) { - Some(_) => return i as u64, - None => (), - }; + if cap.get(i).is_some() { return i as u64 }; } return 0 } diff --git a/aoc2023/src/day02.rs b/aoc2023/src/day02.rs index 4a1021a..cce8b88 100644 --- a/aoc2023/src/day02.rs +++ b/aoc2023/src/day02.rs @@ -41,14 +41,14 @@ impl Solution for Day02 { } fn part_2(&self, input: &str) -> Answer { - let games: Vec<&str> = input.split("\n") + let games: Vec<&str> = input.split('\n') .collect(); let re = Regex::new(r"(\d+) (\w+)").unwrap(); let mut sum: u64 = 0; - for (i, game) in games.iter().enumerate() { + for (_, game) in games.iter().enumerate() { let mut min_red: u64 = 0; let mut min_blue: u64 = 0; let mut min_green: u64 = 0; diff --git a/aoc2023/src/day03.rs b/aoc2023/src/day03.rs index e6bfed0..e13e791 100644 --- a/aoc2023/src/day03.rs +++ b/aoc2023/src/day03.rs @@ -6,7 +6,7 @@ pub struct Day03; impl Solution for Day03 { fn part_1(&self, input: &str) -> Answer { - let lines: Vec<&str> = input.split("\n").collect(); + let lines: Vec<&str> = input.split('\n').collect(); let mut out: u64 = 0; diff --git a/aoc2023/src/day05.rs b/aoc2023/src/day05.rs index f29af25..5567f84 100644 --- a/aoc2023/src/day05.rs +++ b/aoc2023/src/day05.rs @@ -43,7 +43,7 @@ impl Solution for Day05 { let blocks: Vec<&str> = input.split("\n\n") .collect(); - let mut seeds: Vec = blocks[0].split(" ") + let mut seeds: Vec = blocks[0].split(' ') .skip(1) .collect::>() .chunks(2) @@ -53,9 +53,9 @@ impl Solution for Day05 { let rule_blocks: Vec> = blocks.iter() .skip(1) .map( - |block| block.split("\n").skip(1).map( + |block| block.split('\n').skip(1).map( |line| { - let l: Vec<&str> = line.split(" ").collect(); + let l: Vec<&str> = line.split(' ').collect(); Rule::from_rules(l[0].parse::().unwrap(), l[1].parse::().unwrap(), l[2].parse::().unwrap()) } ).collect() @@ -138,7 +138,7 @@ impl Rule { } fn from_rules(dest: i64, source: i64, range: i64) -> Rule { - Rule {start: source, end: source + range - 1, offset: (dest - source) as i64} + Rule {start: source, end: source + range - 1, offset: (dest - source)} } fn apply(&self, seeds: &SeedRange) -> Option<(Option, SeedRange, Option)> { diff --git a/aoc2023/src/day06.rs b/aoc2023/src/day06.rs index c7dabfd..74d7844 100644 --- a/aoc2023/src/day06.rs +++ b/aoc2023/src/day06.rs @@ -5,7 +5,7 @@ pub struct Day06; impl Solution for Day06 { fn part_1(&self, input: &str) -> Answer { - let data: Vec<&str> = input.split("\n").collect(); + let data: Vec<&str> = input.split('\n').collect(); let times: Vec = data[0].split_whitespace().skip(1).map(|x| x.parse::().unwrap()).collect(); let dists: Vec = data[1].split_whitespace().skip(1).map(|x| x.parse::().unwrap()).collect(); @@ -23,7 +23,7 @@ impl Solution for Day06 { } fn part_2(&self, input: &str) -> Answer { - let data: Vec<&str> = input.split("\n").collect(); + let data: Vec<&str> = input.split('\n').collect(); let time: u64 = data[0].split_whitespace().skip(1).collect::>().join("").parse::().unwrap(); let dist: u64 = data[1].split_whitespace().skip(1).collect::>().join("").parse::().unwrap(); diff --git a/aoc2023/src/day07.rs b/aoc2023/src/day07.rs index 41076c8..5e7155a 100644 --- a/aoc2023/src/day07.rs +++ b/aoc2023/src/day07.rs @@ -103,20 +103,14 @@ fn compare2(hand1: &(&str, u64), hand2: &(&str, u64)) -> Ordering { let val = o[&'J']; o.remove(&'J'); let max_key = o.iter().max_by(|a, b| a.1.cmp(&b.1)).unwrap_or((&'A', &0)).0; - o.insert(*max_key, match o.get(&max_key) { - Some(x) => x, - None => &0 - } + val); + o.insert(*max_key, o.get(max_key).unwrap_or(&0) + val); } if s.contains_key(&'J') { let val = s[&'J']; s.remove(&'J'); let max_key = s.iter().max_by(|a, b| a.1.cmp(&b.1)).unwrap_or((&'A', &0)).0; - s.insert(*max_key, match s.get(&max_key) { - Some(x) => x, - None => &0 - } + val); + s.insert(*max_key, s.get(max_key).unwrap_or(&0) + val); } if ( s.keys().count() < o.keys().count() ) | @@ -124,7 +118,7 @@ fn compare2(hand1: &(&str, u64), hand2: &(&str, u64)) -> Ordering { else if ( s.keys().count() > o.keys().count() ) | ( s.values().max() < o.values().max() ) { return Ordering::Less } - let ordering = vec!['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J']; + let ordering = ['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J']; for (i, j) in zip(hand1.0.chars(), hand2.0.chars()) { if ordering.iter().position(|&x| x==i) < ordering.iter().position(|&x| x==j) { @@ -134,5 +128,5 @@ fn compare2(hand1: &(&str, u64), hand2: &(&str, u64)) -> Ordering { } } - return Ordering::Equal + Ordering::Equal } diff --git a/aoc2023/src/day08.rs b/aoc2023/src/day08.rs index 819611e..f8e124a 100644 --- a/aoc2023/src/day08.rs +++ b/aoc2023/src/day08.rs @@ -13,7 +13,7 @@ impl Solution for Day08 { let directions: HashMap<&str, (&str, &str)> = input.split_once("\n\n") .unwrap() .1 - .split("\n") + .split('\n') .map(|line| { let caps = re.captures(line).unwrap(); (caps.get(1).unwrap().as_str(), (caps.get(2).unwrap().as_str(), caps.get(3).unwrap().as_str())) @@ -44,7 +44,7 @@ impl Solution for Day08 { let directions: HashMap<&str, (&str, &str)> = input.split_once("\n\n") .unwrap() .1 - .split("\n") + .split('\n') .map(|line| { let caps = re.captures(line).unwrap(); (caps.get(1).unwrap().as_str(), (caps.get(2).unwrap().as_str(), caps.get(3).unwrap().as_str())) @@ -52,7 +52,7 @@ impl Solution for Day08 { .collect::>(); let mut curr: Vec<&str> = directions.keys() - .filter_map(|x| if x.chars().last() == Some('A') { Some(*x) } else { None }) + .filter_map(|x| if x.ends_with('A') { Some(*x) } else { None }) .collect(); let mut steps: u64 = 0; @@ -70,12 +70,12 @@ impl Solution for Day08 { .collect(); for i in 0..curr.len() { - if (period[i] == None && curr[i].chars().last() == Some('Z') ) { + if (period[i].is_none() && curr[i].ends_with('Z') ) { period[i] = Some(steps); } } - if period.iter().all(|x| *x != None) { + if period.iter().all(|x| x.is_some()) { break } } @@ -104,12 +104,10 @@ fn gcd(mut n: u64, mut m: u64) -> u64 { // the smaller number if m < n { // initialize a temp variable - let temp = m; - m = n; - n = temp; + std::mem::swap(&mut m, &mut n); } // Get the divisor - m = m % n; + m %= n; } // return n aka the greatest common denominator n