creamsnail's recent activity

  1. Comment on Day 1: Secret Entrance in ~comp.advent_of_code

    creamsnail
    Link Parent
    That's a lot, I appreciate being able to look at what others have done to solve the problems. Primarily because as the event goes on I'll likely be checking out other solutions more and more to...

    That's a lot, I appreciate being able to look at what others have done to solve the problems. Primarily because as the event goes on I'll likely be cheating checking out other solutions more and more to hopefully learn more!

    1 vote
  2. Comment on Day 1: Secret Entrance in ~comp.advent_of_code

    creamsnail
    Link Parent
    Honestly that looks way better than how I approached it, really well done! I'm also surprised mine runs only 20ms longer than yours. Especially since I've got some very egregious for loops, and...

    Honestly that looks way better than how I approached it, really well done! I'm also surprised mine runs only 20ms longer than yours. Especially since I've got some very egregious for loops, and rotates in there.

    2 votes
  3. Comment on Day 3: Lobby in ~comp.advent_of_code

    creamsnail
    Link
    I'll be honest I just couldn't wrap my head around part two for some reason. I ended up doing the shameful thing and took a look at what @Berdes had. It was really well done, and very concise for...

    I'll be honest I just couldn't wrap my head around part two for some reason. I ended up doing the shameful thing and took a look at what @Berdes had. It was really well done, and very concise for both parts. I ended up adapting some of your code into mine for the second part and left the first one in my mangled spaghetti. I'm also amazed at how concise yours and @zkxs 's are. One day I'll get there, but kudo's to you both!

    Rust Parts 1 and 2

    I did have a working solution with the test numbers provided but when we went from 15 digits to 100, well... exponential growth hurts and brute forcing it was not cutting it.

    use std::fs::read_to_string;
    
    const TEST_FILE: &str = "example_input.txt";
    const PUZZLE_INPUT: &str = "puzzle_input.txt";
    
    // Reads in the puzzle input text file which contains the D##
    fn read_puzzle_input(file_name: &str) -> Vec<String> {
        return read_to_string(file_name)
            .unwrap()
            .lines()
            .map(String::from)
            .collect();
    }
    
    fn main() {
        // Part 1
        let part_one_answer = part_one(read_puzzle_input(PUZZLE_INPUT));
        println!("The total output voltage is: {}", part_one_answer);
        // Part 2
        let part_two_answer = part_two(read_puzzle_input(PUZZLE_INPUT));
        println!("The total combined output voltage is: {}", part_two_answer);
    }
    
    fn part_one(battery_banks: Vec<String>) -> u32 {
        let mut total_output_joltage = 0;
    
        for bank in battery_banks.iter() {
            // Create our array of chars
            let bank_array: Vec<char> = bank.chars().collect();
            // Setup some vars as we iterate
            let mut skip_item: usize = 0;
            let mut first_item = 0;
            let mut last_item = 0;
    
            // Iterate to find our first number
            for (position, battery) in bank_array.iter().enumerate() {
                let num = battery.to_digit(10).unwrap();
                // This will grab the first item that is the highest number that isn't the last item in the vector
                if num > first_item && position < bank_array.len() - 1 {
                    first_item = num;
                    skip_item = position
                }
            }
            // Iterate to find our second number; ignore the items LESS than the position in the array.
            for (position, battery) in bank_array.iter().enumerate() {
                let num = battery.to_digit(10).unwrap();
                if num > last_item && position > skip_item {
                    last_item = num
                }
            }
            // Combine our first and last numbers
            let producing_jolts = format!("{}{}", first_item, last_item)
                .parse::<u32>()
                .unwrap();
            total_output_joltage += producing_jolts
        }
        return total_output_joltage;
    }
    
    fn part_two(battery_banks: Vec<String>) -> u64 {
        let mut total_output_joltage: u64 = 0;
    
        for bank in battery_banks.iter() {
            let mut max_values = [0; 13];
            let bank_array: Vec<u32> = bank.chars().map(|c| c.to_digit(10).unwrap()).collect();
            for number in bank_array {
                for i in (1..13).rev() {
                    max_values[i] = max_values[i].max(max_values[i - 1] * 10 + number as u64)
                }
            }
            total_output_joltage += max_values[12];
        }
        return total_output_joltage;
    }
    
    #[test]
    fn part_one_example() {
        let battery_banks = read_puzzle_input(TEST_FILE);
        let total_output_joltage = part_one(battery_banks);
        assert_eq!(total_output_joltage, 357);
    }
    
    #[test]
    fn part_two_example() {
        let battery_banks = read_puzzle_input(TEST_FILE);
        let total_output_joltage = part_two(battery_banks);
        assert_eq!(total_output_joltage, 3121910778619);
    }
    
    2 votes
  4. Comment on Day 2: Gift Shop in ~comp.advent_of_code

    creamsnail
    Link
    Part two really was painful for some reason. I couldn't wrap my mind around how to find all the duplicates when it could just be two, or three or more. So this is what I ended up with. I feel like...

    Part two really was painful for some reason. I couldn't wrap my mind around how to find all the duplicates when it could just be two, or three or more. So this is what I ended up with. I feel like I was going down a brute force path there for a bit before coming to this solution.

    Rust - Parts 1 and 2
    use std::fs::read_to_string;
    
    const TEST_FILE: &str = "example_input.txt";
    const PUZZLE_INPUT: &str = "puzzle_input.txt";
    
    fn main() {
        let part_one_answer = part_one(read_puzzle_input(PUZZLE_INPUT));
        println!("The answer for part one is: {}", part_one_answer);
    
        let part_two_answer = part_two(read_puzzle_input(PUZZLE_INPUT));
        println!("The answer for part two is: {}", part_two_answer);
    }
    
    // Reads in the puzzle input text file which contains the D##
    fn read_puzzle_input(file_name: &str) -> Vec<String> {
        return read_to_string(file_name)
            .unwrap()
            .lines()
            .map(String::from)
            .collect();
    }
    
    fn part_one(id_ranges: Vec<String>) -> i64 {
        // For us to add all the invalid numbers
        let mut invalid_id_total: i64 = 0;
        // Iterate through the ranges provided
        for line in id_ranges {
            // Split the string into two because for some reason this is oddly complicated to accomplish in rust?
            if let Some((start_range, end_range)) = line.split_once('-') {
                // Set our strings to real numbers to build our for loop range
                let start: i64 = start_range.parse::<i64>().unwrap();
                let end: i64 = end_range.parse::<i64>().unwrap();
                // Iterate over our range
                for number in start..end + 1 {
                    // Re-convert the number to a string
                    let str_number = number.to_string();
                    // Split the string into two bits and compare
                    let (first_half, second_half) = str_number.split_at(str_number.len() / 2);
                    if first_half == second_half {
                        //println!("Found invalid ID: {}", str_number);
                        invalid_id_total += number
                    }
                }
            } else {
                // Would normally handle an error here due to the Some() above in case the `-` didn't exist.
                continue;
            }
        }
        return invalid_id_total;
    }
    
    fn part_two(id_ranges: Vec<String>) -> i64 {
        // For us to add all the invalid numbers
        let mut invalid_id_total: i64 = 0;
        // Iterate through the ranges provided
        for line in id_ranges {
            // Split the string into two because for some reason this is oddly complicated to accomplish in rust?
            if let Some((start_range, end_range)) = line.split_once('-') {
                // Set our strings to real numbers to build our for loop range
                let start: i64 = start_range.parse::<i64>().unwrap();
                let end: i64 = end_range.parse::<i64>().unwrap();
                // Iterate over our range
                for number in start..end + 1 {
                    // Re-convert the number to a string
                    let str_number = number.to_string();
                    // Split the string into two bits and compare
                    let (first_half, second_half) = str_number.split_at(str_number.len() / 2);
                    let mut first_split: Vec<&str> = str_number.split(&str_number[..1]).collect();
                    first_split.dedup();
                    if first_half == second_half {
                        //println!("Found invalid ID: {}", str_number);
                        invalid_id_total += number;
                        println!("Invalid Number: {:?}", number);
                    } else if number > 9 && first_split.len() == 1 {
                        invalid_id_total += number;
                        println!("Invalid Number: {:?}", number);
                    } else {
                        // Iterate through the first few to see if there are repeated numbers like 2,3,4 characters in repeated
                        for i in 1..str_number.len() / 2 {
                            let mut test_vec: Vec<&str> = str_number.split(&str_number[..i]).collect();
                            test_vec.dedup();
                            if test_vec.len() == 1 {
                                invalid_id_total += number;
                                println!("Invalid Number: {:?}", number);
                            }
                        }
                    }
                }
            } else {
                // Would normally handle an error here due to the Some() above in case the `-` didn't exist.
                continue;
            }
        }
        return invalid_id_total;
    }
    
    #[test]
    fn part_one_example() {
        let instructions = read_puzzle_input(TEST_FILE);
        let added_invalid_ids = part_one(instructions);
        assert_eq!(added_invalid_ids, 1227775554);
    }
    
    #[test]
    fn part_two_example() {
        let instructions = read_puzzle_input(TEST_FILE);
        let added_invalid_ids = part_two(instructions);
        assert_eq!(added_invalid_ids, 4174379265);
    }
    
    1 vote
  5. Comment on Day 1: Secret Entrance in ~comp.advent_of_code

    creamsnail
    Link
    I probably did not follow the prescribed way to accomplish these. But I did them, and that's all that really matters. I hope to see other versions for those who are doing it in Rust. I think I saw...

    I probably did not follow the prescribed way to accomplish these. But I did them, and that's all that really matters.

    I hope to see other versions for those who are doing it in Rust. I think I saw two other folks on here mention trying it this year, hopefully it'll inspire them to post their solutions when or if they can (no pressure).

    Parts 1 and 2 (with tests) Also I thought it was kind of cute that the second part's hex `0x434C49434B` decodes to `CLICK`
    use std::fs::read_to_string;
    
    const test_file: &str = "example_input.txt";
    const puzzle_input: &str = "puzzle_input.txt";
    
    fn main() {
        // PART ONE
        let part_one_start = 50;
        let part_one_answer = part_one(read_puzzle_input(puzzle_input), part_one_start);
        println!("The answer to part one is: {}", part_one_answer);
    
        let part_two_start: usize = 50;
        let part_two_answer = part_two(read_puzzle_input(puzzle_input), part_two_start);
        println!("The answer to part two is: {}", part_two_answer)
    }
    
    // Reads in the puzzle input text file which contains the D##
    fn read_puzzle_input(file_name: &str) -> Vec<String> {
        return read_to_string(file_name)
            .unwrap()
            .lines()
            .map(String::from)
            .collect();
    }
    
    fn part_one(instruction: Vec<String>, start_point: usize) -> i32 {
        let mut dial = vec![
            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
            "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
            "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
            "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76",
            "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91",
            "92", "93", "94", "95", "96", "97", "98", "99",
        ];
        dial.rotate_left(start_point);
    
        // Keep track of how many times we hit 0
        let mut count = 0;
    
        // Iterate over the instructions
        for line in instruction {
            let (direction, number) = line.split_at(1);
            // Set the number to a digit so we can rotate the dial
            let num: usize = number.parse::<usize>().unwrap();
            if direction == "R" {
                for _ in 0..num {
                    dial.rotate_left(1)
                }
            } else if direction == "L" {
                for _ in 0..num {
                    dial.rotate_right(1)
                }
            }
            if dial[0] == "0" {
                count += 1;
            }
        }
        return count;
    }
    
    fn part_two(instruction: Vec<String>, start_point: usize) -> i32 {
        let mut dial = vec![
            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
            "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
            "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
            "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76",
            "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91",
            "92", "93", "94", "95", "96", "97", "98", "99",
        ];
        dial.rotate_left(start_point);
    
        // Keep track of how many times we hit 0
        let mut count = 0;
    
        // Iterate over the instructions
        for line in instruction {
            let (direction, number) = line.split_at(1);
            // Set the number to a digit so we can rotate the dial
            let num: usize = number.parse::<usize>().unwrap();
            if direction == "R" {
                for _ in 0..num {
                    dial.rotate_left(1);
                    if dial[0] == "0" {
                        count += 1;
                    }
                }
            } else if direction == "L" {
                for _ in 0..num {
                    dial.rotate_right(1);
                    if dial[0] == "0" {
                        count += 1;
                    }
                }
            }
        }
        return count;
    }
    
    #[test]
    fn part_one_example() {
        let instruction = read_puzzle_input(test_file);
        let start_point: usize = 50;
        assert_eq!(part_one(instruction, start_point), 3);
    }
    
    #[test]
    fn part_two_example() {
        let instruction = read_puzzle_input(test_file);
        let start_point: usize = 50;
        assert_eq!(part_two(instruction, start_point), 6);
    }
    
    2 votes
  6. Comment on Faithless: A Sojourn Story in ~books

    creamsnail
    Link Parent
    Thank you for the guest pass, I did end up getting it for a year but sadly didn't end up using it nearly as much as I thought I would. I think I'll stick to probably supporting through patreon and...

    Thank you for the guest pass, I did end up getting it for a year but sadly didn't end up using it nearly as much as I thought I would. I think I'll stick to probably supporting through patreon and such as I think it's a bit more direct in some capacity and I can focus on the folks I actively watch.

    1 vote
  7. Comment on Faithless: A Sojourn Story in ~books

    creamsnail
    Link Parent
    I’d be super happy to try it out, I’ve watched tons of videos from real engineering and started checking out paper skies on YouTube. Both from what I’ve been advertised are on there. I’ve been...

    I’d be super happy to try it out, I’ve watched tons of videos from real engineering and started checking out paper skies on YouTube. Both from what I’ve been advertised are on there. I’ve been wanting to try before I bought and haven’t known anyone with access. Is there really quite a bit to watch on there and have you enjoyed it?

    Link to real engineering cause why not:

    https://youtube.com/@realengineering

    1 vote