some formatting and some optimizations
This commit is contained in:
@@ -5,16 +5,13 @@ 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() || acc.is_empty() {
|
if line.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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,7 +65,7 @@ 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 })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ fn main() -> std::io::Result<()> {
|
|||||||
*/
|
*/
|
||||||
//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
|
||||||
|
|||||||
Reference in New Issue
Block a user