Day 5 of Advent of Code in q

Author - Rory Kemp

Original Problem Statement

--- Day 5: Print Queue ---

Satisfied with their search on Ceres, the squadron of scholars suggests subsequently scanning the stationery stacks of sub-basement 17.

The North Pole printing department is busier than ever this close to Christmas, and while The Historians continue their search of this historically significant facility, an Elf operating a very familiar printer beckons you over.

The Elf must recognize you, because they waste no time explaining that the new sleigh launch safety manual updates won't print correctly. Failure to update the safety manuals would be dire indeed, so you offer your services.

Safety protocols clearly indicate that new pages for the safety manuals must be printed in a very specific order. The notation X|Y means that if both page number X and page number Y are to be produced as part of an update, page number X must be printed at some point before page number Y.

The Elf has for you both the page ordering rules and the pages to produce in each update (your puzzle input), but can't figure out whether each update has the pages in the right order.

For example:

47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13

75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

The first section specifies the page ordering rules, one per line. The first rule, 47|53, means that if an update includes both page number 47 and page number 53, then page number 47 must be printed at some point before page number 53. (47 doesn't necessarily need to be immediately before 53; other pages are allowed to be between them.)

The second section specifies the page numbers of each update. Because most safety manuals are different, the pages needed in the updates are different too. The first update, 75,47,61,53,29, means that the update consists of page numbers 75, 47, 61, 53, and 29.

To get the printers going as soon as possible, start by identifying which updates are already in the right order.

In the above example, the first update (75,47,61,53,29) is in the right order:

Because the first update does not include some page numbers, the ordering rules involving those missing page numbers are ignored.

The second and third updates are also in the correct order according to the rules. Like the first update, they also do not include every page number, and so only some of the ordering rules apply - within each update, the ordering rules that involve missing page numbers are not used.

The fourth update, 75,97,47,61,53, is not in the correct order: it would print 75 before 97, which violates the rule 97|75.

The fifth update, 61,13,29, is also not in the correct order, since it breaks the rule 29|13.

The last update, 97,13,75,29,47, is not in the correct order due to breaking several rules.

For some reason, the Elves also need to know the middle page number of each update being printed. Because you are currently only printing the correctly-ordered updates, you will need to find the middle page number of each correctly-ordered update. In the above example, the correctly-ordered updates are:

75,47,61,53,29
97,61,53,29,13
75,29,13

These have middle page numbers of 61, 53, and 29 respectively. Adding these page numbers together gives 143.

Of course, you'll need to be careful: the actual list of page ordering rules is bigger and more complicated than the above example.

Determine which updates are already in the correct order. What do you get if you add up the middle page number from those correctly-ordered updates?

First of all, we can read the input. The input contains two separate chunks, separated by an empty line. We can get these two chunks, firstly by joining each line by ".", and then by splitting on ".." Then split each chunk on "." again.
show (a;b): "." vs' ".." vs "." sv read0 `5.txt
("47|53";"97|13";"97|61";"97|47";"75|29";"61|13";"75|53";"29|13";"97|29";"53|..
("75,47,61,53,29";"97,61,53,29,13";"75,29,13";"75,97,47,61,53";"61,13,29";"97..

Now, for the first chunk, we split on "|" and cast to int. We also apply the `u attribute, which will speed up operations later. See Set Attribute for more information.

show a: `u#"J"$"|"vs' a
47 53
97 13
97 61
97 47
75 29
61 13
75 53
29 13
97 29
53 29
61 53
97 53
61 29
47 13
75 47
97 75
47 61
75 61
47 29
75 13
53 13

For the second chunk, it is the same except using "," to split instead.

show b: "J"$","vs' b
75 47 61 53 29
97 61 53 29 13
75 29 13
75 97 47 61 53
61 13 29
97 13 75 29 47

Now, we need to determine which pages are in the right order, according to the rules. We can do this by comparing each item to each other item. This is technically more inefficient than required, but it is convenient for later.

Given one page rule, say 75,47,61,53,29, let's check if it is in order. We know it should be from the problem statement. First of all, make a 2d grid of pairs of elements.

{x ,/:\: x} test: 75,47,61,53,29
75 75 75 47 75 61 75 53 75 29
47 75 47 47 47 61 47 53 47 29
61 75 61 47 61 61 61 53 61 29
53 75 53 47 53 61 53 53 53 29
29 75 29 47 29 61 29 53 29 29

Now, check which of these pairs are in the rules.

{(x ,/:\: x) in a} test
01111b
00111b
00011b
00001b
00000b

We see it has this upper triangular structure, where every element is less than the elements after it. This is what would be expected for it to be ordered. How do we test this? One way is to take the sum, and then compare it to til count x. Note that we also need to cast, as summing booleans returns a different type, as indicated by the i suffix.

{(til count x) ~ "j" $ 0N!sum (x ,/:\: x) in a} test
0 1 2 3 4i
1b

Now, run this on each item in b, to find the sorted pages.

show sorted: b where {(til count x) ~ "j" $ sum (x ,/:\: x) in a} each b 
75 47 61 53 29
97 61 53 29 13
75 29 13

To take the middle item of a list, we take the count, floor divide by 2, and index.

mid:{x count[x] div 2}; mid 1 2 3 4 5
3

Now we can find the sums of all the middle indices of the sorted page lists.

sum mid each sorted
143

Part 2:

Part 2 Original Description:

--- Part Two ---

While the Elves get to work printing the correctly-ordered updates, you have a little time to fix the rest of them.

For each of the incorrectly-ordered updates, use the page ordering rules to put the page numbers in the right order. For the above example, here are the three incorrectly-ordered updates and their correct orderings:

After taking only the incorrectly-ordered updates and ordering them correctly, their middle page numbers are 47, 29, and 47. Adding these together produces 123.

Find the updates which are not in the correct order. What do you get if you add up the middle page numbers after correctly ordering just those updates?

We can reuse our code from earlier, but adapt it to sort the values accordingly. Let's take 97,13,75,29,47 as an example.

{sum (x ,/:\: x) in a} 97,13,75,29,47
1 0 2 3 4

The correct result is 97,75,47,29,13 We can see that this corresponds to the indices in the desired result, so we can reorder it accordingly

{x iasc sum (x ,/:\: x) in a} 97,13,75,29,47
97 75 47 29 13

Now, apply this to each of the non-sorted items in b, and sum the mid values.

sum {mid x iasc sum (x ,/:\: x) in a} each b except sorted
123

Recap:

The full solution is this.


(a;b): "." vs' ".." vs "." sv read0 `5.txt
a: `u#"J"$"|"vs' a
b: "J"$","vs' b
sorted: b where {(til count x) ~ "j" $ sum (x ,/:\: x) in a} each b 
mid:{x count[x] div 2}
sum mid each sorted / part 1
sum {mid x iasc sum (x ,/:\: x) in a} each b except sorted / part 2

Extra!

For part 2, this was just one of several approaches that would work. It is actually possible to combine part 1 and part 2 in one, by checking if the reordered version was equal to the original. We can construct a table like this:

tab: {s:x iasc sum(x,/:\:x) in a; `s`m!(s~x; mid s)}each b
Where the s column marks if it was sorted originally, and the m column is the mid value. Then extracting the answers for Part 1 and Part 2 is simply

(exec sum m by s from tab) 10b