You brehs ready for this year's 'Advent of Code' ?

TheAnointedOne

Superstar
Joined
Jul 30, 2012
Messages
7,814
Reputation
676
Daps
30,789

I know there's a great number of 6 figure, 6 cert brehs on here. It's highly encouraged that you participate in this.

It's a yearly coding competition. They release a new puzzle for 25 days in December. All of the puzzles revolve around a Christmas-esque theme. The daily puzzles increase in difficulty.

You can look at past puzzles to get an idea of what this is about.

---- Edit ----

Was trending on twitter
 
Last edited:

Ryze

All Star
Joined
Mar 11, 2022
Messages
1,192
Reputation
277
Daps
5,413
:russ:Leaderboard gets stacked quickly. I guess I'll be playing for fun to see how far I can get
 

TheAnointedOne

Superstar
Joined
Jul 30, 2012
Messages
7,814
Reputation
676
Daps
30,789
My solution in rust.

Code:
fn main() {
    let file_path = "resources/day1.txt" ;
    let calories = get_data_by_line( file_path ).unwrap() ;        

    let grouped = calories.split( |x| x.is_empty() ) ;

    let sums = grouped.into_iter().map( |x| {

        x.iter().fold( 0, |accum, item| accum + item.parse::<u32>().unwrap() )
    })
    .collect::<Vec<u32>>() ;    

    // part one
    let m = sums.iter().max().unwrap() ;
    println!( "{}", m ) ;    

    // part two
    let m = max_nth( sums, 3 ) ;
    println!( "{:?}", m.iter().sum::<u32>() ) ;    
}

fn max_nth( input:Vec<u32>, n:usize ) -> Vec<u32> {

    let mut ret = input ;

    ret.sort() ;
    ret.reverse() ;

    ret[0..n].to_vec()
}

fn get_data_by_line( filePath:&str ) -> std::io::Result<Vec<String>> {

    let file = File::open( filePath )? ;    
    let buffer = BufReader::new( file ) ;

    let mut lines = Vec::new() ;

    for line in buffer.lines() {
        lines.push( line? ) ;
    }

    Ok(lines)
}

Code sloppy will refactor later
 
Top