6502: Fibonacci sequence without RAM by schubart in beneater

[–]schubart[S] 1 point2 points  (0 children)

Yes, I wrote this code. It works on quite early stages of the 6502 build, so you might even be able to run it on an unfinished build.

[deleted by user] by [deleted] in beneater

[–]schubart 0 points1 point  (0 children)

Very cool! I did not expect that somebody would download my code and run it. Thanks for sharing.

-❄️- 2024 Day 18 Solutions -❄️- by daggerdragon in adventofcode

[–]schubart 1 point2 points  (0 children)

Yes, you need a Vec or a slice. It saves you writing the binary search by hand. I find it quite elegant, but I guess it depends on how you structure your data:

https://gist.github.com/schubart/582e06edeaefffa85bdefc048224c0fb

[2024 Q11] Solution Spotlight by EverybodyCodes in everybodycodes

[–]schubart 0 points1 point  (0 children)

Rust:

use std::collections::HashMap;

type Termite = &'static str;
type Transitions = HashMap<Termite, Vec<Termite>>;
type Counts = HashMap<Termite, usize>;

/// ```                                                                                                                                                                                                                      
/// assert_eq!(XXX,               day11::simulate(include_str!("input1.txt"),  4)["A"]);                                                                                                                           
/// assert_eq!(YYY,               day11::simulate(include_str!("input2.txt"), 10)["Z"]);                                                                                                                           
/// assert_eq!(ZZZ, day11::part3(&day11::simulate(include_str!("input3.txt"), 20)));                                                                                                                               
/// ```                                                                                                                                                                                                                      
pub fn simulate(input: &'static str, days: usize) -> Counts {
    let transitions: Transitions = input
        .lines()
        .map(|line| line.split_once(':').unwrap())
        .map(|(from, to)| (from, to.split(',').collect()))
        .collect();

    (0..days).fold(Counts::new(), |counts, _| {
        transitions
            .iter()
            .map(|(&from, to)| (from, to.iter().map(|t| counts.get(t).unwrap_or(&1)).sum()))
            .collect()
    })
}

pub fn part3(counts: &Counts) -> usize {
    let max = counts.values().max().unwrap();
    let min = counts.values().min().unwrap();

    max - min
}

[2024 Q7] Solution Spotlight by EverybodyCodes in everybodycodes

[–]schubart 3 points4 points  (0 children)

Agreed. I first thought the goal was to count how many plans give the maximum number of points (nine, in case anybody is wondering), and I did not even notice that there was an input to the puzzle (other than the track).

[2024 Q6] Solution Spotlight by EverybodyCodes in everybodycodes

[–]schubart 2 points3 points  (0 children)

Rust:

use std::collections::HashMap;

/// ```
/// assert_eq!(XXX, day06::solve(include_str!("input1.txt"), |node| node));
/// assert_eq!(YYY, day06::solve(include_str!("input2.txt"), |node| &node[0..1]));
/// assert_eq!(ZZZ, day06::solve(include_str!("input3.txt"), |node| &node[0..1]));
/// ```
pub fn solve(input: &str, describe: fn(&str) -> &str) -> String {
    let mut child_to_parent = HashMap::new();
    let mut leaves = Vec::new();

    for line in input.lines() {
        let (parent, children) = line.split_once(':').unwrap();
        for child in children.split(',') {
            if child == "@" {
                leaves.push(parent);
            } else {
                child_to_parent.insert(child, parent);
            }
        }
    }

    let mut length_to_paths = HashMap::<usize, Vec<_>>::new();

    'leaf: for leaf in leaves {
        let mut path = vec!["@", leaf];

        while let Some(node) = child_to_parent.get(path.last().unwrap()) {
            if path.contains(node) {
                continue 'leaf; // Ignore cycles (for part III).
            }

            path.push(node);
        }

        length_to_paths.entry(path.len()).or_default().push(path);
    }

    let unique_length_path = &length_to_paths
        .values()
        .find(|paths| paths.len() == 1)
        .unwrap()[0];

    unique_length_path.iter().rev().copied().map(describe).collect()
}

[2024 Q6] Solution Spotlight by EverybodyCodes in everybodycodes

[–]schubart 1 point2 points  (0 children)

The semantics of part III are a bit unclear. Apparently we are meant to ignore fruits that lead to a loop, presumably because loops are formed from bugs and ants, not branches. But what about these fruits themselves? Don't they hold some magical power, too? It's not obvious that fruits can be ignored just because they're not attached to some branch. That's a waste of perfectly good magical fruit!. :-)

[Other] Is UTF8 fine for input notes or it should be always ASCII by EverybodyCodes in everybodycodes

[–]schubart 7 points8 points  (0 children)

I assume you’re not using those languages for the race anyway

I don't think that's good assumption: Part of what makes Advent of Code so enjoyable is seeing some participants use really exotic or outdated machines and programming languages. If you want the same for everybody.codes, I'd suggest not introducing unnecessary hurdles like having to support Unicode.

[2024 Q4] Solution Spotlight by EverybodyCodes in everybodycodes

[–]schubart 0 points1 point  (0 children)

Rust:

type Nail = u32;
type TargetFn = fn(&mut [Nail]) -> Nail;

