Unable to load image

r/drama Advent of Code S02E01 - Official DISCUSSION Thread

Ok you Python-using heathens. What did you think for difficulty this time around?

Post your coding atrocities below.

For those of you who missed the early signups. Feel free to join the leaderboard late (though rankings are based on time).

42
Jump in the discussion.

No email address required.

Took me a long time to remember to do this but I beat the release of day two :)

I bounced off haskell immediately and cbf re-learning from scratch so it's rust season again:

// line, head, tail, recursive
fn part2(line: String, first: Option<char>, last: Option<char>) -> String {
   let mut new_first = first;
   let mut new_last = last;

   if line.len() == 0 {
       return format!("{}{}", first.unwrap(), last.unwrap());
   } else {
       for p in [
           ("one", '1'),
           ("two", '2'),
           ("three", '3'),
           ("four", '4'),
           ("five", '5'),
           ("six", '6'),
           ("seven", '7'),
           ("eight", '8'),
           ("nine", '9'),
       ] {
           if line.starts_with(p.0) {
               if first.is_none() {
                   new_first = Some(p.1);
               }
               new_last = Some(p.1);
               break;
           }
       }

       let mut ln_new = line.chars();
       let c = ln_new.next().unwrap();
       if c.is_digit(10) {
           if first.is_none() {
               new_first = Some(c);
           }
           new_last = Some(c);
       }

       return part2(ln_new.collect(), new_first, new_last);
   }
}

fn main() {
   // Part One
   let lines = include_str!("input.txt").lines().collect::<Vec<_>>();
   let mut n = 0;
   for line in lines.clone() {
       let mut first: Option<char> = None;
       let mut last: Option<char> = None;
       for c in line.chars() {
           if c.is_digit(10) {
               if first.is_none() {
                   first = Some(c);
               }
               last = Some(c);
           }
       }

       n += format!("{}{}", first.unwrap(), last.unwrap())
           .parse::<i32>()
           .unwrap();
   }

   println!("Part 1: {}", n);

   // Part Two
   let mut m = 0;
   for line in lines {
       m += part2(line.to_string(), None, None).parse::<i32>().unwrap();
   }

   println!("Part 2: {}", m);
}
Jump in the discussion.

No email address required.

I have to say rust is less ugly than I thought it'd be. Why did they choose let var: Type = ... instead of just Type var = ...?

Jump in the discussion.

No email address required.

Most of the time it will infer the type from what's passed in/used later, but yeah shit can get cluttered

Jump in the discussion.

No email address required.

You had a chance to not be completely worthless, but it looks like you threw it away. At least you're consistent.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.