From 00bd0cfbde0ea4b9a99f546c92953278b27c8b05 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 4 Dec 2022 18:44:10 -0500 Subject: [PATCH] rename type --- 2022/src/day_1.rs | 8 ++++---- 2022/src/day_2.rs | 11 ++++++----- 2022/src/day_3.rs | 8 ++++---- 2022/src/day_4.rs | 8 ++++---- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/2022/src/day_1.rs b/2022/src/day_1.rs index d2fbc7b..4dafc4a 100644 --- a/2022/src/day_1.rs +++ b/2022/src/day_1.rs @@ -1,17 +1,17 @@ -type Input = Vec>; +type Data = Vec>; -fn parse(input: &str) -> Input { +fn parse(input: &str) -> Data { input .split("\n\n") .map(|lines| lines.lines().map(|line| line.parse().unwrap()).collect()) .collect() } -fn part_one(data: Input) -> i32 { +fn part_one(data: Data) -> i32 { data.iter().map(|cals| cals.iter().sum()).max().unwrap() } -fn part_two(data: Input) -> i32 { +fn part_two(data: Data) -> i32 { let mut cals: std::collections::BinaryHeap = data.iter().map(|cals| cals.iter().sum()).collect(); cals.pop().unwrap() + cals.pop().unwrap() + cals.pop().unwrap() diff --git a/2022/src/day_2.rs b/2022/src/day_2.rs index 3c29c3b..7da30f9 100644 --- a/2022/src/day_2.rs +++ b/2022/src/day_2.rs @@ -1,7 +1,5 @@ use Shape::*; -type Input = Vec>; - #[derive(Debug, PartialEq, Eq)] enum Shape { Rock, @@ -39,13 +37,16 @@ impl Shape { } } } -fn parse(input: &str) -> Input { + +type Data = Vec>; + +fn parse(input: &str) -> Data { input .lines() .map(|line| line.split(' ').map(|s| s.chars().next().unwrap()).collect()) .collect() } -fn part_one(data: Input) -> i32 { +fn part_one(data: Data) -> i32 { data.iter() .map(|round| { let my_move = Shape::from_char(round[1]); @@ -61,7 +62,7 @@ fn part_one(data: Input) -> i32 { }) .sum() } -fn part_two(data: Input) -> i32 { +fn part_two(data: Data) -> i32 { data.iter() .map(|round| match round[1] { 'X' => Shape::from_char(round[0]).wins_to().as_int(), diff --git a/2022/src/day_3.rs b/2022/src/day_3.rs index 4424f3a..e56cf86 100644 --- a/2022/src/day_3.rs +++ b/2022/src/day_3.rs @@ -1,4 +1,4 @@ -type Input = Vec; +type Data = Vec; fn get_priority(c: char) -> i32 { match c { @@ -7,10 +7,10 @@ fn get_priority(c: char) -> i32 { _ => panic!("expected letter"), } } -fn parse(input: &str) -> Input { +fn parse(input: &str) -> Data { input.lines().map(str::to_owned).collect() } -fn part_one(data: Input) -> i32 { +fn part_one(data: Data) -> i32 { data.iter() .map(|s| { use std::collections::HashSet; @@ -20,7 +20,7 @@ fn part_one(data: Input) -> i32 { }) .sum() } -fn part_two(data: Input) -> i32 { +fn part_two(data: Data) -> i32 { data.as_slice() .chunks(3) .map(|c| { diff --git a/2022/src/day_4.rs b/2022/src/day_4.rs index 8d21125..29396d4 100644 --- a/2022/src/day_4.rs +++ b/2022/src/day_4.rs @@ -1,6 +1,6 @@ -type Input = Vec>; +type Data = Vec>; -fn parse(input: &str) -> Input { +fn parse(input: &str) -> Data { input .lines() .map(|line| { @@ -15,7 +15,7 @@ fn parse(input: &str) -> Input { }) .collect() } -fn part_one(data: Input) -> i32 { +fn part_one(data: Data) -> i32 { data.into_iter().fold(0, |acc, curr| { if (curr[0] <= curr[2] && curr[1] >= curr[3]) || (curr[2] <= curr[0] && curr[3] >= curr[1]) { @@ -25,7 +25,7 @@ fn part_one(data: Input) -> i32 { } }) } -fn part_two(data: Input) -> i32 { +fn part_two(data: Data) -> i32 { data.into_iter().fold(0, |acc, curr| { if (curr[0] >= curr[2] && curr[0] <= curr[3]) || (curr[1] >= curr[2] && curr[1] <= curr[3])