rename type

This commit is contained in:
Adam Jeniski 2022-12-04 18:44:10 -05:00
parent ee1a97f61a
commit 00bd0cfbde
4 changed files with 18 additions and 17 deletions

View File

@ -1,17 +1,17 @@
type Input = Vec<Vec<i32>>; type Data = Vec<Vec<i32>>;
fn parse(input: &str) -> Input { fn parse(input: &str) -> Data {
input input
.split("\n\n") .split("\n\n")
.map(|lines| lines.lines().map(|line| line.parse().unwrap()).collect()) .map(|lines| lines.lines().map(|line| line.parse().unwrap()).collect())
.collect() .collect()
} }
fn part_one(data: Input) -> i32 { fn part_one(data: Data) -> i32 {
data.iter().map(|cals| cals.iter().sum()).max().unwrap() 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<i32> = let mut cals: std::collections::BinaryHeap<i32> =
data.iter().map(|cals| cals.iter().sum()).collect(); data.iter().map(|cals| cals.iter().sum()).collect();
cals.pop().unwrap() + cals.pop().unwrap() + cals.pop().unwrap() cals.pop().unwrap() + cals.pop().unwrap() + cals.pop().unwrap()

View File

@ -1,7 +1,5 @@
use Shape::*; use Shape::*;
type Input = Vec<Vec<char>>;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
enum Shape { enum Shape {
Rock, Rock,
@ -39,13 +37,16 @@ impl Shape {
} }
} }
} }
fn parse(input: &str) -> Input {
type Data = Vec<Vec<char>>;
fn parse(input: &str) -> Data {
input input
.lines() .lines()
.map(|line| line.split(' ').map(|s| s.chars().next().unwrap()).collect()) .map(|line| line.split(' ').map(|s| s.chars().next().unwrap()).collect())
.collect() .collect()
} }
fn part_one(data: Input) -> i32 { fn part_one(data: Data) -> i32 {
data.iter() data.iter()
.map(|round| { .map(|round| {
let my_move = Shape::from_char(round[1]); let my_move = Shape::from_char(round[1]);
@ -61,7 +62,7 @@ fn part_one(data: Input) -> i32 {
}) })
.sum() .sum()
} }
fn part_two(data: Input) -> i32 { fn part_two(data: Data) -> i32 {
data.iter() data.iter()
.map(|round| match round[1] { .map(|round| match round[1] {
'X' => Shape::from_char(round[0]).wins_to().as_int(), 'X' => Shape::from_char(round[0]).wins_to().as_int(),

View File

@ -1,4 +1,4 @@
type Input = Vec<String>; type Data = Vec<String>;
fn get_priority(c: char) -> i32 { fn get_priority(c: char) -> i32 {
match c { match c {
@ -7,10 +7,10 @@ fn get_priority(c: char) -> i32 {
_ => panic!("expected letter"), _ => panic!("expected letter"),
} }
} }
fn parse(input: &str) -> Input { fn parse(input: &str) -> Data {
input.lines().map(str::to_owned).collect() input.lines().map(str::to_owned).collect()
} }
fn part_one(data: Input) -> i32 { fn part_one(data: Data) -> i32 {
data.iter() data.iter()
.map(|s| { .map(|s| {
use std::collections::HashSet; use std::collections::HashSet;
@ -20,7 +20,7 @@ fn part_one(data: Input) -> i32 {
}) })
.sum() .sum()
} }
fn part_two(data: Input) -> i32 { fn part_two(data: Data) -> i32 {
data.as_slice() data.as_slice()
.chunks(3) .chunks(3)
.map(|c| { .map(|c| {

View File

@ -1,6 +1,6 @@
type Input = Vec<Vec<i32>>; type Data = Vec<Vec<i32>>;
fn parse(input: &str) -> Input { fn parse(input: &str) -> Data {
input input
.lines() .lines()
.map(|line| { .map(|line| {
@ -15,7 +15,7 @@ fn parse(input: &str) -> Input {
}) })
.collect() .collect()
} }
fn part_one(data: Input) -> i32 { fn part_one(data: Data) -> i32 {
data.into_iter().fold(0, |acc, curr| { data.into_iter().fold(0, |acc, curr| {
if (curr[0] <= curr[2] && curr[1] >= curr[3]) || (curr[2] <= curr[0] && curr[3] >= curr[1]) 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| { data.into_iter().fold(0, |acc, curr| {
if (curr[0] >= curr[2] && curr[0] <= curr[3]) if (curr[0] >= curr[2] && curr[0] <= curr[3])
|| (curr[1] >= curr[2] && curr[1] <= curr[3]) || (curr[1] >= curr[2] && curr[1] <= curr[3])