Skip to content

Commit

Permalink
Refactor previous years to comply with clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiac committed Dec 3, 2023
1 parent a42434d commit 37c8213
Show file tree
Hide file tree
Showing 35 changed files with 247 additions and 323 deletions.
2 changes: 1 addition & 1 deletion aoc-solver/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ criterion_main!(benches);

fn benchmark_all(c: &mut Criterion) {
c.bench_function("all", |b| {
b.iter(|| Y2022::run_all());
b.iter(Y2022::run_all);
});
}

Expand Down
4 changes: 2 additions & 2 deletions aoc-solver/src/y2020/day01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Solution for Day01 {
fn part_1(&self, input: &str) -> Result<usize, AocError> {
let input: Vec<usize> = input
.trim()
.split("\n")
.lines()
.map(|num| num.parse::<usize>().expect("parsing"))
.collect();

Expand All @@ -31,7 +31,7 @@ impl Solution for Day01 {
fn part_2(&self, input: &str) -> Result<usize, AocError> {
let input: Vec<usize> = input
.trim()
.split("\n")
.lines()
.map(|num| num.parse::<usize>().expect("parsing"))
.collect();

Expand Down
105 changes: 43 additions & 62 deletions aoc-solver/src/y2020/day02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,31 @@ fn validate_part1(db_row: &DatabaseRow) -> bool {
.chars()
.filter(|c| c == &db_row.check_char)
.count();
return occurences >= db_row.min && occurences <= db_row.max;
occurences >= db_row.min && occurences <= db_row.max
}

fn validate_part2(db_row: &DatabaseRow) -> bool {
let first_match = db_row.password.chars().nth(db_row.min - 1).unwrap() == db_row.check_char;
let second_match = db_row.password.chars().nth(db_row.max - 1).unwrap() == db_row.check_char;
return first_match ^ second_match;
first_match ^ second_match
}

fn parse_input(input: &str) -> Vec<DatabaseRow> {
let input_regex = Regex::new(r"^(\d+)-(\d+) (\S): (\S+)$").unwrap();
let rows: Vec<DatabaseRow> = input

input
.lines()
.map(|line| {
let capture = input_regex.captures(line).unwrap();

return DatabaseRow {
DatabaseRow {
min: capture[1].parse::<usize>().unwrap(),
max: capture[2].parse::<usize>().unwrap(),
password: capture[4].parse::<String>().unwrap(),
check_char: capture[3].parse::<char>().unwrap(),
};
}
})
.collect();

return rows;
.collect()
}

impl Solution for Day02 {
Expand Down Expand Up @@ -80,33 +79,24 @@ mod tests {

#[test]
fn it_validates_part1_pws_correctly() {
assert_eq!(
validate_part1(&DatabaseRow {
password: String::from("abcde"),
check_char: 'a',
min: 1,
max: 3
}),
true
);
assert_eq!(
validate_part1(&DatabaseRow {
password: String::from("cdefg"),
check_char: 'b',
min: 1,
max: 3
}),
false
);
assert_eq!(
validate_part1(&DatabaseRow {
password: String::from("ccccccccc"),
check_char: 'c',
min: 2,
max: 9
}),
true
)
assert!(validate_part1(&DatabaseRow {
password: String::from("abcde"),
check_char: 'a',
min: 1,
max: 3
}));
assert!(!validate_part1(&DatabaseRow {
password: String::from("cdefg"),
check_char: 'b',
min: 1,
max: 3
}));
assert!(validate_part1(&DatabaseRow {
password: String::from("ccccccccc"),
check_char: 'c',
min: 2,
max: 9
}))
}

#[test]
Expand All @@ -119,33 +109,24 @@ mod tests {

#[test]
fn it_validates_part2_pws_correctly() {
assert_eq!(
validate_part2(&DatabaseRow {
password: String::from("abcde"),
check_char: 'a',
min: 1,
max: 3
}),
true
);
assert_eq!(
validate_part2(&DatabaseRow {
password: String::from("cdefg"),
check_char: 'b',
min: 1,
max: 3
}),
false
);
assert_eq!(
validate_part2(&DatabaseRow {
password: String::from("ccccccccc"),
check_char: 'c',
min: 2,
max: 9
}),
false
)
assert!(validate_part2(&DatabaseRow {
password: String::from("abcde"),
check_char: 'a',
min: 1,
max: 3
}));
assert!(!validate_part2(&DatabaseRow {
password: String::from("cdefg"),
check_char: 'b',
min: 1,
max: 3
}));
assert!(!validate_part2(&DatabaseRow {
password: String::from("ccccccccc"),
check_char: 'c',
min: 2,
max: 9
}))
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions aoc-solver/src/y2020/day03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn count_trees(

while current.1 < rows {
if trees.contains(&current) {
hits = hits + 1;
hits += 1;
}

current.0 = (current.0 + right) % columns;
current.1 = current.1 + down;
current.1 += down;
}

return hits;
hits
}

fn parse(input: &str) -> (HashSet<(usize, usize)>, usize, usize) {
Expand All @@ -40,7 +40,7 @@ fn parse(input: &str) -> (HashSet<(usize, usize)>, usize, usize) {
let rows = input.lines().count();
let columns = input.lines().next().unwrap().len();

return (trees, columns, rows);
(trees, columns, rows)
}

impl Solution for Day03 {
Expand Down
46 changes: 20 additions & 26 deletions aoc-solver/src/y2020/day04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,34 @@ struct Passport {
cid: Option<String>,
}

fn is_char_hex_digit(c: char) -> bool {
c.is_ascii() && c.is_digit(16)
}

fn is_char_digit(c: char) -> bool {
return c.is_ascii() && c.is_digit(10);
}

fn not_whitespace(input: &str) -> IResult<&str, &str> {
nom::bytes::complete::is_not(" \t\n")(input)
}

fn birth_year(input: &str) -> IResult<&str, u32> {
map_res(
delimited(multispace0, preceded(tag("byr:"), digit1), multispace0),
|i: &str| u32::from_str_radix(i, 10),
|i: &str| i.parse::<u32>(),
)(input)
}

fn issue_year(input: &str) -> IResult<&str, u32> {
map_res(
delimited(multispace0, preceded(tag("iyr:"), digit1), multispace0),
|i: &str| u32::from_str_radix(i, 10),
|i: &str| i.parse::<u32>(),
)(input)
}

fn expiration_year(input: &str) -> IResult<&str, u32> {
map_res(
delimited(multispace0, preceded(tag("eyr:"), digit1), multispace0),
|i: &str| u32::from_str_radix(i, 10),
|i: &str| i.parse::<u32>(),
)(input)
}

fn parse_centimeters(input: &str) -> IResult<&str, (u32, &str)> {
pair(
map_res(digit1, |i: &str| u32::from_str_radix(i, 10)),
map_res(digit1, |i: &str| i.parse::<u32>()),
alt((tag("cm"), tag("in"))),
)(input)
}
Expand All @@ -69,7 +61,10 @@ fn height(input: &str) -> IResult<&str, (u32, &str)> {
}

fn parse_hex_color(input: &str) -> IResult<&str, &str> {
preceded(tag("#"), take_while_m_n(6, 6, is_char_hex_digit))(input)
preceded(
tag("#"),
take_while_m_n(6, 6, |c: char| c.is_ascii_hexdigit()),
)(input)
}

fn hair_color(input: &str) -> IResult<&str, &str> {
Expand Down Expand Up @@ -101,7 +96,7 @@ fn eye_color(input: &str) -> IResult<&str, &str> {
}

fn parse_passport_number(input: &str) -> IResult<&str, &str> {
take_while_m_n(9, 9, is_char_digit)(input)
take_while_m_n(9, 9, |c: char| c.is_ascii_digit())(input)
}

fn passport_id(input: &str) -> IResult<&str, &str> {
Expand All @@ -119,12 +114,9 @@ fn country_id(input: &str) -> IResult<&str, Option<String>> {
multispace0,
)(input)?;

let cid_string = match parsed {
Some(c) => Some(c.to_string()),
None => None,
};
let cid_string = parsed.map(|c| c.to_string());

return Ok((unhandled, cid_string));
Ok((unhandled, cid_string))
}

fn parse_passport_part1(input: &str) -> IResult<&str, ()> {
Expand Down Expand Up @@ -172,16 +164,17 @@ fn parse_passport_part2(input: &str) -> IResult<&str, Passport> {
))(input)?;

let passport = Passport {
byr: byr,
iyr: iyr,
eyr: eyr,
byr,
iyr,
eyr,
hgt: (hgt.0, String::from(hgt.1)),
hcl: String::from(hcl),
ecl: String::from(ecl),
pid: String::from(pid),
cid: cid,
cid,
};
return Ok((unhandled, passport));

Ok((unhandled, passport))
}

impl Solution for Day04 {
Expand All @@ -207,7 +200,7 @@ impl Solution for Day04 {
.split("\n\n")
.map(parse_passport_part2)
.filter_map(|passport| {
if !passport.is_ok() {
if passport.is_err() {
return None;
}

Expand All @@ -232,7 +225,8 @@ impl Solution for Day04 {
if valid.hgt.1 == "in" && (valid.hgt.0 < 59 || valid.hgt.0 > 76) {
return None;
}
return Some(valid);

Some(valid)
})
.count();

Expand Down
4 changes: 2 additions & 2 deletions aoc-solver/src/y2020/day05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn parse_boarding_pass(input: &str) -> BoardingPass {
let column = u8::from_str_radix(&binary_str[7..10], 2).unwrap();

BoardingPass {
row: row,
column: column,
row,
column,
id: 8 * row as usize + column as usize,
}
}
Expand Down
4 changes: 2 additions & 2 deletions aoc-solver/src/y2020/day07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn build_rules_tree(input: &str) -> HashMap<String, Rule> {
}
}

return rules;
rules
}

fn find_ancestors(current: String, rules: HashMap<String, Rule>) -> HashSet<String> {
Expand All @@ -68,7 +68,7 @@ fn find_ancestors(current: String, rules: HashMap<String, Rule>) -> HashSet<Stri
ancestors.extend(find_ancestors(parent.clone(), rules.clone()));
}

return ancestors;
ancestors
}

fn find_inner_bags_count(current: String, rules: HashMap<String, Rule>) -> usize {
Expand Down
12 changes: 6 additions & 6 deletions aoc-solver/src/y2020/day08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl GameConsole {
fn acc(&mut self) -> Flag {
self.accumulator += self.mem[self.program_counter].arg;
self.program_counter += 1;
return Flag::Running;
Flag::Running
}

// `jmp` jumps to a new instruction relative to itself.
Expand All @@ -42,14 +42,14 @@ impl GameConsole {
fn jmp(&mut self) -> Flag {
self.program_counter =
(self.program_counter as i32 + self.mem[self.program_counter].arg) as usize;
return Flag::Running;
Flag::Running
}

// `nop` stands for No OPeration - it does nothing. The instruction immediately below it is executed next.
#[inline]
fn nop(&mut self) -> Flag {
self.program_counter += 1;
return Flag::Running;
Flag::Running
}

pub fn run_program(&mut self) -> Flag {
Expand All @@ -76,14 +76,14 @@ impl GameConsole {
fn parse_instruction(input: &str) -> IResult<&str, Instruction> {
let (i, operation) = alt((tag("acc"), tag("jmp"), tag("nop")))(input)?;
let (i, _ws) = space0(i)?;
let arg = i32::from_str_radix(i, 10).unwrap();
let arg = i.parse::<i32>().unwrap();

let instruction = Instruction {
operation: operation.to_string(),
arg: arg,
arg,
};

return Ok(("", instruction));
Ok(("", instruction))
}

impl Solution for Day08 {
Expand Down
Loading

0 comments on commit 37c8213

Please sign in to comment.