Skip to content

Commit

Permalink
Improvement: Multiple exercises are now more efficients
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 24, 2024
1 parent 765167c commit 11a4438
Show file tree
Hide file tree
Showing 20 changed files with 357 additions and 429 deletions.
12 changes: 6 additions & 6 deletions src/year_2015/day_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@
/// ```
pub fn day_01_v1(input: impl Into<String>) -> i16 {
let mut lvl: i16 = 0;
for chr in input.into().chars() {
for chr in input.into().bytes() {
match chr {
'(' => lvl += 1,
')' => lvl -= 1,
b'(' => lvl += 1,
b')' => lvl -= 1,
_ => panic!("Invalid character: {}", chr),
}
}
Expand Down Expand Up @@ -135,10 +135,10 @@ pub fn day_01_v1(input: impl Into<String>) -> i16 {
/// ```
pub fn day_01_v2(input: impl Into<String>) -> i16 {
let mut lvl: i16 = 0;
for (idx, chr) in input.into().chars().enumerate() {
for (idx, chr) in input.into().bytes().enumerate() {
match chr {
'(' => lvl += 1,
')' => lvl -= 1,
b'(' => lvl += 1,
b')' => lvl -= 1,
_ => panic!("Invalid character: {}", chr),
}
if lvl < 0 {
Expand Down
74 changes: 31 additions & 43 deletions src/year_2015/day_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,75 +30,63 @@
//! > Your puzzle answer was ~~`REDACTED`~~.
//!
use std::str::FromStr;

#[derive(Debug, PartialEq)]
struct PresentBox {
height: u8,
width: u8,
length: u8,
height: u16,
width: u16,
length: u16,
}

impl PresentBox {
fn new(input: &str) -> Self {
let mut nums: Vec<u16> = input
.split('x')
.map(|num| num.parse::<u16>().unwrap())
.collect();
nums.sort();

PresentBox {
height: nums[0],
width: nums[1],
length: nums[2],
}
}

fn area_small(&self) -> u16 {
self.height as u16 * self.width as u16
self.height * self.width
}

fn area_med(&self) -> u16 {
self.width as u16 * self.length as u16
self.width * self.length
}

fn area_large(&self) -> u16 {
self.length as u16 * self.height as u16
self.length * self.height
}

fn wrapper(&self) -> u16 {
(3 * self.area_small()) + (2 * self.area_med()) + (2 * self.area_large())
}

fn ribbon(&self) -> u16 {
(self.height as u16 * 2) + (self.width as u16 * 2) + (self.area_small() * self.length as u16)
}
}

impl FromStr for PresentBox {
type Err = std::num::ParseIntError;

fn from_str(input: &str) -> Result<Self, Self::Err> {
let num_str: Vec<&str> = input.split('x').collect();
let mut num_int: [u8; 3] = [
num_str[0].parse::<u8>().unwrap(),
num_str[1].parse::<u8>().unwrap(),
num_str[2].parse::<u8>().unwrap(),
];
num_int.sort();

Ok(PresentBox {
height: num_int[0],
width: num_int[1],
length: num_int[2],
})
(self.height * 2) + (self.width * 2) + (self.area_small() * self.length)
}
}

pub fn day_02_v1(input: impl Into<String>) -> u32 {
let mut total: u32 = 0;
for line in input.into().lines() {
if let Ok(present) = PresentBox::from_str(line) {
total += present.wrapper() as u32
}
}
total
input
.into()
.lines()
.map(|line| PresentBox::new(line).wrapper() as u32)
.sum()
}

pub fn day_02_v2(input: impl Into<String>) -> u32 {
let mut total: u32 = 0;
for line in input.into().lines() {
if let Ok(present) = PresentBox::from_str(line) {
total += present.ribbon() as u32
}
}
total
input
.into()
.lines()
.map(|line| PresentBox::new(line).ribbon() as u32)
.sum()
}

solvable!(day_02, day_02_v1, day_02_v2, u32);
Expand Down
40 changes: 14 additions & 26 deletions src/year_2015/day_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,45 @@
//! > Your puzzle answer was ~~`REDACTED`~~.
//!
use itertools::Itertools;
use std::collections::HashSet;

#[inline]
fn move_character(mut pos: (i8, i8), direction: char) -> (i8, i8) {
fn move_character(pos: &mut (i8, i8), direction: u8) {
match direction {
'>' => pos.0 += 1,
'<' => pos.0 -= 1,
'v' => pos.1 += 1,
'^' => pos.1 -= 1,
b'>' => pos.0 += 1,
b'<' => pos.0 -= 1,
b'v' => pos.1 += 1,
b'^' => pos.1 -= 1,
_ => panic!("Invalid direction character: {direction}"),
}
pos
// pos
}

pub fn day_03_v1(input: impl Into<String>) -> usize {
let mut santa: (i8, i8) = (0, 0);
let mut houses = HashSet::from([santa]);

for chr in input.into().chars() {
santa = move_character(santa, chr);
for direction in input.into().bytes() {
move_character(&mut santa, direction);
houses.insert(santa);
}

houses.len()
}

pub fn day_03_v2(input: impl Into<String>) -> usize {
let mut santa: (i8, i8) = (0, 0);
let mut robot: (i8, i8) = (0, 0);
let mut houses = HashSet::from([santa]);
let moves: Vec<char> = input.into().chars().collect();

for chars in moves.chunks(2) {
santa = move_character(santa, chars[0]);
robot = move_character(robot, chars[1]);
for mut chars in &input.into().bytes().chunks(2) {
move_character(&mut santa, chars.next().unwrap());
move_character(&mut robot, chars.next().unwrap());
houses.insert(santa);
houses.insert(robot);
}

houses.len()
}

Expand All @@ -78,20 +80,6 @@ solvable!(day_03, day_03_v1, day_03_v2, usize);
mod tests {
use super::*;

#[test]
fn moves_characters_properly() {
let sample_moves: [((i8, i8), char, (i8, i8)); 4] = [
((0, 0), '>', (1, 0)),
((0, 0), '<', (-1, 0)),
((0, 0), '^', (0, -1)),
((0, 0), 'v', (0, 1)),
];

for (sample, direction, result) in sample_moves.iter() {
assert_eq!(move_character(*sample, *direction), *result);
}
}

#[test]
fn works_with_samples_v1() {
let sample_one: [(&str, usize); 3] = [
Expand Down
14 changes: 7 additions & 7 deletions src/year_2015/day_05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn string_is_nice_v1(input: &str) -> bool {
fn string_is_nice_v2(input: &str) -> bool {
let mut twice_pair = false;
let mut repeated = false;
let letters: Vec<_> = input.chars().collect();
let letters: Vec<_> = input.as_bytes().to_vec();
for i in 0..(input.len() - 2) {
if letters[i] == letters[i + 2] {
repeated = true;
Expand All @@ -89,19 +89,19 @@ fn string_is_nice_v2(input: &str) -> bool {
}

pub fn day_05_v1(input: impl Into<String>) -> usize {
let input_str = input.into();
return input_str
input
.into()
.lines()
.filter(|line| string_is_nice_v1(line))
.count();
.count()
}

pub fn day_05_v2(input: impl Into<String>) -> usize {
let input_str = input.into();
return input_str
input
.into()
.lines()
.filter(|line| string_is_nice_v2(line))
.count();
.count()
}

solvable!(day_05, day_05_v1, day_05_v2, usize);
Expand Down
Loading

0 comments on commit 11a4438

Please sign in to comment.