Skip to content

Commit

Permalink
refactor: cleanup all days
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefKuchar committed Dec 3, 2024
1 parent d412bcb commit b5c89d9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
1 change: 0 additions & 1 deletion data/examples/03.txt

This file was deleted.

4 changes: 2 additions & 2 deletions src/bin/01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ fn get_lists(input: &str) -> (Vec<u32>, Vec<u32>) {
.lines()
.flat_map(|line| line.split_whitespace().map(|x| x.parse::<u32>().unwrap()))
.collect();
let left = numbers.iter().step_by(2).map(|x| *x).collect();
let right = numbers.iter().skip(1).map(|x| *x).step_by(2).collect();
let left = numbers.iter().step_by(2).copied().collect();
let right = numbers.iter().skip(1).step_by(2).copied().collect();

(left, right)
}
Expand Down
13 changes: 6 additions & 7 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ fn is_ok(sequence: &[i32]) -> bool {
sequence
.windows(2)
.map(|x| x[0] - x[1])
.fold(Some(Ordering::Equal), |acc, x| {
.try_fold(Ordering::Equal, |acc, x| {
let ord = x.cmp(&0);
if x.abs() >= 1 && x.abs() <= 3 {
match acc {
Some(Ordering::Equal) => Some(ord),
Some(Ordering::Less) => match ord {
Ordering::Equal => Some(ord),
Ordering::Less => match ord {
Ordering::Less => Some(Ordering::Less),
_ => None,
},
Some(Ordering::Greater) => match ord {
Ordering::Greater => match ord {
Ordering::Greater => Some(Ordering::Greater),
_ => None,
},
None => None,
}
} else {
None
Expand All @@ -43,7 +42,7 @@ pub fn part_one(input: &str) -> Option<u32> {
Some(
parse_input(input)
.iter()
.map(|line| is_ok(&line))
.map(|line| is_ok(line))
.filter(|x| *x)
.count() as u32,
)
Expand All @@ -54,7 +53,7 @@ pub fn part_two(input: &str) -> Option<u32> {
parse_input(input)
.iter()
.map(|line| {
let mut ok = is_ok(&line);
let mut ok = is_ok(line);
for i in 0..line.len() {
let mut line = line.clone();
line.remove(i);
Expand Down
3 changes: 2 additions & 1 deletion src/bin/03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ mod tests {

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
let result =
part_one("xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))");
assert_eq!(result, Some(161));
}

Expand Down

0 comments on commit b5c89d9

Please sign in to comment.