some formatting and some optimizations

This commit is contained in:
Dylan "smellyfis" Thies
2022-12-12 16:03:42 -05:00
parent 4ef68d5f36
commit b85cc13465
3 changed files with 47 additions and 47 deletions

View File

@@ -1,25 +1,22 @@
use std::fs::File; use std::fs::File;
use std::io::{prelude::*, BufReader}; use std::io::{prelude::*, BufReader};
fn main() -> std::io::Result<()>{ fn main() -> std::io::Result<()> {
let file = File::open("input")?; let file = File::open("input")?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
let mut elves = reader.lines() let mut elves = reader.lines().fold(vec![0_u64], |mut acc, line| {
.fold(Vec::new(), |mut acc, line| { let line = line.unwrap();
let line = line.unwrap(); //empty lines mean new elf
//empty lines mean new elf if line.is_empty() {
if line.is_empty() || acc.is_empty() { acc.push(0_u64);
acc.push(0_u64); } else {
}
// the first time through is an edge case preventing an else here // the first time through is an edge case preventing an else here
if ! line.is_empty() { let last = acc.last_mut().unwrap();
let last = acc.last_mut().unwrap(); *last += line.parse::<u64>().unwrap();
*last += line.parse::<u64>().unwrap(); }
} acc
acc });
});
//order the elves since we don't care about position anymore //order the elves since we don't care about position anymore
elves.sort(); elves.sort();
@@ -29,7 +26,7 @@ fn main() -> std::io::Result<()>{
println!("Part 1: {}", elves[max]); println!("Part 1: {}", elves[max]);
//Part 2 is get the sum of the largest 3 //Part 2 is get the sum of the largest 3
let counts: u64 = elves[(max-2)..].iter().sum(); let counts: u64 = elves[(max - 2)..].iter().sum();
println!("Part 2: {counts}"); println!("Part 2: {counts}");
Ok(()) Ok(())

View File

@@ -2,10 +2,10 @@ use std::fs::File;
use std::io::{prelude::*, BufReader}; use std::io::{prelude::*, BufReader};
#[derive(Debug)] #[derive(Debug)]
struct HoHoError{} struct HoHoError {}
#[derive(PartialEq)] #[derive(PartialEq)]
enum Choice{ enum Choice {
Rock = 1, Rock = 1,
Paper, Paper,
Scissors, Scissors,
@@ -18,7 +18,7 @@ impl std::str::FromStr for Choice {
"A" | "X" => Ok(Choice::Rock), "A" | "X" => Ok(Choice::Rock),
"B" | "Y" => Ok(Choice::Paper), "B" | "Y" => Ok(Choice::Paper),
"C" | "Z" => Ok(Choice::Scissors), "C" | "Z" => Ok(Choice::Scissors),
_ => Err(HoHoError{}), _ => Err(HoHoError {}),
} }
} }
} }
@@ -33,7 +33,7 @@ impl Choice {
} }
0 0
} }
fn beats (&self) -> Choice { fn beats(&self) -> Choice {
match self { match self {
Choice::Rock => Choice::Scissors, Choice::Rock => Choice::Scissors,
Choice::Paper => Choice::Rock, Choice::Paper => Choice::Rock,
@@ -49,7 +49,7 @@ impl Choice {
} }
} }
struct Game{ struct Game {
pub opponent: Choice, pub opponent: Choice,
pub you: Choice, pub you: Choice,
} }
@@ -57,7 +57,7 @@ struct Game{
impl std::str::FromStr for Game { impl std::str::FromStr for Game {
type Err = HoHoError; type Err = HoHoError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let str_split = s.split(" ").collect::<Vec<&str>>(); let str_split = s.split(' ').collect::<Vec<&str>>();
let opponent: Choice = str_split[0].parse()?; let opponent: Choice = str_split[0].parse()?;
// game1 // game1
//let you: Choice = str_split[1].parse()?; //let you: Choice = str_split[1].parse()?;
@@ -65,14 +65,14 @@ impl std::str::FromStr for Game {
"X" => opponent.beats(), "X" => opponent.beats(),
"Y" => str_split[0].parse()?, "Y" => str_split[0].parse()?,
"Z" => opponent.loses(), "Z" => opponent.loses(),
_ => return Err(HoHoError{}) _ => return Err(HoHoError {}),
}; };
Ok(Game{opponent, you}) Ok(Game { opponent, you })
} }
} }
impl Game { impl Game {
fn outcome (&self) -> i32 { fn outcome(&self) -> i32 {
self.you.cmp(&self.opponent) self.you.cmp(&self.opponent)
} }
@@ -82,7 +82,7 @@ impl Game {
} }
} }
fn main() -> std::io::Result<()>{ fn main() -> std::io::Result<()> {
//read in file //read in file
let file = File::open("input")?; let file = File::open("input")?;
let reader = BufReader::new(file); let reader = BufReader::new(file);

View File

@@ -7,24 +7,25 @@ fn main() -> std::io::Result<()> {
let reader = BufReader::new(file); let reader = BufReader::new(file);
/* /*
let value = reader let value = reader
.lines() .lines()
.map(|line| { .map(|line| {
let line = line.unwrap(); let line = line.unwrap();
let (comp1, comp2) = line.split_at(line.len() / 2); let (comp1, comp2) = line.split_at(line.len() / 2);
let duplicate = comp2.chars().find(|c| comp1.contains(*c)).unwrap(); let duplicate = comp2.chars().find(|c| comp1.contains(*c)).unwrap();
match duplicate { match duplicate {
n @'a'..='z' => (n as i32) - ('a' as i32) + 1_i32, n @'a'..='z' => (n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (n as i32) - ('A' as i32) + 27_i32, n @ 'A'..='Z' => (n as i32) - ('A' as i32) + 27_i32,
_ => 0, _ => 0,
} }
}) })
.sum::<i32>(); .sum::<i32>();
println!("Part 1: {value}"); println!("Part 1: {value}");
*/ */
//part 2 //part 2
// fold the lines into groups of three // fold the lines into groups of three
let value = reader.lines() let value = reader
.lines()
.fold(Vec::new(), |mut acc: Vec<Vec<String>>, line| { .fold(Vec::new(), |mut acc: Vec<Vec<String>>, line| {
if acc.is_empty() || acc.last().unwrap().len() == 3 { if acc.is_empty() || acc.last().unwrap().len() == 3 {
acc.push(Vec::new()) acc.push(Vec::new())
@@ -38,7 +39,8 @@ fn main() -> std::io::Result<()> {
[g1, g2, g3] => (g1, g2, g3), [g1, g2, g3] => (g1, g2, g3),
_ => panic!("not get here"), _ => panic!("not get here"),
}; };
match g1.chars() match g1
.chars()
.fold(Vec::new(), |mut combo: Vec<char>, ch| { .fold(Vec::new(), |mut combo: Vec<char>, ch| {
if g2.contains(ch) { if g2.contains(ch) {
combo.push(ch) combo.push(ch)
@@ -46,14 +48,15 @@ fn main() -> std::io::Result<()> {
combo combo
}) })
.iter() .iter()
.find(|c| { .find(|c| g3.contains(**c))
g3.contains(**c) .unwrap()
}).unwrap(){ {
n @ 'a'..='z' => (*n as i32) - ('a' as i32) + 1_i32, n @ 'a'..='z' => (*n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (*n as i32) - ('A' as i32) + 27_i32, n @ 'A'..='Z' => (*n as i32) - ('A' as i32) + 27_i32,
_ => 0, _ => 0,
} }
}).sum::<i32>(); })
.sum::<i32>();
println!("Part 2: {value}"); println!("Part 2: {value}");
// find common letter in the groups // find common letter in the groups
// find common letters in the first 2 then find the common in the third // find common letters in the first 2 then find the common in the third