chore: fmt + clippy

This commit is contained in:
Xavier Morel
2024-11-14 15:36:19 +01:00
parent 5eba391ebe
commit e243ca9498
3 changed files with 58 additions and 96 deletions

View File

@@ -29,27 +29,21 @@ impl PosP1 {
_y if _y > 1 => 1,
_ => y,
};
PosP1 {x: x, y: y}
PosP1 { x, y }
}
}
impl Keypad for PosP1 {
fn init() -> PosP1 {
Self {
x: 0,
y: 0,
}
Self { x: 0, y: 0 }
}
fn to_digit(self) -> char {
let row_offset = (self.y * -1) + 1;
let row_offset = -self.y + 1;
let col_offset = self.x + 1;
char::from_digit(
((row_offset * 3) + col_offset + 1).try_into().unwrap(),
10
).unwrap()
char::from_digit(((row_offset * 3) + col_offset + 1).try_into().unwrap(), 10).unwrap()
}
fn move_to(self, dir: &Dir) -> PosP1 {
match dir {
Dir::Up => PosP1::from_pos(self.x, self.y + 1),
@@ -60,12 +54,12 @@ impl Keypad for PosP1 {
}
}
const DISP_P2: [[char; 5]; 5]= [
['0', '0', '1', '0', '0', ],
['0', '2', '3', '4', '0', ],
['5', '6', '7', '8', '9', ],
['0', 'A', 'B', 'C', '0', ],
['0', '0', 'D', '0', '0', ],
const DISP_P2: [[char; 5]; 5] = [
['0', '0', '1', '0', '0'],
['0', '2', '3', '4', '0'],
['5', '6', '7', '8', '9'],
['0', 'A', 'B', 'C', '0'],
['0', '0', 'D', '0', '0'],
];
#[derive(Debug, Copy, Clone)]
@@ -77,12 +71,9 @@ struct PosP2 {
impl PosP2 {
fn from(row: usize, col: usize) -> Option<Self> {
if row > 4 || col > 4 {
return None
return None;
}
let res = PosP2 {
row: row,
col: col,
};
let res = PosP2 { row, col };
if res.to_digit() == '0' {
None
} else {
@@ -91,13 +82,9 @@ impl PosP2 {
}
}
impl Keypad for PosP2 {
fn init() -> PosP2 {
Self {
row: 2,
col: 0
}
Self { row: 2, col: 0 }
}
fn to_digit(self) -> char {
@@ -122,43 +109,43 @@ enum Dir {
Right,
}
fn compute_digit<T: Keypad>(input: T, path: &Vec<Dir>) -> T {
path
.into_iter()
.fold(input, |pos, dir| pos.move_to(dir))
fn compute_digit<T: Keypad>(input: T, path: &[Dir]) -> T {
path.iter().fold(input, |pos, dir| pos.move_to(dir))
}
fn parse_input(input: &str) -> Vec<Vec<Dir>> {
input
.to_string()
.strip_suffix("\n")
.unwrap_or(&input)
.unwrap_or(input)
.split("\n")
.map(|s| s
.chars()
.map(|c| match c {
'U' => Dir::Up,
'D' => Dir::Down,
'L' => Dir::Left,
'R' => Dir::Right,
_ => panic!("Invalid input!"),
})
.collect()
).collect()
.map(|s| {
s.chars()
.map(|c| match c {
'U' => Dir::Up,
'D' => Dir::Down,
'L' => Dir::Left,
'R' => Dir::Right,
_ => panic!("Invalid input!"),
})
.collect()
})
.collect()
}
fn solve<T: Keypad + Copy>(input: &str) -> Option<String> {
let dirs = parse_input(input);
let digit: T = T::init();
let res: Vec<char> = dirs.into_iter().scan(digit, |digit, ds| {
*digit = compute_digit(*digit, &ds);
Some(digit.to_digit())
}).collect();
let res: Vec<char> = dirs
.into_iter()
.scan(digit, |digit, ds| {
*digit = compute_digit(*digit, &ds);
Some(digit.to_digit())
})
.collect();
Some(String::from_iter(res))
}
pub fn part_one(input: &str) -> Option<String> {
solve::<PosP1>(input)
}
@@ -173,17 +160,17 @@ mod tests {
#[test]
fn test_posp1_to_digit() {
assert_eq!(PosP1 {x: -1, y: 1}.to_digit(), '1');
assert_eq!(PosP1 {x: 0, y: 1}.to_digit(), '2');
assert_eq!(PosP1 {x: 1, y: 1}.to_digit(), '3');
assert_eq!(PosP1 {x: -1, y: 0}.to_digit(), '4');
assert_eq!(PosP1 {x: 0, y: 0}.to_digit(), '5');
assert_eq!(PosP1 {x: 1, y: 0}.to_digit(), '6');
assert_eq!(PosP1 {x: -1, y: -1}.to_digit(), '7');
assert_eq!(PosP1 {x: 0, y: -1}.to_digit(), '8');
assert_eq!(PosP1 {x: 1, y: -1}.to_digit(), '9');
assert_eq!(PosP1 { x: -1, y: 1 }.to_digit(), '1');
assert_eq!(PosP1 { x: 0, y: 1 }.to_digit(), '2');
assert_eq!(PosP1 { x: 1, y: 1 }.to_digit(), '3');
assert_eq!(PosP1 { x: -1, y: 0 }.to_digit(), '4');
assert_eq!(PosP1 { x: 0, y: 0 }.to_digit(), '5');
assert_eq!(PosP1 { x: 1, y: 0 }.to_digit(), '6');
assert_eq!(PosP1 { x: -1, y: -1 }.to_digit(), '7');
assert_eq!(PosP1 { x: 0, y: -1 }.to_digit(), '8');
assert_eq!(PosP1 { x: 1, y: -1 }.to_digit(), '9');
}
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));