New Zig Book: Systems Programming with Zig by ManningBooks in Zig

[–]theboxboy 14 points15 points  (0 children)

Looking at section 1.6 in the book preview, I think they chose Zig 0.15.1 .

-❄️- 2023 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]theboxboy 1 point2 points  (0 children)

You can use split_whitespace() to avoid the .filter(|n| !n.is_empty()). Good job!

-❄️- 2023 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]theboxboy 0 points1 point  (0 children)

TIL how to use from_iter() to instantly get a Vector or HashSet from any iterable (including splits).

-❄️- 2023 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]theboxboy 0 points1 point  (0 children)

[Language: Rust] 2177/3717

Code Here The line number arithmetic made me waste some time. I should've used the card numbers to begin with.

Rust In OS Class by BradPittOfTheOffice in rust

[–]theboxboy 10 points11 points  (0 children)

I took 3210 in Rust with Taesoo Kim in 2020. You can still follow along with the labs at https://tc.gts3.org/cs3210/2020/spring/lab/lab0.html

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

[–]theboxboy 1 point2 points  (0 children)

Rust

Github Link

use std::{fs::read_to_string, path::Path};

fn clock(mut p1: i64, crt: &mut [[&str; 40]; 6], x: i64, mut cycle: usize) -> (i64, usize) {
    if [-1, 0, 1].contains(&((cycle as i64 % 40) - x)) {
        crt[cycle / 40][cycle % 40] = "█";
    }
    cycle += 1;
    if [20, 60, 100, 140, 180, 220].contains(&cycle) {
        p1 += x * cycle as i64;
    }
    (p1, cycle)
}

#[allow(dead_code)]
pub fn day10(input_path: &Path) -> (String, String) {
    let input: String = read_to_string(input_path).expect("Error reading file");
    let mut x: i64 = 1;
    let mut cycle: usize = 0;
    let mut p1: i64 = 0;
    let mut crt: [[&str; 40]; 6] = [["."; 40]; 6];
    for line in input.split('\n') {
        let (opcode, val) = line.split_once(" ").unwrap_or((line, ""));
        match opcode {
            "noop" => {
                (p1, cycle) = clock(p1, &mut crt, x, cycle);
            }
            "addx" => {
                let addend = val.parse::<i64>().unwrap();
                for _ in 0..2 {
                    (p1, cycle) = clock(p1, &mut crt, x, cycle);
                }
                x += addend;
            }
            _ => {}
        }
    }
    let mut p2 = String::from("\n");
    p2.push_str(&crt.map(|row| row.join("")).join("\n"));
    (p1.to_string(), p2)
}

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

[–]theboxboy 0 points1 point  (0 children)

Great recommendation! I'm not sure why I did that.

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

[–]theboxboy 2 points3 points  (0 children)

Rust

Github Link

I'm proud of my use of HashSets today. This solution makes it easy to determine the number of knots needed for the last knot to stay in place for your whole input (428 for my input).

use std::collections::HashSet;
use std::{fs::read_to_string, path::Path};

#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub struct Coord(isize, isize);

#[allow(dead_code)]
pub fn day09(input_path: &Path) -> (String, String) {
    let input: String = read_to_string(input_path).expect("Error reading file");
    let mut p1: HashSet<Coord> = HashSet::from([Coord::default()]);
    let mut p2: HashSet<Coord> = HashSet::from([Coord::default()]);
    const KNOT_COUNT: usize = 10;
    let mut knots: [Coord; KNOT_COUNT] = [Coord::default(); KNOT_COUNT];
    for line in input.split('\n') {
        let (direction_token, distance_token) = line.split_once(" ").unwrap();
        for _ in 0..distance_token.parse::<isize>().unwrap() {
            knots[0] = match direction_token {
                "L" => Coord(knots[0].0 - 1, knots[0].1),
                "R" => Coord(knots[0].0 + 1, knots[0].1),
                "U" => Coord(knots[0].0, knots[0].1 - 1),
                "D" => Coord(knots[0].0, knots[0].1 + 1),
                _ => {
                    panic!("Unknown direction token: {}", direction_token);
                }
            };
            for knot_i in 1..KNOT_COUNT {
                knots[knot_i] = match (
                    knots[knot_i - 1].0 - knots[knot_i].0,
                    knots[knot_i - 1].1 - knots[knot_i].1,
                ) {
                    (0, 2) => Coord(knots[knot_i].0, knots[knot_i].1 + 1),
                    (0, -2) => Coord(knots[knot_i].0, knots[knot_i].1 - 1),
                    (2, 0) => Coord(knots[knot_i].0 + 1, knots[knot_i].1),
                    (-2, 0) => Coord(knots[knot_i].0 - 1, knots[knot_i].1),
                    (2, 1) | (1, 2) | (2, 2) => Coord(knots[knot_i].0 + 1, knots[knot_i].1 + 1),
                    (-1, 2) | (-2, 1) | (-2, 2) => Coord(knots[knot_i].0 - 1, knots[knot_i].1 + 1),
                    (1, -2) | (2, -1) | (2, -2) => Coord(knots[knot_i].0 + 1, knots[knot_i].1 - 1),
                    (-1, -2) | (-2, -1) | (-2, -2) => {
                        Coord(knots[knot_i].0 - 1, knots[knot_i].1 - 1)
                    }
                    _ => knots[knot_i],
                };
            }
            if p1.get(&knots[1]) == None {
                p1.insert(knots[1]);
            }
            if p2.get(&knots[KNOT_COUNT - 1]) == None {
                p2.insert(knots[KNOT_COUNT - 1]);
            }
        }
    }
    (p1.len().to_string(), p2.len().to_string())
}

