Unable to load image

Advent of code day 4 :marseysad:

This is the advent of code day 4 thread for bragging about your solution and bullying people who haven't gotten it yet :marseyill:

35
Jump in the discussion.

No email address required.

Anyone else doing Rust?

![](/images/16701799855292351.webp)

Jump in the discussion.

No email address required.

Why even use rust at this point? folds without named functions make code almost unreadable (readable, but you have to spend more time to understand it)

Anyway, here's mine:

use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str::FromStr;


struct Range {
    start: i32,
    finish: i32,
}


impl Range {
    fn includes(&self, other: &Range) -> bool {
        return self.start <= other.start && self.finish >= other.finish;
    }

    fn overlaps(&self, other: &Range) -> bool {
        return self.finish >= other.start && self.start <= other.finish
    }
}


impl FromStr for Range {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (start, finish) = s.split_once('-').ok_or_else(|| "no '-' in range")?;
        let start = start.parse::<i32>().map_err(|_| "invalid start")?;
        let finish = finish.parse::<i32>().map_err(|_| "invalid finish")?;
        Ok(Range{start, finish})
    }
}


fn main() {
    let file = File::open("input.txt").expect("Can't open input file");
    let reader = BufReader::new(file);

    let result = reader.lines().filter(|line| {
        let line = line.as_ref().expect("Can't read a line from file");
        let (left, right) = line.split_once(',').expect("No ',' in line");
        let left = left.parse::<Range>().expect("Can't convert");
        let right = right.parse::<Range>().expect("Can't convert");
        left.overlaps(&right) // left.includes(&right) || right.includes(&left)
    }).count();

    println!("{}", result);
}
Jump in the discussion.

No email address required.

Why even use rust at this point?

I like the language

folds without named functions make code almost unreadable (readable, but you have to spend more time to understand it)

for loops are evil

Jump in the discussion.

No email address required.

I'm not a masochist, it felt like I was staring at black boxes hidden in more boxes looking at stdlib. might just be r-slurred though

Jump in the discussion.

No email address required.

Filtered

:#marseyitsover:

Jump in the discussion.

No email address required.

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