From 663375d8f8a3b4e35352ef43edb22a3db666647a Mon Sep 17 00:00:00 2001 From: fuckwit Date: Thu, 23 Nov 2023 09:22:23 +0100 Subject: [PATCH] improve performance of 2016 day 1 part 2 and format fixes --- src/year2016/day01.rs | 4 ++-- src/year2016/day03.rs | 5 ++++- src/year2022/day01.rs | 12 +++++++++--- src/year2022/day02.rs | 31 +++++++++++++++++-------------- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/year2016/day01.rs b/src/year2016/day01.rs index 893ef8a..d8faf18 100644 --- a/src/year2016/day01.rs +++ b/src/year2016/day01.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::collections::BTreeSet; use crate::util::{index::*, parse::ParseExt}; @@ -29,7 +29,7 @@ pub fn part1(input: &str) -> impl std::fmt::Display { pub fn part2(input: &str) -> impl std::fmt::Display { let mut pos = Ix2::ORIGIN; let mut dir = Ix2::UP; - let mut set = HashSet::new(); + let mut set = BTreeSet::new(); for (d, s) in parse(input) { dir = if d == b'R' { diff --git a/src/year2016/day03.rs b/src/year2016/day03.rs index 654f766..fb110e0 100644 --- a/src/year2016/day03.rs +++ b/src/year2016/day03.rs @@ -52,5 +52,8 @@ fn test_part1() { #[test] fn test_part2() { - assert_eq!("2", part2("100 200 300\n101 201 301\n102 202 302").to_string()); + assert_eq!( + "2", + part2("100 200 300\n101 201 301\n102 202 302").to_string() + ); } diff --git a/src/year2022/day01.rs b/src/year2022/day01.rs index dd5c610..7828b9f 100644 --- a/src/year2022/day01.rs +++ b/src/year2022/day01.rs @@ -16,10 +16,16 @@ pub fn part2(input: &str) -> impl std::fmt::Display { #[test] fn test_part1() { - assert_eq!("24000", part1("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()) + assert_eq!( + "24000", + part1("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string() + ) } #[test] fn test_part2() { - assert_eq!("45000", part2("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()) -} \ No newline at end of file + assert_eq!( + "45000", + part2("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string() + ) +} diff --git a/src/year2022/day02.rs b/src/year2022/day02.rs index bb3b485..ab09335 100644 --- a/src/year2022/day02.rs +++ b/src/year2022/day02.rs @@ -1,22 +1,22 @@ enum Move { Rock = 1, Paper = 2, - Scissors = 3 + Scissors = 3, } enum GameResult { Loss = 0, Draw = 3, - Win = 6 + Win = 6, } impl From<&str> for Move { fn from(value: &str) -> Self { match value { - "A" | "X" => Self::Rock, - "B" | "Y" => Self::Paper, - "C" | "Z" => Self::Scissors, - _ => unreachable!() + "A" | "X" => Self::Rock, + "B" | "Y" => Self::Paper, + "C" | "Z" => Self::Scissors, + _ => unreachable!(), } } } @@ -27,18 +27,21 @@ impl From<&str> for GameResult { "X" => Self::Loss, "Y" => Self::Draw, "Z" => Self::Win, - _ => unreachable!() + _ => unreachable!(), } } } fn calc(input: &str, calc: fn(&str, &str) -> u32) -> u32 { - input.lines().map(|l| { - let Some((l, r)) = l.split_once(' ') else { - unreachable!() - }; - (calc)(l, r) - }).sum() + input + .lines() + .map(|l| { + let Some((l, r)) = l.split_once(' ') else { + unreachable!() + }; + (calc)(l, r) + }) + .sum() } pub fn part1(input: &str) -> impl std::fmt::Display { @@ -89,4 +92,4 @@ fn test_part1() { #[test] fn test_part2() { assert_eq!("12", part2("A Y\nB X\nC Z").to_string()) -} \ No newline at end of file +}