Skip to content

Commit

Permalink
clippy + some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
majcn committed Sep 12, 2023
1 parent ba0d817 commit 767395c
Show file tree
Hide file tree
Showing 29 changed files with 536 additions and 642 deletions.
4 changes: 2 additions & 2 deletions src/bin/01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn part_x(data: Vec<Vec<u32>>) -> Vec<u32> {
pub fn part_one(input: &str) -> Option<u32> {
let data = parse_data(input);

let result = part_x(data).into_iter().rev().next();
let result = part_x(data).into_iter().next_back().unwrap();

result
Some(result)
}

pub fn part_two(input: &str) -> Option<u32> {
Expand Down
44 changes: 22 additions & 22 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
fn parse_data(input: &str) -> Vec<(u8, u8)> {
fn parse_data(input: &str) -> Vec<(char, char)> {
input
.lines()
.map(|x| x.as_bytes())
.map(|x| (x[0], x[2]))
.map(|x| (x[0].into(), x[2].into()))
.collect()
}

pub fn part_one(input: &str) -> Option<u32> {
let data = parse_data(input);

// TODO: const fn VS fn?
fn score((x, y): (u8, u8)) -> u32 {
fn score((x, y): (char, char)) -> u32 {
match (x, y) {
(b'A', b'X') => 3 + 1,
(b'A', b'Y') => 6 + 2,
(b'A', b'Z') => 0 + 3,
(b'B', b'X') => 0 + 1,
(b'B', b'Y') => 3 + 2,
(b'B', b'Z') => 6 + 3,
(b'C', b'X') => 6 + 1,
(b'C', b'Y') => 0 + 2,
(b'C', b'Z') => 3 + 3,
('A', 'X') => 4,
('A', 'Y') => 8,
('A', 'Z') => 3,
('B', 'X') => 1,
('B', 'Y') => 5,
('B', 'Z') => 9,
('C', 'X') => 7,
('C', 'Y') => 2,
('C', 'Z') => 6,
_ => unreachable!(),
}
}
Expand All @@ -33,17 +33,17 @@ pub fn part_one(input: &str) -> Option<u32> {
pub fn part_two(input: &str) -> Option<u32> {
let data = parse_data(input);

fn score((x, y): (u8, u8)) -> u32 {
fn score((x, y): (char, char)) -> u32 {
match (x, y) {
(b'A', b'X') => 0 + 3,
(b'A', b'Y') => 3 + 1,
(b'A', b'Z') => 6 + 2,
(b'B', b'X') => 0 + 1,
(b'B', b'Y') => 3 + 2,
(b'B', b'Z') => 6 + 3,
(b'C', b'X') => 0 + 2,
(b'C', b'Y') => 3 + 3,
(b'C', b'Z') => 6 + 1,
('A', 'X') => 3,
('A', 'Y') => 4,
('A', 'Z') => 8,
('B', 'X') => 1,
('B', 'Y') => 5,
('B', 'Z') => 9,
('C', 'X') => 2,
('C', 'Y') => 6,
('C', 'Z') => 7,
_ => unreachable!(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn parse_data(input: &str) -> Vec<&[u8]> {
}

fn priority(s: u8) -> u32 {
if (b'a'..=b'z').contains(&s) {
if s.is_ascii_lowercase() {
(s as u32) - 96
} else {
(s as u32) - 38
Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn part_two(input: &str) -> Option<u32> {
let data = parse_data(input);

let result = data
.chunks(3)
.chunks_exact(3)
.map(|c| {
let p1 = c[0].iter().collect::<BTreeSet<&u8>>();
let p2 = c[1].iter().collect::<BTreeSet<&u8>>();
Expand Down
13 changes: 4 additions & 9 deletions src/bin/04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ use regex::Regex;
use std::collections::BTreeSet;
use std::ops::RangeInclusive;

use advent_of_code::util::parse::ParseRegex;

fn parse_data(input: &str) -> Vec<(RangeInclusive<u32>, RangeInclusive<u32>)> {
let re = Regex::new(r"^(\d+)-(\d+),(\d+)-(\d+)$").unwrap();

input
.lines()
.map(|x| {
let captures = re.captures(x).unwrap();
let r1: RangeInclusive<u32> =
captures[1].parse().unwrap()..=captures[2].parse().unwrap();
let r2: RangeInclusive<u32> =
captures[3].parse().unwrap()..=captures[4].parse().unwrap();

(r1, r2)
})
.map(|x| re.parse_u32(x))
.map(|[r1_min, r1_max, r2_min, r2_max]| (r1_min..=r1_max, r2_min..=r2_max))
.collect()
}

Expand Down
22 changes: 10 additions & 12 deletions src/bin/05.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use regex::Regex;

use advent_of_code::util::parse::ParseRegex;

struct Command {
n: usize,
from: usize,
Expand All @@ -13,15 +15,13 @@ fn parse_data(input: &str) -> (State, Vec<Command>) {
let (state_lines, command_lines) = input.split_once("\n\n").unwrap();

let re = Regex::new(r"^move (\d+) from (\d+) to (\d+)$").unwrap();
let commands: Vec<Command> = command_lines
let commands = command_lines
.lines()
.map(|x| {
let captures = re.captures(x).unwrap();
Command {
n: captures[1].parse().unwrap(),
from: captures[2].parse::<usize>().unwrap() - 1,
to: captures[3].parse::<usize>().unwrap() - 1,
}
.map(|x| re.parse_usize(x))
.map(|[n, from, to]| Command {
n,
from: from - 1,
to: to - 1,
})
.collect();

Expand All @@ -42,8 +42,7 @@ fn parse_data(input: &str) -> (State, Vec<Command>) {
}

pub fn part_one(input: &str) -> Option<String> {
let (state_original, commands) = parse_data(input);
let mut state = state_original.clone(); // to_vec();? // TODO: preveri ce to sploh dela na tak nacin (nested?)
let (mut state, commands) = parse_data(input);

for command in commands {
let mut stack_from = std::mem::take(&mut state[command.from]);
Expand All @@ -59,8 +58,7 @@ pub fn part_one(input: &str) -> Option<String> {
}

pub fn part_two(input: &str) -> Option<String> {
let (state_original, commands) = parse_data(input);
let mut state = state_original.iter().cloned().collect::<Vec<_>>(); // TODO: preveri ce to sploh dela na tak nacin
let (mut state, commands) = parse_data(input);

for command in commands {
let mut stack_from = std::mem::take(&mut state[command.from]);
Expand Down
4 changes: 2 additions & 2 deletions src/bin/06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ fn part_x<const N: usize>(data: &[u8]) -> u32 {
pub fn part_one(input: &str) -> Option<u32> {
let data = parse_data(input);

let result = part_x::<4>(&data);
let result = part_x::<4>(data);

Some(result)
}

pub fn part_two(input: &str) -> Option<u32> {
let data = parse_data(input);

let result = part_x::<14>(&data);
let result = part_x::<14>(data);

Some(result)
}
Expand Down
5 changes: 3 additions & 2 deletions src/bin/07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ pub fn part_two(input: &str) -> Option<u32> {
.into_iter()
.map(|folder| calculate_size(&data, folder))
.filter(|size| used_disk_space - size < TOTAL_DISK_SPACE - REQUIRED_DISK_SPACE)
.min();
.min()
.unwrap();

result
Some(result)
}

fn main() {
Expand Down
26 changes: 11 additions & 15 deletions src/bin/09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Command {
fn parse_data(input: &str) -> Vec<Command> {
input
.lines()
.map(|x| x.split_once(" ").unwrap())
.map(|x| x.split_once(' ').unwrap())
.map(|x| Command {
direction: x.0.as_bytes()[0],
steps: x.1.parse().unwrap(),
Expand All @@ -38,24 +38,20 @@ fn part_x<const N: usize>(data: &[Command]) -> u32 {
}

for i in 1..N {
let head = rope[(i - 1) as usize];
let tail = &mut rope[i as usize];
let head = rope[i - 1];
let tail = &mut rope[i];

if (head.x - tail.x).abs() > 1 || (head.y - tail.y).abs() > 1 {
let diff_x = if head.x < tail.x {
-1
} else if head.x > tail.x {
1
} else {
0
let diff_x = match Ord::cmp(&head.x, &tail.x) {
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Greater => 1,
std::cmp::Ordering::Equal => 0,
};

let diff_y = if head.y < tail.y {
-1
} else if head.y > tail.y {
1
} else {
0
let diff_y = match Ord::cmp(&head.y, &tail.y) {
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Greater => 1,
std::cmp::Ordering::Equal => 0,
};

*tail += Point::new(diff_x, diff_y);
Expand Down
36 changes: 12 additions & 24 deletions src/bin/10.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod interpreter {
enum Command {
NOOP,
ADDX(i32),
Noop,
AddX(i32),
}

// TODO; malo poglej tele tipe. malo so smesni
// kako se drgac na simpl handla negativne stevilke pri -1
pub struct Interpreter<'a> {
program: Vec<&'a str>,
program_index: usize,
Expand All @@ -22,7 +20,7 @@ mod interpreter {
program,
program_index: 0,
current_program_progress: 0,
current_command: Command::NOOP,
current_command: Command::Noop,
register_x: 1,
cycle: 0,
halt: false,
Expand All @@ -35,28 +33,21 @@ mod interpreter {

fn init_next_command(&mut self) {
let command = self.program[self.program_index];
let (command_name, command_value) =
command.split_once(" ").unwrap_or_else(|| (command, ""));
let (command_name, command_value) = command.split_once(' ').unwrap_or((command, ""));

match command_name {
"noop" => {
self.current_program_progress = 1;
self.current_command = Command::NOOP;
}
"addx" => {
self.current_program_progress = 2;
self.current_command = Command::ADDX(command_value.parse().unwrap());
}
_ => {}
}
(self.current_program_progress, self.current_command) = match command_name {
"noop" => (1, Command::Noop),
"addx" => (2, Command::AddX(command_value.parse().unwrap())),
_ => unreachable!(),
};

self.program_index += 1;
}

fn execute_command(&mut self) {
self.register_x += match self.current_command {
Command::NOOP => 0,
Command::ADDX(v) => v,
Command::Noop => 0,
Command::AddX(v) => v,
}
}

Expand Down Expand Up @@ -115,10 +106,7 @@ pub fn part_two(input: &str) -> Option<String> {
let x = p.cycle % 40;
let y = p.cycle / 40;

display[y][x] = match p.register_x - x as i32 {
-1 | 0 | 1 => true,
_ => false,
};
display[y][x] = matches!(p.register_x - x as i32, -1 | 0 | 1);

p.exec_single_cycle();
}
Expand Down
Loading

0 comments on commit 767395c

Please sign in to comment.