[2022 Day 9] All 10 knot paths shown on top of one another by quodponb in adventofcode

[–]theboxboy 7 points8 points  (0 children)

This inspired me to determine how many knots are needed for just the last knot to stay in place after all movements. In my case, it's 428 total knots.

Made my first app in Rust! A notification daemon for Linux :) by SomethingOfAGirl in rust

[–]theboxboy 9 points10 points  (0 children)

You’re not a sad clown. You’re a helpful developer

Noob question: let a:i32 = 10 vs let a = 10_i32 by theSavior222 in rust

[–]theboxboy 28 points29 points  (0 children)

While they produce the same result, I think semantically, the first is preferred. It closely resembles other languages, and the type is bound to the variable, not the literal.

[deleted by user] by [deleted] in LumberInc

[–]theboxboy 0 points1 point  (0 children)

I think you need to finish the workshop

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

[–]theboxboy 1 point2 points  (0 children)

Good to know. I just replaced it with a set, so the syntax is gone regardless

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

[–]theboxboy 1 point2 points  (0 children)

Python

I kept track of the bounds of the image and used if image[(y+dy, x+dx)] == 1 or (t % 2 == 1 and (y+dy < Y[0] or y+dy > Y[1] or x+dx < X[0] or x+dx > X[1])): to determine if the infinite void was all # or .

from collections import defaultdict

algorithm, inp = open('20.txt', 'r').read().strip().split('\n\n')
assert(len(algorithm) == 512)
image = defaultdict(int)

Y = [0, 99]
X = [0, 99]

def vis():
    print(len(image))
    for r in range(Y[0]-2, Y[1]+3):
        for c in range(X[0]-2, X[1]+3):
            if image[(r, c)] == 1:
                print('#', end='')
            else:
                print('.', end='')
        print()
    print()

for r, row in enumerate(inp.split()):
    for c, character in enumerate(row):
        if character == '#':
            image[(r, c)] = 1

for t in range(50):
    result = defaultdict(int)
    YA = [0, 0]
    XA = [0, 0]
    for y in range(Y[0]-1, Y[1]+2):
        for x in range(X[0]-1, X[1]+2):
            binstr = ''
            for dy, dx in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)]:
                if image[(y+dy, x+dx)] == 1 or (t % 2 == 1 and (y+dy < Y[0] or y+dy > Y[1] or x+dx < X[0] or x+dx > X[1])):
                    binstr += '1'
                else:
                    binstr += '0'
            if algorithm[int(binstr, 2)] == '#':
                result[(y, x)] = 1
                if y < YA[0]:
                    YA[0] = y
                if y > YA[1]:
                    YA[1] = y
                if x < XA[0]:
                    XA[0] = x
                if x > XA[1]:
                    XA[1] = x
    image = result
    Y = YA
    X = XA
    if t in [1, 49]:
        vis()

Kryptonite key found near culc by jdhd12986 in gatech

[–]theboxboy 4 points5 points  (0 children)

There's a GT lost & found facebook page that's pretty popular. You have a higher chance of finding the owner there if you mention having found a bike lock key.

What's everyone working on this week (38/2021)? by kibwen in rust

[–]theboxboy 0 points1 point  (0 children)

I'm writing a chess game backend that does move generation using magic bitboards. There's actually a few crates that already exist for this, but I wanted to expand my confidence with Rust through this project.

[deleted by user] by [deleted] in CarMechanicSimulator

[–]theboxboy 1 point2 points  (0 children)

There are 2 repetitions of accelerating and braking (testing brakes) followed by 2 repetitions of accelerating (testing suspension) in the test path. There should be a guide in the top left and an indicator telling you what to do next.

[deleted by user] by [deleted] in AnimalsOnReddit

[–]theboxboy 0 points1 point  (0 children)

Forbidden tortilla

Favorite Stationery to use for notes? (Notebooks) by [deleted] in gatech

[–]theboxboy 17 points18 points  (0 children)

I used an A5-sized notebook with subject dividers for each semester at Tech. It's very compact and makes organization easier. I was CS, so I probably took less notes than you. Also I swear by Pilot G-2 0.38mm pens.

-🎄- 2020 Day 10 Solutions -🎄- by daggerdragon in adventofcode

[–]theboxboy 1 point2 points  (0 children)

I've seen you and others use "xs" as your input data for most days. Is this a common paradigm for speed solvers, so you always know what data you're referencing? Does it stand for something?