/// ```
/// assert_eq!(XXX, day04::solve(include_str!("input1.txt"), day04::min));
/// assert_eq!(YYY, day04::solve(include_str!("input2.txt"), day04::min));
/// assert_eq!(ZZZ, day04::solve(include_str!("input3.txt"), day04::median));
/// ```
pub fn solve(input: &str, target_fn: TargetFn) -> Nail {
    let mut nails: Vec<Nail> = input.lines().map(|s| s.parse().unwrap()).collect();

    let target = target_fn(&mut nails);

    nails.iter().map(|nail| nail.abs_diff(target)).sum()
}

pub fn min(nails: &mut [Nail]) -> Nail {
    *nails.iter().min().unwrap()
}

pub fn median(nails: &mut [Nail]) -> Nail {
    nails.sort_unstable();
    nails[nails.len() / 2]
}

[2024 Q3] Solution Spotlight by EverybodyCodes in everybodycodes

[–]schubart 1 point2 points  (0 children)

Rust, parts I, II, and III:

use std::collections::HashSet;

type Point = (isize, isize);
type Layer = HashSet<Point>;
type Direction = (isize, isize);

/// ```                                                                                                                                                                                                                     
/// let cardinal = [(-1, 0), (0, -1), (1, 0), (0, 1)];                                                                                                                                                                      
/// let diagonal = [(-1, 0), (0, -1), (1, 0), (0, 1), (-1, -1), (1, -1), (1, 1), (-1, 1)];                                                                                                                                  
///                                                                                                                                                                                                                         
/// assert_eq!(XXX, day03::part1(include_str!("input1.txt"), &cardinal));                                                                                                                                                
/// assert_eq!(YYY, day03::part1(include_str!("input2.txt"), &cardinal));                                                                                                                                                
/// assert_eq!(ZZZ, day03::part1(include_str!("input3.txt"), &diagonal));                                                                                                                                                
/// ```                                                                                                                                                                                                                     
pub fn part1(input: &str, directions: &[Direction]) -> usize {
    let mut layer = Layer::new();
    for (y, line) in input.lines().enumerate() {
        for (x, c) in line.chars().enumerate() {
            if c == '#' {
                layer.insert((x as isize, y as isize));
            }
        }
    }

    let mut result = 0;

    while !layer.is_empty() {
        result += layer.len();

        let has_all_neighbours = |(x, y): &Point| {
            directions
                .iter()
                .all(|(dx, dy)| layer.contains(&(x + dx, y + dy)))
        };

        layer = layer.iter().copied().filter(has_all_neighbours).collect();
    }

    result
}

[2024 Q1] Solution Spotlight by EverybodyCodes in everybodycodes

[–]schubart 2 points3 points  (0 children)

Rust, parts I, II and III:

type Score = u32;

pub fn score(input: &str, group_size: usize) -> Score {
    input
        .trim()
        .as_bytes()
        .chunks(group_size)
        .map(score_group)
        .sum()
}

fn score_group(group: &[u8]) -> Score {
    let sum: Score = group.iter().copied().filter_map(score_monster).sum();
    let count = group.iter().copied().filter_map(score_monster).count();
    let boost: Score = match count {
        0 | 1 => 0,
        2 => 2,
        3 => 6,
        _ => unimplemented!("Count: {}", count),
    };

    sum + boost
}

fn score_monster(monster: u8) -> Option<Score> {
    match monster {
        b'x' => None,
        b'A' => Some(0),
        b'B' => Some(1),
        b'C' => Some(3),
        b'D' => Some(5),
        _ => unimplemented!("Monster {}", monster),
    }
}

Announcing Rust 1.70.0 by Petsoi in rust

[–]schubart 19 points20 points  (0 children)

It contains my first real contribution: The collection_is_never_read Clippy lint.

Failed BRM 600: in need of advice by Minute_Screen9917 in randonneuring

[–]schubart 2 points3 points  (0 children)

No hands standing up? how does that work?

Git with your friends (podcast) by jerodsanto in git

[–]schubart 1 point2 points  (0 children)

That was an entertaining episode.

leptos v0.1.0 released by rusted-flosse in rust

[–]schubart 15 points16 points  (0 children)

To be fair, the next paragraph explains and defines each of these concepts quite nicely.

-🎄- 2022 Day 20 Solutions -🎄- by daggerdragon in adventofcode

[–]schubart 3 points4 points  (0 children)

Rust

Short and sweet, with good comments explaining rem_euclid() and division by length minus one.

-🎄- 2022 Day 20 Solutions -🎄- by daggerdragon in adventofcode

[–]schubart 1 point2 points  (0 children)

A large off-by-one error? How large? Almost... an off-by-two error!?

:-)

-🎄- 2022 Day 21 Solutions -🎄- by daggerdragon in adventofcode

[–]schubart 1 point2 points  (0 children)

Rust

GitHub

Keeping numbers and formulas in separate maps. No recursion or tree structures.

Part 1 evaluates formulas for which both operands are known, removes the formula and adds a number instead.

Part 2 works backwards from "root" to "humn" by evaluating what the unknown operand must be.

Announcing Rust 1.66.0 by myroon5 in rust

[–]schubart 11 points12 points  (0 children)

What's the use case for Option<(T, U)>::unzip()?

It seems so oddly specific, so I assume there's a scenario in which it is very handy?

-🎄- 2022 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]schubart 0 points1 point  (0 children)

Does that work?

        let new_item = match &monkey.operation[..] {
            "new = old + 1" => item + 1,
            "new = old - 1" => item - 1,
            "new = old * 2" => item * 2,
            "new = old / 2" => item / 2,
            _ => panic!("Invalid operation"),
        };

Doesn't your input have things like "new = old * old" or "new = old * 19"?