@CannotSleep420@lemmygrad.ml

CannotSleep420

@CannotSleep420@lemmygrad.ml

(he/him)

vanguard

Terminally online insomniac code monkey from burgerland. Deeply unserious person.

This profile is from a federated server and may be incomplete. Browse more on the original instance.

CannotSleep420,

I’m a bit late to the party for day 2. Here’s my answer:


<span style="color:#323232;">use std::rc::Rc;
</span><span style="color:#323232;">
</span><span style="color:#323232;">use crate::utils::read_lines;
</span><span style="color:#323232;">
</span><span style="color:#323232;">#[derive(Clone, Copy, PartialEq, Eq)]
</span><span style="color:#323232;">enum Cube {
</span><span style="color:#323232;">    Red(usize),
</span><span style="color:#323232;">    Green(usize),
</span><span style="color:#323232;">    Blue(usize),
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">#[derive(Clone, Copy, PartialEq, Eq)]
</span><span style="color:#323232;">struct Bag {
</span><span style="color:#323232;">    pub red: usize,
</span><span style="color:#323232;">    pub green: usize,
</span><span style="color:#323232;">    pub blue: usize,
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">fn parse_num&lt;'a, I>(mut group: I) -> usize
</span><span style="color:#323232;">where
</span><span style="color:#323232;">    I: Iterator,
</span><span style="color:#323232;">{
</span><span style="color:#323232;">    group
</span><span style="color:#323232;">        .next()
</span><span style="color:#323232;">        .expect("Should be number")
</span><span style="color:#323232;">        .parse::()
</span><span style="color:#323232;">        .expect("Should be number")
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">fn process_lines() -> impl Iterator> {
</span><span style="color:#323232;">    read_lines("src/day_2/input.txt")
</span><span style="color:#323232;">        .expect("Could not read file")
</span><span style="color:#323232;">        .map(|line| {
</span><span style="color:#323232;">            let line = line.expect("Should be line");
</span><span style="color:#323232;">            (&amp;line[(line.find(':').unwrap() + 1)..])
</span><span style="color:#323232;">                .split(';')
</span><span style="color:#323232;">                .flat_map(|section| section.split(","))
</span><span style="color:#323232;">                .map(|group| {
</span><span style="color:#323232;">                    let mut group = group.trim().split(' ');
</span><span style="color:#323232;">
</span><span style="color:#323232;">                    match group.next_back().expect("Should be color") {
</span><span style="color:#323232;">                        "red" => Cube::Red(parse_num(group)),
</span><span style="color:#323232;">                        "green" => Cube::Green(parse_num(group)),
</span><span style="color:#323232;">                        "blue" => Cube::Blue(parse_num(group)),
</span><span style="color:#323232;">                        c @ _ => panic!("Color {c} not recognized"),
</span><span style="color:#323232;">                    }
</span><span style="color:#323232;">                })
</span><span style="color:#323232;">                .collect::>()
</span><span style="color:#323232;">        })
</span><span style="color:#323232;">        .into_iter()
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">pub fn solution_1() {
</span><span style="color:#323232;">    let bag = Bag {
</span><span style="color:#323232;">        red: 12,
</span><span style="color:#323232;">        green: 13,
</span><span style="color:#323232;">        blue: 14,
</span><span style="color:#323232;">    };
</span><span style="color:#323232;">    let sum: usize = process_lines()
</span><span style="color:#323232;">        .enumerate()
</span><span style="color:#323232;">        .map(|(i, line)| {
</span><span style="color:#323232;">            let is_possible = line.iter().all(|cube| match cube {
</span><span style="color:#323232;">                Cube::Red(c) => *c &lt;= bag.red,
</span><span style="color:#323232;">                Cube::Green(c) => *c &lt;= bag.green,
</span><span style="color:#323232;">                Cube::Blue(c) => *c &lt;= bag.blue,
</span><span style="color:#323232;">            });
</span><span style="color:#323232;">
</span><span style="color:#323232;">            if is_possible {
</span><span style="color:#323232;">                i + 1
</span><span style="color:#323232;">            } else {
</span><span style="color:#323232;">                0
</span><span style="color:#323232;">            }
</span><span style="color:#323232;">        })
</span><span style="color:#323232;">        .sum();
</span><span style="color:#323232;">
</span><span style="color:#323232;">    println!("{sum}");
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">pub fn solution_2() {
</span><span style="color:#323232;">    let powersum: usize = process_lines()
</span><span style="color:#323232;">        .map(|line| {
</span><span style="color:#323232;">            let bag = line.iter().fold(
</span><span style="color:#323232;">                Bag {
</span><span style="color:#323232;">                    red: 0,
</span><span style="color:#323232;">                    blue: 0,
</span><span style="color:#323232;">                    green: 0,
</span><span style="color:#323232;">                },
</span><span style="color:#323232;">                |mut bag, cube| {
</span><span style="color:#323232;">                    match *cube {
</span><span style="color:#323232;">                        Cube::Red(n) => {
</span><span style="color:#323232;">                            if n > bag.red {
</span><span style="color:#323232;">                                bag.red = n;
</span><span style="color:#323232;">                            }
</span><span style="color:#323232;">                        }
</span><span style="color:#323232;">                        Cube::Green(n) => {
</span><span style="color:#323232;">                            if n > bag.green {
</span><span style="color:#323232;">                                bag.green = n;
</span><span style="color:#323232;">                            }
</span><span style="color:#323232;">                        }
</span><span style="color:#323232;">                        Cube::Blue(n) => {
</span><span style="color:#323232;">                            if n > bag.blue {
</span><span style="color:#323232;">                                bag.blue = n;
</span><span style="color:#323232;">                            }
</span><span style="color:#323232;">                        }
</span><span style="color:#323232;">                    }
</span><span style="color:#323232;">
</span><span style="color:#323232;">                    bag
</span><span style="color:#323232;">                },
</span><span style="color:#323232;">            );
</span><span style="color:#323232;">
</span><span style="color:#323232;">            bag.red * bag.blue * bag.green
</span><span style="color:#323232;">        })
</span><span style="color:#323232;">        .sum();
</span><span style="color:#323232;">
</span><span style="color:#323232;">    println!("{powersum}");
</span><span style="color:#323232;">}
</span>
CannotSleep420,

I finally got my solutions done. I used rust. I feel like 114 lines (not including empty lines or driver code) for both solutions is pretty decent. If lemmy’s code blocks are hard to read, I also put my solutions on github.


<span style="color:#323232;">use std::{
</span><span style="color:#323232;">    cell::OnceCell,
</span><span style="color:#323232;">    collections::{HashMap, VecDeque},
</span><span style="color:#323232;">    ops::ControlFlow::{Break, Continue},
</span><span style="color:#323232;">};
</span><span style="color:#323232;">
</span><span style="color:#323232;">use crate::utils::read_lines;
</span><span style="color:#323232;">
</span><span style="color:#323232;">#[derive(Clone, Copy, PartialEq, Eq)]
</span><span style="color:#323232;">enum NumType {
</span><span style="color:#323232;">    Digit,
</span><span style="color:#323232;">    DigitOrWord,
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">#[derive(Clone, Copy, PartialEq, Eq)]
</span><span style="color:#323232;">enum FromDirection {
</span><span style="color:#323232;">    Left,
</span><span style="color:#323232;">    Right,
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">const WORD_NUM_MAP: OnceCell> = OnceCell::new();
</span><span style="color:#323232;">
</span><span style="color:#323232;">fn init_num_map() -> HashMap&lt;&amp;'static str, u8> {
</span><span style="color:#323232;">    HashMap::from([
</span><span style="color:#323232;">        ("one", b'1'),
</span><span style="color:#323232;">        ("two", b'2'),
</span><span style="color:#323232;">        ("three", b'3'),
</span><span style="color:#323232;">        ("four", b'4'),
</span><span style="color:#323232;">        ("five", b'5'),
</span><span style="color:#323232;">        ("six", b'6'),
</span><span style="color:#323232;">        ("seven", b'7'),
</span><span style="color:#323232;">        ("eight", b'8'),
</span><span style="color:#323232;">        ("nine", b'9'),
</span><span style="color:#323232;">    ])
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">const MAX_WORD_LEN: usize = 5;
</span><span style="color:#323232;">
</span><span style="color:#323232;">fn get_digit<i>(mut bytes: I, num_type: NumType, from_direction: FromDirection) -> Option
</span><span style="color:#323232;">where
</span><span style="color:#323232;">    I: Iterator,
</span><span style="color:#323232;">{
</span><span style="color:#323232;">    let digit = bytes.try_fold(VecDeque::new(), |mut byte_queue, byte| {
</span><span style="color:#323232;">        if byte.is_ascii_digit() {
</span><span style="color:#323232;">            Break(byte)
</span><span style="color:#323232;">        } else if num_type == NumType::DigitOrWord {
</span><span style="color:#323232;">            if from_direction == FromDirection::Left {
</span><span style="color:#323232;">                byte_queue.push_back(byte);
</span><span style="color:#323232;">            } else {
</span><span style="color:#323232;">                byte_queue.push_front(byte);
</span><span style="color:#323232;">            }
</span><span style="color:#323232;">
</span><span style="color:#323232;">            let word = byte_queue
</span><span style="color:#323232;">                .iter()
</span><span style="color:#323232;">                .map(|&amp;byte| byte as char)
</span><span style="color:#323232;">                .collect::();
</span><span style="color:#323232;">
</span><span style="color:#323232;">            for &amp;key in WORD_NUM_MAP
</span><span style="color:#323232;">                .get_or_init(init_num_map)
</span><span style="color:#323232;">                .keys()
</span><span style="color:#323232;">                .filter(|k| k.len() &lt;= byte_queue.len())
</span><span style="color:#323232;">            {
</span><span style="color:#323232;">                if word.contains(key) {
</span><span style="color:#323232;">                    return Break(*WORD_NUM_MAP.get_or_init(init_num_map).get(key).unwrap());
</span><span style="color:#323232;">                }
</span><span style="color:#323232;">            }
</span><span style="color:#323232;">
</span><span style="color:#323232;">            if byte_queue.len() == MAX_WORD_LEN {
</span><span style="color:#323232;">                if from_direction == FromDirection::Left {
</span><span style="color:#323232;">                    byte_queue.pop_front();
</span><span style="color:#323232;">                } else {
</span><span style="color:#323232;">                    byte_queue.pop_back();
</span><span style="color:#323232;">                }
</span><span style="color:#323232;">            }
</span><span style="color:#323232;">
</span><span style="color:#323232;">            Continue(byte_queue)
</span><span style="color:#323232;">        } else {
</span><span style="color:#323232;">            Continue(byte_queue)
</span><span style="color:#323232;">        }
</span><span style="color:#323232;">    });
</span><span style="color:#323232;">
</span><span style="color:#323232;">    if let Break(byte) = digit {
</span><span style="color:#323232;">        Some(byte)
</span><span style="color:#323232;">    } else {
</span><span style="color:#323232;">        None
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">fn process_digits(x: u8, y: u8) -> u16 {
</span><span style="color:#323232;">    ((10 * (x - b'0')) + (y - b'0')).into()
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">fn solution(num_type: NumType) {
</span><span style="color:#323232;">    if let Ok(lines) = read_lines("src/day_1/input.txt") {
</span><span style="color:#323232;">        let sum = lines.fold(0_u16, |acc, line| {
</span><span style="color:#323232;">            let line = line.unwrap_or_else(|_| String::new());
</span><span style="color:#323232;">            let bytes = line.bytes();
</span><span style="color:#323232;">            let left = get_digit(bytes.clone(), num_type, FromDirection::Left).unwrap_or(b'0');
</span><span style="color:#323232;">            let right = get_digit(bytes.rev(), num_type, FromDirection::Right).unwrap_or(left);
</span><span style="color:#323232;">
</span><span style="color:#323232;">            acc + process_digits(left, right)
</span><span style="color:#323232;">        });
</span><span style="color:#323232;">
</span><span style="color:#323232;">        println!("{sum}");
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">pub fn solution_1() {
</span><span style="color:#323232;">    solution(NumType::Digit);
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">pub fn solution_2() {
</span><span style="color:#323232;">    solution(NumType::DigitOrWord);
</span><span style="color:#323232;">}
</span><span style="color:#323232;">```</i>
</span>
CannotSleep420,

One small thing that would make it easier to read would be to use (I don’t know what the syntax is called) as opposed to the magic numbers for getting the ascii code for a character as a u8, e.g. b’0’ instead of 48.

CannotSleep420,

I’m using rust. I’m hoping I’ll be able to finish all the days unlike last year.

CannotSleep420,

Some that come to mind:

  • Scaled sort, which takes community size into consideration when looking for top posts
  • User will be able to block instances without the need for their home instance to defederate
  • The new flow for enabling 2FA makes it harder for users to accidentally lock themselves out of their accounts
  • There will be a feed option to just see posts in communities you moderate (as opposed to subscribed, local, and all)
  • Controversial sort
  • You can import and export account settings as JSON, making switching instances easier
CannotSleep420,

I can see I’m not the only one who made an AI “art” bot @ArtBot

CannotSleep420,

This is surprisingly common with /pol/acks.

CannotSleep420,

Those photos are so 2000s. Adds to the charm.

CannotSleep420,

How can anyone hate the ejaculation station?

CannotSleep420,

At this point Cannibal McGoatfuck is a better choice than every democrat politician.

CannotSleep420,

Piles and piles of children with photographic evidence and multiple parties saying it happened: blood libel

40 babies being beheaded according to hearsay from 1 IOF soldier with no one able to verify the existence of the alleged video: totally real and not blood libel; we must exterminate these savages!

CannotSleep420,

@TranslatorBot English

FlorisBoard | FOSS keyboard that respects your privacy (florisboard.org)

I came across everyday topic on Techlore Discussions about free and open source keyboards for Android and discovered this little gem. It is FlorisBoard, a virtual keyboard for Android which respects privacy of the user. I can sigh with relief and finish my search for that singular keyboard for typing stuff on the go....

CannotSleep420,

I’ve found AnySoft keyboard to have decent enough swipe support as well as word predictions. The only complaints I have about it is that the keyboard sometimes shrinks to a weird size if you use autorotate and its word predictions don’t support contractions.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • anitta
  • thenastyranch
  • magazineikmin
  • tacticalgear
  • InstantRegret
  • ngwrru68w68
  • Durango
  • Youngstown
  • slotface
  • mdbf
  • rosin
  • PowerRangers
  • kavyap
  • DreamBathrooms
  • normalnudes
  • vwfavf
  • hgfsjryuu7
  • cisconetworking
  • osvaldo12
  • everett
  • ethstaker
  • GTA5RPClips
  • khanakhh
  • tester
  • modclub
  • cubers
  • Leos
  • provamag3
  • All magazines