Reformat files
This commit is contained in:
parent
3fc02ca06b
commit
7a5c968572
|
@ -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,16 +15,15 @@ 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 {
|
||||
|
||||
const RADIX: u32 = 10;
|
||||
|
||||
let mut first_digit = 0;
|
||||
|
@ -34,16 +31,14 @@ fn parse_line(s: &str) -> u32{
|
|||
|
||||
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;
|
||||
}
|
|
@ -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,13 +16,14 @@ 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 {
|
||||
|
||||
const RADIX: u32 = 10;
|
||||
|
||||
let digits: HashMap<&str, u32> = HashMap::from([
|
||||
|
@ -43,11 +42,9 @@ fn parse_line(s: &str) -> u32{
|
|||
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 {
|
||||
|
@ -55,19 +52,16 @@ fn parse_line(s: &str) -> u32{
|
|||
}
|
||||
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;
|
||||
}
|
|
@ -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,15 +39,12 @@ 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();
|
||||
|
||||
|
@ -55,12 +52,10 @@ fn process_game(s: &str) -> bool {
|
|||
return false;
|
||||
} 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;
|
||||
|
||||
}
|
|
@ -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,16 +52,18 @@ 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();
|
||||
|
||||
|
@ -65,12 +71,10 @@ fn process_game(s: &str) -> Game {
|
|||
game.red = cube_num;
|
||||
} 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;
|
||||
|
||||
}
|
Loading…
Reference in New Issue