diff --git a/2023/01-trebuchet/src/part_one.rs b/2023/01-trebuchet/src/part_one.rs index c5553f1..98fa680 100644 --- a/2023/01-trebuchet/src/part_one.rs +++ b/2023/01-trebuchet/src/part_one.rs @@ -3,10 +3,8 @@ use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; - pub fn part_one() { if let Ok(lines) = read_lines("./input.txt") { - let mut result = 0; for line in lines { @@ -17,33 +15,30 @@ pub fn part_one() { } } - - fn read_lines

(filename: P) -> io::Result>> -where P: AsRef, { +where + P: AsRef, +{ let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } -fn parse_line(s: &str) -> u32{ - +fn parse_line(s: &str) -> u32 { const RADIX: u32 = 10; - + let mut first_digit = 0; let mut last_digit = 0; for c in s.chars() { if c.is_digit(RADIX) { - let digit = c.to_digit(RADIX).unwrap(); if first_digit == 0 { first_digit = digit; } last_digit = digit; - } } - - return first_digit * 10 + last_digit -} \ No newline at end of file + + return first_digit * 10 + last_digit; +} diff --git a/2023/01-trebuchet/src/part_two.rs b/2023/01-trebuchet/src/part_two.rs index 0d8cc08..37c7ce0 100644 --- a/2023/01-trebuchet/src/part_two.rs +++ b/2023/01-trebuchet/src/part_two.rs @@ -3,10 +3,8 @@ use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; - pub fn part_two() { if let Ok(lines) = read_lines("./input.txt") { - let mut result = 0; for line in lines { @@ -18,16 +16,17 @@ pub fn part_two() { } fn read_lines

(filename: P) -> io::Result>> -where P: AsRef, { +where + P: AsRef, +{ let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } -fn parse_line(s: &str) -> u32{ - +fn parse_line(s: &str) -> u32 { const RADIX: u32 = 10; - let digits : HashMap<&str, u32> = HashMap::from([ + let digits: HashMap<&str, u32> = HashMap::from([ ("one", 1), ("two", 2), ("three", 3), @@ -38,36 +37,31 @@ fn parse_line(s: &str) -> u32{ ("eight", 8), ("nine", 9), ]); - + let mut first_digit = 0; let mut last_digit = 0; for i in 0..s.len() { - let c = s.chars().nth(i).unwrap(); if c.is_digit(RADIX) { - let digit = c.to_digit(RADIX).unwrap(); if first_digit == 0 { first_digit = digit; } - last_digit = digit; + last_digit = digit; } else { - for (k, v) in digits.iter() { if s[i..].starts_with(k) { - if first_digit == 0 { first_digit = *v; } last_digit = *v; } } - } } - - return first_digit * 10 + last_digit -} \ No newline at end of file + + return first_digit * 10 + last_digit; +} diff --git a/2023/02-cube-conundrum/src/part_one.rs b/2023/02-cube-conundrum/src/part_one.rs index c347b06..f14bcbf 100644 --- a/2023/02-cube-conundrum/src/part_one.rs +++ b/2023/02-cube-conundrum/src/part_one.rs @@ -2,10 +2,8 @@ use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; - pub fn part_one() { if let Ok(lines) = read_lines("./input.txt") { - let mut result = 0; for line in lines { @@ -16,18 +14,20 @@ pub fn part_one() { } } - fn read_lines

(filename: P) -> io::Result>> -where P: AsRef, { +where + P: AsRef, +{ let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } fn process_line(s: &str) -> u32 { - let v: Vec<&str> = s.split(':').collect(); - let game_id: u32 = v[0].split("Game ").collect::>()[1].parse::().unwrap(); + let game_id: u32 = v[0].split("Game ").collect::>()[1] + .parse::() + .unwrap(); let games: Vec<&str> = v[1].split(';').collect(); for game in games { @@ -39,28 +39,23 @@ fn process_line(s: &str) -> u32 { } return game_id; - } fn process_game(s: &str) -> bool { - let cubes: Vec<&str> = s.split(',').collect(); for cube in cubes { - let cube_parts: Vec<&str> = cube.trim().split(' ').collect(); let cube_num = cube_parts[0].parse::().unwrap(); if cube_parts[1] == "red" && cube_num > 12 { return false; - } else if cube_parts[1] == "green" && cube_num > 13{ + } else if cube_parts[1] == "green" && cube_num > 13 { return false; - } - else if cube_parts[1] == "blue" && cube_num > 14{ + } else if cube_parts[1] == "blue" && cube_num > 14 { return false; } } return true; - -} \ No newline at end of file +} diff --git a/2023/02-cube-conundrum/src/part_two.rs b/2023/02-cube-conundrum/src/part_two.rs index c85f295..671eb80 100644 --- a/2023/02-cube-conundrum/src/part_two.rs +++ b/2023/02-cube-conundrum/src/part_two.rs @@ -1,18 +1,16 @@ +use std::cmp::max; use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; -use std::cmp::max; struct Game { red: u32, green: u32, - blue: u32 + blue: u32, } - pub fn part_two() { if let Ok(lines) = read_lines("./input.txt") { - let mut result = 0; for line in lines { @@ -23,21 +21,27 @@ pub fn part_two() { } } - fn read_lines

(filename: P) -> io::Result>> -where P: AsRef, { +where + P: AsRef, +{ let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } fn process_line(s: &str) -> u32 { - let v: Vec<&str> = s.split(':').collect(); - let game_id: u32 = v[0].split("Game ").collect::>()[1].parse::().unwrap(); + let game_id: u32 = v[0].split("Game ").collect::>()[1] + .parse::() + .unwrap(); let games: Vec<&str> = v[1].split(';').collect(); - let mut min_game = Game { red: 0, green: 0, blue: 0 }; + let mut min_game = Game { + red: 0, + green: 0, + blue: 0, + }; for game in games { let game = process_game(game); @@ -48,29 +52,29 @@ fn process_line(s: &str) -> u32 { } return min_game.red * min_game.green * min_game.blue; - } fn process_game(s: &str) -> Game { - let mut game = Game { red: 0, green: 0, blue: 0 }; + let mut game = Game { + red: 0, + green: 0, + blue: 0, + }; let cubes: Vec<&str> = s.split(',').collect(); for cube in cubes { - let cube_parts: Vec<&str> = cube.trim().split(' ').collect(); let cube_num = cube_parts[0].parse::().unwrap(); if cube_parts[1] == "red" { game.red = cube_num; - } else if cube_parts[1] == "green"{ + } else if cube_parts[1] == "green" { game.green = cube_num; - } - else if cube_parts[1] == "blue"{ + } else if cube_parts[1] == "blue" { game.blue = cube_num; } } return game; - -} \ No newline at end of file +}