Day 5: Print Queue

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

  • RagingHungryPanda@lemm.ee
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    1 day ago

    I’ve got a “smart” solution and a really dumb one. I’ll start with the smart one (incomplete but you can infer). I did four different ways to try to get it faster, less memory, etc.

    // this is from a nuget package. My Mathy roommate told me this was a topological sort.
    // It's also my preferred, since it'd perform better on larger data sets.
    return lines
        .AsParallel()
        .Where(line => !IsInOrder(GetSoonestOccurrences(line), aggregateRules))
        .Sum(line => line.StableOrderTopologicallyBy(
                getDependencies: page =>
                    aggregateRules.TryGetValue(page, out var mustPreceed) ? mustPreceed.Intersect(line) : Enumerable.Empty<Page>())
            .Middle()
        );
    

    The dumb solution. These comparisons aren’t fully transitive. I can’t believe it works.

    public static SortedSet<Page> Sort3(Page[] line,
        Dictionary<Page, System.Collections.Generic.HashSet<Page>> rules)
    {
        // how the hell is this working?
        var sorted = new SortedSet<Page>(new Sort3Comparer(rules));
        foreach (var page in line)
            sorted.Add(page);
        return sorted;
    }
    
    public static Page[] OrderBy(Page[] line, Dictionary<Page, System.Collections.Generic.HashSet<Page>> rules)
    {
        return line.OrderBy(identity, new Sort3Comparer(rules)).ToArray();
    }
    
    sealed class Sort3Comparer : IComparer<Page>
    {
        private readonly Dictionary<Page, System.Collections.Generic.HashSet<Page>> _rules;
    
        public Sort3Comparer(Dictionary<Page, System.Collections.Generic.HashSet<Page>> rules) => _rules = rules;
    
        public int Compare(Page x, Page y)
        {
            if (_rules.TryGetValue(x, out var xrules))
            {
                if (xrules.Contains(y))
                    return -1;
            }
    
            if (_rules.TryGetValue(y, out var yrules))
            {
                if (yrules.Contains(x))
                    return 1;
            }
    
            return 0;
        }
    }
    
    Method Mean Error StdDev Gen0 Gen1 Allocated
    Part2_UsingList (literally just Insert) 660.3 us 12.87 us 23.20 us 187.5000 35.1563 1144.86 KB
    Part2_TrackLinkedList (wrong now) 1,559.7 us 6.91 us 6.46 us 128.9063 21.4844 795.03 KB
    Part2_TopologicalSort 732.3 us 13.97 us 16.09 us 285.1563 61.5234 1718.36 KB
    Part2_SortedSet 309.1 us 4.13 us 3.45 us 54.1992 10.2539 328.97 KB
    Part2_OrderBy 304.5 us 6.09 us 9.11 us 48.8281 7.8125 301.29 KB
  • Quant@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    5 days ago

    Uiua

    This is the first one that caused me some headache because I didn’t read the instructions carefully enough.
    I kept trying to create a sorted list for when all available pages were used, which got me stuck in an endless loop.

    Another fun part was figuring out to use memberof (∈) instead of find (⌕) in the last line of FindNext. So much time spent on debugging other areas of the code

    Run with example input here

    FindNext ← ⊙(
      ⊡1⍉,
      ⊃▽(▽¬)⊸∈
      ⊙⊙(⊡0⍉.)
      :⊙(⟜(▽¬∈))
    )
    
    # find the order of pages for a given set of rules
    FindOrder ← (
      ◴♭.
      []
      ⍢(⊂FindNext|⋅(>1⧻))
      ⊙◌⊂
    )
    
    PartOne ← (
      &rs ∞ &fo "input-5.txt"
      ∩°□°⊟⊜□¬⌕"\n\n".
      ⊙(⊜(□⊜⋕≠@,.)≠@\n.↘1)
      ⊜(⊜⋕≠@|.)≠@\n.
    
      ⊙.
      ¤
      ⊞(◡(°□:)
        ⟜:⊙(°⊟⍉)
        =2+∩∈
        ▽
        FindOrder
        ⊸≍°□:
        ⊙◌
      )
      ≡◇(⊡⌊÷2⧻.)▽♭
      /+
    )
    
    PartTwo ← (
      &rs ∞ &fo "input-5.txt"
      ∩°□°⊟⊜□¬⌕"\n\n".
      ⊙(⊜(□⊜⋕≠@,.)≠@\n.↘1)
      ⊜(⊜⋕≠@|.)≠@\n.
      ⊙.
      ⍜¤⊞(
        ◡(°□:)
        ⟜:⊙(°⊟⍉)
        =2+∩∈
        ▽
        FindOrder
        ⊸≍°□:
        ⊟∩□
      )
      ⊙◌
      ⊃(⊡0)(⊡1)⍉
      ≡◇(⊡⌊÷2⧻.)▽¬≡°□
      /+
    )
    
    &p "Day 5:"
    &pf "Part 1: "
    &p PartOne
    &pf "Part 2: "
    &p PartTwo
    
  • mykl@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    7 days ago

    Uiua

    Well it’s still today here, and this is how I spent my evening. It’s not pretty or maybe even good, but it works on the test data…

    spoiler

    Uses Kahn’s algorithm with simplifying assumptions based on the helpful nature of the data.

    Try it here

    Data  ()⊸≠@\n "47|53\n97|13\n97|61\n97|47\n75|29\n61|13\n75|53\n29|13\n97|29\n53|29\n61|53\n97|53\n61|29\n47|13\n75|47\n97|75\n47|61\n75|61\n47|29\n75|13\n53|13\n\n75,47,61,53,29\n97,61,53,29,13\n75,29,13\n75,97,47,61,53\n61,13,29\n97,13,75,29,47"
    Rs    ≡◇(⊜⋕⊸≠@|)▽⊸≡◇(⧻⊚⌕@|)Data
    Ps    ≡⍚(⊜⋕⊸≠@,)▽⊸≡◇(¬⧻⊚⌕@|)Data
    
    NoPred   ⊢▽:((=0/+⌕)⊙¤)◴♭⟜≡⊣                # Find entry without predecessors.
    GetLead  (:((¬/+=))⊙¤)NoPred             # Remove that leading entry.
    Rules    ⇌⊂⊃(⇌⊢°□⊢|≡°□↘1)[□⍢(GetLead|≠1)] Rs # Repeatedly find rule without predecessors (Kaaaaaahn!).
    
    Sorted    ⊏⍏⊗,Rules
    IsSorted  /×>0≡/-◫2⊗°□: Rules
    MidVal    :(⌊÷ 2)
    
    ⇌⊕□⊸≡IsSorted Ps        # Group by whether the pages are in sort order.
    ≡◇(/+≡◇(MidVal Sorted)) # Find midpoints and sum.
    
    
    • mykl@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      6 days ago

      Oh my. I just watched yernab’s video, and this becomes so much easier:

      # Order is totally specified, so sort by number of predecessors,
      # check to see which were already sorted, then group and sum each group.
      Data  (□⊜□⊸≠@\n)(¬⦷"\n\n")"47|53\n97|13\n97|61\n97|47\n75|29\n61|13\n75|53\n29|13\n97|29\n53|29\n61|53\n97|53\n61|29\n47|13\n75|47\n97|75\n47|61\n75|61\n47|29\n75|13\n53|13\n\n75,47,61,53,29\n97,61,53,29,13\n75,29,13\n75,97,47,61,53\n61,13,29\n97,13,75,29,47"
      Rs    ≡◇(⊜⋕⊸≠@|)°□⊢Data
      Ps    ≡⍚(⊜⋕⊸≠@,)°□⊣Data
      (/+≡◇(⊡⌊÷2⧻.))¬≡≍⟜:≡⍚(⊏⍏/+⊞(Rs)..).Ps