Reformat files

This commit is contained in:
LordMathis 2023-12-03 17:37:34 +01:00
parent 3fc02ca06b
commit 7a5c968572
4 changed files with 48 additions and 60 deletions

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
where
P: AsRef<Path>,
{
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
}
return first_digit * 10 + last_digit;
}

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
where
P: AsRef<Path>,
{
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
}
return first_digit * 10 + last_digit;
}

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
where
P: AsRef<Path>,
{
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::<Vec<&str>>()[1].parse::<u32>().unwrap();
let game_id: u32 = v[0].split("Game ").collect::<Vec<&str>>()[1]
.parse::<u32>()
.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::<u32>().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;
}
}

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
where
P: AsRef<Path>,
{
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::<Vec<&str>>()[1].parse::<u32>().unwrap();
let game_id: u32 = v[0].split("Game ").collect::<Vec<&str>>()[1]
.parse::<u32>()
.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::<u32>().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;
}
}