diff --git a/aoc2019/src/day03.rs b/aoc2019/src/day03.rs index f91ef19..471d897 100644 --- a/aoc2019/src/day03.rs +++ b/aoc2019/src/day03.rs @@ -190,7 +190,7 @@ impl FromStr for Move { fn from_str(s: &str) -> Result { let direction = s .chars() - .nth(0) + .next() .context("couldn't get direction char in move")?; let s = s.get(1..).context("move missing length")?; diff --git a/aoc2019/src/day04.rs b/aoc2019/src/day04.rs index 4d6b5f4..617fde9 100644 --- a/aoc2019/src/day04.rs +++ b/aoc2019/src/day04.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::fmt::Write; use anyhow::{Context, Result}; @@ -59,16 +60,20 @@ fn part2(begin: usize, end: usize) -> Result { let mut count = 1; let mut prev = digits[0]; for digit in digits.iter().skip(1) { - if prev > *digit { - ordered = false; - break; - } else if prev == *digit { - count += 1; - } else { - if count == 2 { - pair = true; + match prev.cmp(digit) { + Ordering::Greater => { + ordered = false; + break; + } + Ordering::Equal => { + count += 1; + } + Ordering::Less => { + if count == 2 { + pair = true; + } + count = 1; } - count = 1; } prev = *digit; diff --git a/aoc2019/src/day12.rs b/aoc2019/src/day12.rs index bdf61fd..10737b4 100644 --- a/aoc2019/src/day12.rs +++ b/aoc2019/src/day12.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::fmt::Write; use std::str::FromStr; @@ -52,28 +53,40 @@ fn part1(mut planets: Vec, steps: usize) -> Result { let first = &mut begin[pair.0]; let second = &mut end[0]; - if first.position.x > second.position.x { - first.velocity.x -= 1; - second.velocity.x += 1; - } else if first.position.x < second.position.x { - first.velocity.x += 1; - second.velocity.x -= 1; + match first.position.x.cmp(&second.position.x) { + Ordering::Greater => { + first.velocity.x -= 1; + second.velocity.x += 1; + } + Ordering::Less => { + first.velocity.x += 1; + second.velocity.x -= 1; + } + Ordering::Equal => {} } - if first.position.y > second.position.y { - first.velocity.y -= 1; - second.velocity.y += 1; - } else if first.position.y < second.position.y { - first.velocity.y += 1; - second.velocity.y -= 1; + match first.position.y.cmp(&second.position.y) { + Ordering::Greater => { + first.velocity.y -= 1; + second.velocity.y += 1; + } + Ordering::Less => { + first.velocity.y += 1; + second.velocity.y -= 1; + } + Ordering::Equal => {} } - if first.position.z > second.position.z { - first.velocity.z -= 1; - second.velocity.z += 1; - } else if first.position.z < second.position.z { - first.velocity.z += 1; - second.velocity.z -= 1; + match first.position.z.cmp(&second.position.z) { + Ordering::Greater => { + first.velocity.z -= 1; + second.velocity.z += 1; + } + Ordering::Less => { + first.velocity.z += 1; + second.velocity.z -= 1; + } + Ordering::Equal => {} } } @@ -114,28 +127,40 @@ fn part2(mut planets: Vec) -> Result { let first = &mut begin[pair.0]; let second = &mut end[0]; - if first.position.x > second.position.x { - first.velocity.x -= 1; - second.velocity.x += 1; - } else if first.position.x < second.position.x { - first.velocity.x += 1; - second.velocity.x -= 1; + match first.position.x.cmp(&second.position.x) { + Ordering::Greater => { + first.velocity.x -= 1; + second.velocity.x += 1; + } + Ordering::Less => { + first.velocity.x += 1; + second.velocity.x -= 1; + } + Ordering::Equal => {} } - if first.position.y > second.position.y { - first.velocity.y -= 1; - second.velocity.y += 1; - } else if first.position.y < second.position.y { - first.velocity.y += 1; - second.velocity.y -= 1; + match first.position.y.cmp(&second.position.y) { + Ordering::Greater => { + first.velocity.y -= 1; + second.velocity.y += 1; + } + Ordering::Less => { + first.velocity.y += 1; + second.velocity.y -= 1; + } + Ordering::Equal => {} } - if first.position.z > second.position.z { - first.velocity.z -= 1; - second.velocity.z += 1; - } else if first.position.z < second.position.z { - first.velocity.z += 1; - second.velocity.z -= 1; + match first.position.z.cmp(&second.position.z) { + Ordering::Greater => { + first.velocity.z -= 1; + second.velocity.z += 1; + } + Ordering::Less => { + first.velocity.z += 1; + second.velocity.z -= 1; + } + Ordering::Equal => {} } } diff --git a/aoc2019/src/day13.rs b/aoc2019/src/day13.rs index 1d8276a..72afe59 100644 --- a/aoc2019/src/day13.rs +++ b/aoc2019/src/day13.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::collections::HashMap; use std::fmt::{self, Display, Write}; @@ -30,13 +31,7 @@ fn part1(memory: Vec) -> Result { map.insert(pos, tile); } - Ok(map - .values() - .filter(|t| match t { - Tile::Block => true, - _ => false, - }) - .count()) + Ok(map.values().filter(|t| matches!(t, Tile::Block)).count()) } #[allow(dead_code)] @@ -50,12 +45,10 @@ fn print_screen(screen: &[Vec]) { } fn get_next_move(paddle_pos: (i64, i64), ball_pos: (i64, i64)) -> i64 { - if ball_pos.0 > paddle_pos.0 { - 1 - } else if ball_pos.0 == paddle_pos.0 { - 0 - } else { - -1 + match ball_pos.0.cmp(&paddle_pos.0) { + Ordering::Greater => 1, + Ordering::Equal => 0, + Ordering::Less => -1, } } diff --git a/aoc2019/src/day14.rs b/aoc2019/src/day14.rs index 00c2132..398953a 100644 --- a/aoc2019/src/day14.rs +++ b/aoc2019/src/day14.rs @@ -90,7 +90,7 @@ fn get_ore_cost( in_stock += num_reactions * recipe.produced; } - inventory.insert(material.clone(), in_stock - quantity); + inventory.insert(material, in_stock - quantity); Ok(total) }