make clippy happy
This commit is contained in:
parent
8896610dac
commit
61798c7ac4
7 changed files with 26 additions and 37 deletions
|
|
@ -5,7 +5,7 @@ pub struct Day01;
|
||||||
|
|
||||||
impl Solution for Day01 {
|
impl Solution for Day01 {
|
||||||
fn part_1(&self, input: &str) -> Answer {
|
fn part_1(&self, input: &str) -> Answer {
|
||||||
let words: Vec<&str> = input.split("\n")
|
let words: Vec<&str> = input.split('\n')
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut sum: u64 = 0;
|
let mut sum: u64 = 0;
|
||||||
|
|
@ -14,9 +14,9 @@ impl Solution for Day01 {
|
||||||
let mut first: Option<u64> = None;
|
let mut first: Option<u64> = None;
|
||||||
let mut last: Option<u64> = None;
|
let mut last: Option<u64> = None;
|
||||||
for char in word.chars() {
|
for char in word.chars() {
|
||||||
if char.is_digit(10) {
|
if char.is_ascii_digit() {
|
||||||
last = Some(char.to_digit(10).unwrap().into());
|
last = Some(char.to_digit(10).unwrap().into());
|
||||||
if first == None {
|
if first.is_none() {
|
||||||
first = Some(char.to_digit(10).unwrap().into());
|
first = Some(char.to_digit(10).unwrap().into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -29,7 +29,7 @@ impl Solution for Day01 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_2(&self, input: &str) -> Answer {
|
fn part_2(&self, input: &str) -> Answer {
|
||||||
let words: Vec<&str> = input.split("\n")
|
let words: Vec<&str> = input.split('\n')
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let first = Regex::new(
|
let first = Regex::new(
|
||||||
|
|
@ -40,7 +40,7 @@ impl Solution for Day01 {
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
Answer::from(words.iter()
|
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::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.iter()
|
.iter()
|
||||||
.sum::<u64>())
|
.sum::<u64>())
|
||||||
|
|
@ -49,10 +49,7 @@ impl Solution for Day01 {
|
||||||
|
|
||||||
fn matches(cap: ®ex::Captures) -> u64 {
|
fn matches(cap: ®ex::Captures) -> u64 {
|
||||||
for i in 1..10 {
|
for i in 1..10 {
|
||||||
match cap.get(i) {
|
if cap.get(i).is_some() { return i as u64 };
|
||||||
Some(_) => return i as u64,
|
|
||||||
None => (),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,14 +41,14 @@ impl Solution for Day02 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_2(&self, input: &str) -> Answer {
|
fn part_2(&self, input: &str) -> Answer {
|
||||||
let games: Vec<&str> = input.split("\n")
|
let games: Vec<&str> = input.split('\n')
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let re = Regex::new(r"(\d+) (\w+)").unwrap();
|
let re = Regex::new(r"(\d+) (\w+)").unwrap();
|
||||||
|
|
||||||
let mut sum: u64 = 0;
|
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_red: u64 = 0;
|
||||||
let mut min_blue: u64 = 0;
|
let mut min_blue: u64 = 0;
|
||||||
let mut min_green: u64 = 0;
|
let mut min_green: u64 = 0;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ pub struct Day03;
|
||||||
|
|
||||||
impl Solution for Day03 {
|
impl Solution for Day03 {
|
||||||
fn part_1(&self, input: &str) -> Answer {
|
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;
|
let mut out: u64 = 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ impl Solution for Day05 {
|
||||||
let blocks: Vec<&str> = input.split("\n\n")
|
let blocks: Vec<&str> = input.split("\n\n")
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut seeds: Vec<SeedRange> = blocks[0].split(" ")
|
let mut seeds: Vec<SeedRange> = blocks[0].split(' ')
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.collect::<Vec<&str>>()
|
.collect::<Vec<&str>>()
|
||||||
.chunks(2)
|
.chunks(2)
|
||||||
|
|
@ -53,9 +53,9 @@ impl Solution for Day05 {
|
||||||
let rule_blocks: Vec<Vec<Rule>> = blocks.iter()
|
let rule_blocks: Vec<Vec<Rule>> = blocks.iter()
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.map(
|
.map(
|
||||||
|block| block.split("\n").skip(1).map(
|
|block| block.split('\n').skip(1).map(
|
||||||
|line| {
|
|line| {
|
||||||
let l: Vec<&str> = line.split(" ").collect();
|
let l: Vec<&str> = line.split(' ').collect();
|
||||||
Rule::from_rules(l[0].parse::<i64>().unwrap(), l[1].parse::<i64>().unwrap(), l[2].parse::<i64>().unwrap())
|
Rule::from_rules(l[0].parse::<i64>().unwrap(), l[1].parse::<i64>().unwrap(), l[2].parse::<i64>().unwrap())
|
||||||
}
|
}
|
||||||
).collect()
|
).collect()
|
||||||
|
|
@ -138,7 +138,7 @@ impl Rule {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_rules(dest: i64, source: i64, range: i64) -> 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>, SeedRange, Option<SeedRange>)> {
|
fn apply(&self, seeds: &SeedRange) -> Option<(Option<SeedRange>, SeedRange, Option<SeedRange>)> {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ pub struct Day06;
|
||||||
|
|
||||||
impl Solution for Day06 {
|
impl Solution for Day06 {
|
||||||
fn part_1(&self, input: &str) -> Answer {
|
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<u64> = data[0].split_whitespace().skip(1).map(|x| x.parse::<u64>().unwrap()).collect();
|
let times: Vec<u64> = data[0].split_whitespace().skip(1).map(|x| x.parse::<u64>().unwrap()).collect();
|
||||||
let dists: Vec<u64> = data[1].split_whitespace().skip(1).map(|x| x.parse::<u64>().unwrap()).collect();
|
let dists: Vec<u64> = data[1].split_whitespace().skip(1).map(|x| x.parse::<u64>().unwrap()).collect();
|
||||||
|
|
||||||
|
|
@ -23,7 +23,7 @@ impl Solution for Day06 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_2(&self, input: &str) -> Answer {
|
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::<Vec<&str>>().join("").parse::<u64>().unwrap();
|
let time: u64 = data[0].split_whitespace().skip(1).collect::<Vec<&str>>().join("").parse::<u64>().unwrap();
|
||||||
let dist: u64 = data[1].split_whitespace().skip(1).collect::<Vec<&str>>().join("").parse::<u64>().unwrap();
|
let dist: u64 = data[1].split_whitespace().skip(1).collect::<Vec<&str>>().join("").parse::<u64>().unwrap();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,20 +103,14 @@ fn compare2(hand1: &(&str, u64), hand2: &(&str, u64)) -> Ordering {
|
||||||
let val = o[&'J'];
|
let val = o[&'J'];
|
||||||
o.remove(&'J');
|
o.remove(&'J');
|
||||||
let max_key = o.iter().max_by(|a, b| a.1.cmp(&b.1)).unwrap_or((&'A', &0)).0;
|
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) {
|
o.insert(*max_key, o.get(max_key).unwrap_or(&0) + val);
|
||||||
Some(x) => x,
|
|
||||||
None => &0
|
|
||||||
} + val);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.contains_key(&'J') {
|
if s.contains_key(&'J') {
|
||||||
let val = s[&'J'];
|
let val = s[&'J'];
|
||||||
s.remove(&'J');
|
s.remove(&'J');
|
||||||
let max_key = s.iter().max_by(|a, b| a.1.cmp(&b.1)).unwrap_or((&'A', &0)).0;
|
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) {
|
s.insert(*max_key, s.get(max_key).unwrap_or(&0) + val);
|
||||||
Some(x) => x,
|
|
||||||
None => &0
|
|
||||||
} + val);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( s.keys().count() < o.keys().count() ) |
|
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() ) |
|
else if ( s.keys().count() > o.keys().count() ) |
|
||||||
( s.values().max() < o.values().max() ) { return Ordering::Less }
|
( 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()) {
|
for (i, j) in zip(hand1.0.chars(), hand2.0.chars()) {
|
||||||
if ordering.iter().position(|&x| x==i) < ordering.iter().position(|&x| x==j) {
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ impl Solution for Day08 {
|
||||||
let directions: HashMap<&str, (&str, &str)> = input.split_once("\n\n")
|
let directions: HashMap<&str, (&str, &str)> = input.split_once("\n\n")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.1
|
.1
|
||||||
.split("\n")
|
.split('\n')
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
let caps = re.captures(line).unwrap();
|
let caps = re.captures(line).unwrap();
|
||||||
(caps.get(1).unwrap().as_str(), (caps.get(2).unwrap().as_str(), caps.get(3).unwrap().as_str()))
|
(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")
|
let directions: HashMap<&str, (&str, &str)> = input.split_once("\n\n")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.1
|
.1
|
||||||
.split("\n")
|
.split('\n')
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
let caps = re.captures(line).unwrap();
|
let caps = re.captures(line).unwrap();
|
||||||
(caps.get(1).unwrap().as_str(), (caps.get(2).unwrap().as_str(), caps.get(3).unwrap().as_str()))
|
(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::<HashMap<&str, (&str, &str)>>();
|
.collect::<HashMap<&str, (&str, &str)>>();
|
||||||
|
|
||||||
let mut curr: Vec<&str> = directions.keys()
|
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();
|
.collect();
|
||||||
|
|
||||||
let mut steps: u64 = 0;
|
let mut steps: u64 = 0;
|
||||||
|
|
@ -70,12 +70,12 @@ impl Solution for Day08 {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for i in 0..curr.len() {
|
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);
|
period[i] = Some(steps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if period.iter().all(|x| *x != None) {
|
if period.iter().all(|x| x.is_some()) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -104,12 +104,10 @@ fn gcd(mut n: u64, mut m: u64) -> u64 {
|
||||||
// the smaller number
|
// the smaller number
|
||||||
if m < n {
|
if m < n {
|
||||||
// initialize a temp variable
|
// initialize a temp variable
|
||||||
let temp = m;
|
std::mem::swap(&mut m, &mut n);
|
||||||
m = n;
|
|
||||||
n = temp;
|
|
||||||
}
|
}
|
||||||
// Get the divisor
|
// Get the divisor
|
||||||
m = m % n;
|
m %= n;
|
||||||
}
|
}
|
||||||
// return n aka the greatest common denominator
|
// return n aka the greatest common denominator
|
||||||
n
|
n
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue