Day 4: Perilous Pier

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • CameronDev@programming.devOPM
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    22 hours ago

    Rust

    spoiler
    fn check_string(s: &str, len: usize, offset: usize) -> bool {
            let mut char = "?";
            let mut i = offset;
            while let Some(c) = s.get(i..i+1) {
                if char == "?" {
                    char = c;
                }
                if c != "?" && c != char {
                    return false
                }
                i+=len
            }
            true
        }
    
        fn get_min_moves(s: &str) -> usize {
            'outer: for len in 1..s.len() {
                for offset in 0..len {
                    if !check_string(s, len, offset) {
                        continue 'outer
                    }
                }
    
                return len
            }
            s.len()
        }
    
        #[test]
        fn test_y2026_day4_part1() {
            let input = include_str!("../../input/2026/day_4.txt");
            let total: usize = input.lines().map(|l| {
                get_min_moves(l)
            }).sum();
    
            println!("{total}")
        }
    
        #[test]
        fn test_y2026_day4_part2() {
            let input = include_str!("../../input/2026/day_4.txt");
            let mut string: String = String::with_capacity(input.len());
            input.lines().for_each(|l| {
                string += l;
            });
            let total = get_min_moves(&string);
            println!("{total}")
        }
    

    Bit disappointing that the approach for pt1 works completely out of the box for pt2. Not terribly interesting. Might have a crack at an excel solution still. edit: CBF with excel, someone else can give it a go.

    • addie@feddit.uk
      link
      fedilink
      arrow-up
      3
      ·
      21 hours ago

      Yeah. Your mission, should you choose to accept it, is to suggest an algorithm that would work for part 1, but would fail for part 2 so you’d have to rewrite it.

      I suggest generating all the possible repeat sequences, and then testing them all to see whether they’d match the barcode. The longest ‘part 1’ barcode I have is 8 characters; given 6 colours, there’s 8 * 6 = 48 strings to test if there’s a single repeat match, (8/2) * 6^2 = 144 double repeat matches, (8/3) * 6^3 = 432 trebles, …, (8/8) * (6^8) = 1679616 eight character repeats. That shouldn’t take a ridiculous amount of time. Probably have to generate them on demand, as otherwise you’d need to spend a lot of £££ on RAM to hold them all, but I’m thinking you’d churn through them in a few minutes.

      My part 2 answer is 1000+, so testing the 6^1000 ~= 10^778 possibilities to see if any of them work would take too damn long. Although you never know, might get lucky if you just do them at random.

      • CameronDev@programming.devOPM
        link
        fedilink
        arrow-up
        3
        ·
        19 hours ago

        I do wonder if that was the intent, that people would brute force the instructions, and then need to do something more in line with what we all did for pt2? Maybe we’ve been doing this too long and we aren’t doing the dumb first solutions anymore :|