mirror of
https://github.com/mx42/aoc2016.git
synced 2026-01-14 13:49:53 +01:00
chore: fmt + clippy
This commit is contained in:
@@ -136,8 +136,7 @@ impl State {
|
||||
fn scan_steps(&mut self, steps: Vec<Step>) -> Vec<Pos> {
|
||||
steps
|
||||
.into_iter()
|
||||
.map(|step| self.all_locations_towards(&step))
|
||||
.flatten()
|
||||
.flat_map(|step| self.all_locations_towards(&step))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
@@ -158,13 +157,13 @@ fn parse_input(_input: &str) -> Vec<Step> {
|
||||
_input
|
||||
.to_string()
|
||||
.strip_suffix("\n")
|
||||
.unwrap_or(&_input)
|
||||
.unwrap_or(_input)
|
||||
.split(", ")
|
||||
.map(|s| {
|
||||
let (direction, value) = s.split_at(1);
|
||||
let length: u32 = value
|
||||
.parse()
|
||||
.expect(&format!("invalid length! {:?}", value).to_string());
|
||||
let length: u32 = value.parse().unwrap_or_else(|_| {
|
||||
panic!("{}", format!("invalid length! {:?}", value).to_string())
|
||||
});
|
||||
match direction {
|
||||
"L" => Step(Turn::Left, length),
|
||||
"R" => Step(Turn::Right, length),
|
||||
|
||||
105
src/bin/02.rs
105
src/bin/02.rs
@@ -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));
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
advent_of_code::solution!(3);
|
||||
|
||||
fn parse_line(input: &str) -> [u32; 3] {
|
||||
let nb: Result<Vec<u32>, _> = input
|
||||
.split_whitespace()
|
||||
.map(str::parse)
|
||||
.collect();
|
||||
let nb: Result<Vec<u32>, _> = input.split_whitespace().map(str::parse).collect();
|
||||
if let Ok(nb) = nb {
|
||||
if nb.len() == 3 {
|
||||
return [nb[0], nb[1], nb[2]];
|
||||
@@ -26,7 +23,7 @@ fn parse_input(input: &str) -> Vec<[u32; 3]> {
|
||||
fn parse_input_p2(input: &str) -> Vec<[u32; 3]> {
|
||||
parse_input(input)
|
||||
.chunks(3)
|
||||
.map(|c| {
|
||||
.flat_map(|c| {
|
||||
let v = c.to_vec();
|
||||
if v.len() != 3 {
|
||||
println!("{:?}", v);
|
||||
@@ -34,44 +31,23 @@ fn parse_input_p2(input: &str) -> Vec<[u32; 3]> {
|
||||
}
|
||||
let vs = [v[0], v[1], v[2]];
|
||||
// LAAAAAAZY... Could have transposed
|
||||
let [
|
||||
[ta1, tb1, tc1],
|
||||
[ta2, tb2, tc2],
|
||||
[ta3, tb3, tc3],
|
||||
] = vs;
|
||||
[
|
||||
[ta1, ta2, ta3],
|
||||
[tb1, tb2, tb3],
|
||||
[tc1, tc2, tc3],
|
||||
].to_vec()
|
||||
let [[ta1, tb1, tc1], [ta2, tb2, tc2], [ta3, tb3, tc3]] = vs;
|
||||
[[ta1, ta2, ta3], [tb1, tb2, tb3], [tc1, tc2, tc3]].to_vec()
|
||||
})
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn is_valid(triangle: &[u32; 3]) -> bool {
|
||||
let &[a, b, c] = triangle;
|
||||
(a + b) > c
|
||||
&& (b + c) > a
|
||||
&& (c + a) > b
|
||||
(a + b) > c && (b + c) > a && (c + a) > b
|
||||
}
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
Some(
|
||||
parse_input(input)
|
||||
.into_iter()
|
||||
.filter(is_valid)
|
||||
.count() as u32
|
||||
)
|
||||
Some(parse_input(input).into_iter().filter(is_valid).count() as u32)
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
Some(
|
||||
parse_input_p2(input)
|
||||
.into_iter()
|
||||
.filter(is_valid)
|
||||
.count() as u32
|
||||
)
|
||||
Some(parse_input_p2(input).into_iter().filter(is_valid).count() as u32)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user