Skip to content

Commit

Permalink
Add Day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
dominique-mueller committed Dec 5, 2024
1 parent 8a2ad25 commit 84083fe
Show file tree
Hide file tree
Showing 5 changed files with 1,688 additions and 1 deletion.
119 changes: 119 additions & 0 deletions day-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Day 5: Print Queue

<br>

## Part 1

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](https://adventofcode.com/2017/day/1) 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:

```txt
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:

- `75` is correctly first because there are rules that put each other page after it: `75|47`, `75|61`, `75|53`, and `75|29`.
- `47` is correctly second because 75 must be before it (`75|47`) and every other page must be after it according to `47|61`, `47|53`, and
`47|29`.
- `61` is correctly in the middle because 75 and 47 are before it (`75|61` and `47|61`) and 53 and 29 are after it (`61|53` and `61|29`).
- `53` is correctly fourth because it is before page number 29 (`53|29`).
- `29` is the only page left and so is correctly last.

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:

```txt
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?**

<br>

## Part 2

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:

- `75,97,47,61,53` becomes `97,75,47,61,53`.
- `61,13,29` becomes `61,29,13`.
- `97,13,75,29,47` becomes `97,75,47,29,13`.

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?**
25 changes: 25 additions & 0 deletions day-5/day-5.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import path from 'node:path';

import { calculateSumOfMiddlePageNumbersOfCorrectedUpdates, calculateSumOfMiddlePageNumbersOfCorrectlyOrderedUpdates } from './day-5';

describe('Day 5: Print Queue', () => {
const updatesFilePath = path.join(__dirname, 'updates.txt');

it('Part 1: should calculate sum of middle page numbers of correctly ordered updates', async () => {
const expectedSumOfMiddlePageNumbersOfCorrectlyOrderedUpdates = 6051; // Verified for this dataset

const sumOfMiddlePageNumbersOfCorrectlyOrderedUpdates = await calculateSumOfMiddlePageNumbersOfCorrectlyOrderedUpdates(updatesFilePath);

assert.strictEqual(sumOfMiddlePageNumbersOfCorrectlyOrderedUpdates, expectedSumOfMiddlePageNumbersOfCorrectlyOrderedUpdates);
});

it('Part 2: should calculate sum of middle page numbers of corrected updates', async () => {
const expectedSumOfMiddlePageNumbersOfCorrectedUpdates = 5093; // Verified for this dataset

const sumOfMiddlePageNumbersOfCorrectedUpdates = await calculateSumOfMiddlePageNumbersOfCorrectedUpdates(updatesFilePath);

assert.strictEqual(sumOfMiddlePageNumbersOfCorrectedUpdates, expectedSumOfMiddlePageNumbersOfCorrectedUpdates);
});
});
171 changes: 171 additions & 0 deletions day-5/day-5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import fs from 'node:fs/promises';

/**
* Read file
*/
const readFile = async (filePath: string): Promise<string> => {
const fileContents = await fs.readFile(filePath, {
encoding: 'utf8',
});
const normalizedFileContents = fileContents.trim().split(/\r?\n/).join('\n');
return normalizedFileContents;
};

/**
* Parse page ordering rules
*/
const parsePageOrderingRules = (updatesFileContent: string): Array<[number, number]> => {
return updatesFileContent
.split('\n\n')[0]
.split('\n')
.map((pageRuleAsString) => {
return pageRuleAsString.split('|').map((pageAsString) => {
return parseInt(pageAsString, 10);
}) as [number, number];
});
};

/**
* Parse updates
*/
const parseUpdates = (updatesFileContent: string): Array<Array<number>> => {
return updatesFileContent
.split('\n\n')[1]
.split('\n')
.map((pageRuleAsString) => {
return pageRuleAsString.split(',').map((pageAsString) => {
return parseInt(pageAsString, 10);
});
});
};

/**
* Checks whether the given pages are correctly ordered
*/
const arePagesCorrectlyOrdered = (pages: Array<number>, pageOrderingRules: Array<[number, number]>): boolean => {
return pages.every((page, pageIndex) => {
// Find page rules
const applicablePageOrderingRules = pageOrderingRules.filter((pageOrderingRule) => {
return pageOrderingRule.some((pageOrderingRulePart) => {
return pageOrderingRulePart === page;
});
});

// Check whether page is valid based on applicable rules
const isValidPage = applicablePageOrderingRules.every((applicablePageOrderingRule) => {
return applicablePageOrderingRule[0] === page
? pages.slice(0, pageIndex).every((pageBeforeCurrentPage) => {
return pageBeforeCurrentPage !== applicablePageOrderingRule[1];
})
: pages.slice(pageIndex + 1).every((pageAfterCurrentPage) => {
return pageAfterCurrentPage !== applicablePageOrderingRule[0];
});
});

// Done
return isValidPage;
});
};

/**
* Correct order of pages
*/
const correctOrderOfPages = (pages: Array<number>, pageOrderingRules: Array<[number, number]>): Array<number> => {
// Re-order pages (in-place mutation) separately from original pages to keep track of progress
let correctlyOrderedPages = structuredClone(pages);

// Look at each page, and correct if necessary
for (let pageIndex = 0; pageIndex < pages.length; pageIndex++) {
// Find the last page that needs to be before the current page
const lastRequiredPreviousPageIndex = Math.max(
...pageOrderingRules
.filter((pageOrderingRule) => {
return pageOrderingRule[1] === pages[pageIndex];
})
.map((pageOrderingRule) => {
return pageOrderingRule[0];
})
.map((previousPage) => {
return correctlyOrderedPages.indexOf(previousPage);
}),
);

// Skip if this page does not need to be re-ordered (shifted back)
const pageIndexInCorrectlyOrderedPages = correctlyOrderedPages.indexOf(pages[pageIndex]);
if (lastRequiredPreviousPageIndex < pageIndexInCorrectlyOrderedPages) {
continue; // Continue early
}

// Re-assemble pages by shifting the current page back behind the last required previous page (in-place mutation)
correctlyOrderedPages = [
...correctlyOrderedPages.slice(0, pageIndexInCorrectlyOrderedPages),
...correctlyOrderedPages.slice(pageIndexInCorrectlyOrderedPages + 1, lastRequiredPreviousPageIndex),
correctlyOrderedPages[lastRequiredPreviousPageIndex],
correctlyOrderedPages[pageIndexInCorrectlyOrderedPages],
...correctlyOrderedPages.slice(lastRequiredPreviousPageIndex + 1),
];
}

// Done
return correctlyOrderedPages;
};

/**
* Calculate sum of middle page numbers
*/
const calculateSumOfMiddlePageNumbers = (updates: Array<Array<number>>): number => {
return updates
.map((pages) => {
return pages[(pages.length - 1) / 2];
})
.reduce((sumOfMiddlePageNumbers, middlePageNumber) => {
return sumOfMiddlePageNumbers + middlePageNumber;
}, 0);
};

/**
* Part 1: Calculate sum of middle page numbers of correctly ordered updates
*/
export const calculateSumOfMiddlePageNumbersOfCorrectlyOrderedUpdates = async (updatesFilePath: string) => {
// Get data
const updatesFileContent = await readFile(updatesFilePath);
const pageOrderingRules = parsePageOrderingRules(updatesFileContent);
const updates = parseUpdates(updatesFileContent);

// Find correctly ordered updates
const correctlyOrderedUpdates = updates.filter((pages) => {
return arePagesCorrectlyOrdered(pages, pageOrderingRules);
});

// Calculate sum of middle page numbers of correctly ordered updates
const sumOfMiddlePageNumbersOfCorrectlyOrderedUpdates = calculateSumOfMiddlePageNumbers(correctlyOrderedUpdates);

// Done
return sumOfMiddlePageNumbersOfCorrectlyOrderedUpdates;
};

/**
* Part 2: Calculate sum of middle page numbers of corrected updates
*/
export const calculateSumOfMiddlePageNumbersOfCorrectedUpdates = async (updatesFilePath: string) => {
// Get data
const updatesFileContent = await readFile(updatesFilePath);
const pageOrderingRules = parsePageOrderingRules(updatesFileContent);
const updates = parseUpdates(updatesFileContent);

// Find incorrectly ordered updates
const incorrectlyOrderedUpdates = updates.filter((pages) => {
return !arePagesCorrectlyOrdered(pages, pageOrderingRules);
});

// Correct incorrectly ordered updates
const correctedUpdates = incorrectlyOrderedUpdates.map((pages) => {
return correctOrderOfPages(pages, pageOrderingRules);
});

// Calculate sum of middle page numbers of correctly ordered updates
const sumOfMiddlePageNumbersOfCorrectedUpdates = calculateSumOfMiddlePageNumbers(correctedUpdates);

// Done
return sumOfMiddlePageNumbersOfCorrectedUpdates;
};
Loading

0 comments on commit 84083fe

Please sign in to comment.