From 86b1ce17df228e0a29b1e5a475c31c9f7406342c Mon Sep 17 00:00:00 2001 From: Marcelo Adamatti Date: Mon, 9 Dec 2024 16:00:20 -0300 Subject: [PATCH] chore: wip --- src/2024/day5/README.md | 90 +++ src/2024/day5/part1.ts | 59 ++ src/2024/day5/part1.txt | 1374 ++++++++++++++++++++++++++++++++++++++ src/2024/day5/part2.ts | 83 +++ src/2024/day6/README.md | 182 +++++ src/2024/day6/part1.ts | 90 +++ src/2024/day6/part1.txt | 130 ++++ src/2024/day6/sample.txt | 10 + src/2024/day7/README.md | 48 ++ src/2024/day7/part1.ts | 68 ++ src/2024/day7/part1.txt | 850 +++++++++++++++++++++++ src/2024/day7/part2.ts | 80 +++ 12 files changed, 3064 insertions(+) create mode 100644 src/2024/day5/README.md create mode 100644 src/2024/day5/part1.ts create mode 100644 src/2024/day5/part1.txt create mode 100644 src/2024/day5/part2.ts create mode 100644 src/2024/day6/README.md create mode 100644 src/2024/day6/part1.ts create mode 100644 src/2024/day6/part1.txt create mode 100644 src/2024/day6/sample.txt create mode 100644 src/2024/day7/README.md create mode 100644 src/2024/day7/part1.ts create mode 100644 src/2024/day7/part1.txt create mode 100644 src/2024/day7/part2.ts diff --git a/src/2024/day5/README.md b/src/2024/day5/README.md new file mode 100644 index 0000000..f47107b --- /dev/null +++ b/src/2024/day5/README.md @@ -0,0 +1,90 @@ +--- 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: + +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: + +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? + +Your puzzle answer was 4957. + +The first half of this puzzle is complete! It provides one gold star: * + +--- 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: + +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? \ No newline at end of file diff --git a/src/2024/day5/part1.ts b/src/2024/day5/part1.ts new file mode 100644 index 0000000..bd95829 --- /dev/null +++ b/src/2024/day5/part1.ts @@ -0,0 +1,59 @@ +import fs from 'fs'; + +type Rule = { + before: number; + after: number; + }; + + function parseInput(input: string): {rules: Rule[], updates: number[][]} { + const [rulesSection, updatesSection] = input.trim().split('\n\n'); + + // Parse rules + const rules = rulesSection.split('\n').map(line => { + const [before, after] = line.split('|').map(Number); + return { before, after }; + }); + + // Parse updates + const updates = updatesSection.split('\n').map(line => + line.split(',').map(Number) + ); + + return { rules, updates }; + } + + function isValidOrder(update: number[], rules: Rule[]): boolean { + // Filter rules that only contain numbers present in this update + const applicableRules = rules.filter(rule => + update.includes(rule.before) && update.includes(rule.after) + ); + + // Check each applicable rule + for (const rule of applicableRules) { + const beforeIndex = update.indexOf(rule.before); + const afterIndex = update.indexOf(rule.after); + + if (beforeIndex > afterIndex) { + return false; + } + } + + return true; + } + + function getMiddleNumber(update: number[]): number { + const middleIndex = Math.floor(update.length / 2); + return update[middleIndex]; + } + + export function part1(input: string): number { + const { rules, updates } = parseInput(input); + + const validUpdates = updates.filter(update => isValidOrder(update, rules)); + const middleNumbers = validUpdates.map(getMiddleNumber); + + return middleNumbers.reduce((sum, num) => sum + num, 0); + } + + const input = fs.readFileSync('part1.txt', 'utf8'); + console.log(part1(input)); \ No newline at end of file diff --git a/src/2024/day5/part1.txt b/src/2024/day5/part1.txt new file mode 100644 index 0000000..c1cf89c --- /dev/null +++ b/src/2024/day5/part1.txt @@ -0,0 +1,1374 @@ +76|86 +34|42 +34|59 +82|34 +82|52 +82|71 +69|15 +69|83 +69|76 +69|79 +75|37 +75|92 +75|77 +75|26 +75|43 +92|55 +92|71 +92|42 +92|31 +92|87 +92|73 +68|51 +68|82 +68|88 +68|22 +68|69 +68|91 +68|78 +28|65 +28|11 +28|26 +28|78 +28|56 +28|91 +28|29 +28|51 +49|68 +49|86 +49|32 +49|56 +49|75 +49|43 +49|11 +49|46 +49|82 +72|93 +72|49 +72|73 +72|37 +72|31 +72|42 +72|83 +72|87 +72|76 +72|32 +42|28 +42|43 +42|18 +42|88 +42|32 +42|96 +42|13 +42|68 +42|51 +42|98 +42|47 +37|28 +37|79 +37|81 +37|52 +37|93 +37|47 +37|59 +37|49 +37|55 +37|94 +37|69 +37|63 +46|68 +46|34 +46|85 +46|92 +46|65 +46|82 +46|26 +46|78 +46|86 +46|28 +46|91 +46|98 +46|22 +73|42 +73|63 +73|11 +73|79 +73|28 +73|15 +73|96 +73|46 +73|18 +73|52 +73|93 +73|55 +73|81 +73|56 +29|69 +29|72 +29|52 +29|13 +29|34 +29|73 +29|37 +29|51 +29|75 +29|85 +29|59 +29|55 +29|95 +29|26 +29|98 +78|13 +78|34 +78|81 +78|37 +78|71 +78|69 +78|82 +78|77 +78|26 +78|72 +78|87 +78|55 +78|59 +78|79 +78|92 +78|52 +87|42 +87|22 +87|51 +87|56 +87|94 +87|46 +87|68 +87|18 +87|86 +87|15 +87|91 +87|49 +87|93 +87|32 +87|96 +87|29 +87|88 +94|46 +94|42 +94|91 +94|49 +94|98 +94|76 +94|68 +94|29 +94|75 +94|88 +94|96 +94|86 +94|28 +94|32 +94|95 +94|93 +94|15 +94|18 +79|18 +79|56 +79|94 +79|81 +79|91 +79|76 +79|49 +79|47 +79|32 +79|88 +79|96 +79|42 +79|28 +79|87 +79|22 +79|46 +79|83 +79|86 +79|93 +56|43 +56|69 +56|75 +56|98 +56|91 +56|51 +56|88 +56|68 +56|65 +56|13 +56|85 +56|37 +56|22 +56|72 +56|82 +56|86 +56|26 +56|34 +56|23 +56|92 +88|31 +88|82 +88|65 +88|37 +88|95 +88|59 +88|55 +88|52 +88|34 +88|51 +88|92 +88|43 +88|13 +88|75 +88|85 +88|78 +88|69 +88|72 +88|26 +88|71 +88|23 +91|78 +91|98 +91|82 +91|77 +91|34 +91|43 +91|13 +91|92 +91|23 +91|85 +91|65 +91|26 +91|69 +91|51 +91|88 +91|52 +91|37 +91|72 +91|55 +91|73 +91|95 +91|75 +86|59 +86|73 +86|69 +86|72 +86|51 +86|75 +86|29 +86|34 +86|91 +86|82 +86|37 +86|95 +86|85 +86|65 +86|78 +86|43 +86|98 +86|52 +86|26 +86|23 +86|13 +86|88 +86|92 +31|83 +31|29 +31|22 +31|18 +31|15 +31|81 +31|11 +31|46 +31|28 +31|86 +31|32 +31|49 +31|96 +31|42 +31|63 +31|87 +31|93 +31|47 +31|79 +31|56 +31|68 +31|91 +31|76 +31|94 +96|92 +96|78 +96|98 +96|75 +96|28 +96|95 +96|15 +96|77 +96|82 +96|43 +96|29 +96|46 +96|22 +96|91 +96|68 +96|88 +96|11 +96|86 +96|26 +96|85 +96|56 +96|51 +96|13 +96|65 +22|34 +22|29 +22|69 +22|88 +22|75 +22|77 +22|86 +22|59 +22|65 +22|51 +22|13 +22|95 +22|26 +22|82 +22|85 +22|23 +22|72 +22|37 +22|91 +22|73 +22|78 +22|98 +22|43 +22|92 +55|18 +55|86 +55|22 +55|68 +55|32 +55|96 +55|93 +55|31 +55|47 +55|76 +55|28 +55|94 +55|42 +55|15 +55|56 +55|87 +55|83 +55|79 +55|11 +55|81 +55|63 +55|71 +55|49 +55|46 +18|88 +18|68 +18|15 +18|29 +18|77 +18|92 +18|56 +18|51 +18|78 +18|96 +18|11 +18|82 +18|43 +18|46 +18|22 +18|75 +18|13 +18|91 +18|26 +18|85 +18|28 +18|98 +18|86 +18|95 +77|72 +77|59 +77|81 +77|76 +77|26 +77|23 +77|47 +77|31 +77|52 +77|93 +77|79 +77|63 +77|87 +77|71 +77|69 +77|42 +77|94 +77|83 +77|65 +77|55 +77|34 +77|49 +77|73 +77|37 +71|31 +71|11 +71|68 +71|28 +71|63 +71|22 +71|32 +71|79 +71|47 +71|93 +71|49 +71|86 +71|56 +71|46 +71|15 +71|81 +71|87 +71|42 +71|29 +71|83 +71|94 +71|18 +71|96 +71|76 +81|51 +81|56 +81|28 +81|76 +81|93 +81|15 +81|22 +81|47 +81|96 +81|88 +81|86 +81|75 +81|46 +81|87 +81|68 +81|98 +81|18 +81|91 +81|94 +81|32 +81|29 +81|11 +81|49 +81|42 +51|13 +51|69 +51|26 +51|23 +51|59 +51|71 +51|52 +51|55 +51|63 +51|34 +51|65 +51|98 +51|78 +51|73 +51|77 +51|37 +51|85 +51|79 +51|82 +51|92 +51|31 +51|43 +51|95 +51|72 +98|95 +98|37 +98|82 +98|83 +98|59 +98|34 +98|78 +98|92 +98|65 +98|52 +98|79 +98|26 +98|31 +98|69 +98|72 +98|55 +98|77 +98|63 +98|71 +98|73 +98|85 +98|43 +98|13 +98|23 +59|93 +59|79 +59|52 +59|11 +59|94 +59|96 +59|18 +59|31 +59|71 +59|32 +59|55 +59|83 +59|56 +59|49 +59|76 +59|42 +59|15 +59|28 +59|81 +59|46 +59|87 +59|68 +59|63 +59|47 +43|31 +43|63 +43|72 +43|85 +43|69 +43|59 +43|78 +43|55 +43|37 +43|79 +43|73 +43|81 +43|71 +43|26 +43|34 +43|13 +43|82 +43|23 +43|65 +43|77 +43|92 +43|87 +43|52 +43|83 +63|56 +63|94 +63|81 +63|15 +63|91 +63|22 +63|79 +63|88 +63|46 +63|49 +63|11 +63|87 +63|47 +63|93 +63|68 +63|86 +63|29 +63|28 +63|83 +63|42 +63|32 +63|76 +63|18 +63|96 +95|71 +95|63 +95|81 +95|23 +95|34 +95|31 +95|78 +95|72 +95|92 +95|65 +95|37 +95|85 +95|26 +95|52 +95|13 +95|69 +95|79 +95|59 +95|73 +95|82 +95|55 +95|43 +95|83 +95|77 +52|18 +52|11 +52|28 +52|46 +52|42 +52|68 +52|79 +52|22 +52|56 +52|31 +52|93 +52|83 +52|15 +52|49 +52|96 +52|87 +52|94 +52|47 +52|76 +52|63 +52|55 +52|81 +52|32 +52|71 +93|22 +93|75 +93|78 +93|32 +93|98 +93|18 +93|96 +93|85 +93|82 +93|51 +93|15 +93|11 +93|28 +93|49 +93|29 +93|68 +93|88 +93|86 +93|95 +93|56 +93|13 +93|46 +93|91 +93|43 +85|52 +85|87 +85|42 +85|92 +85|81 +85|23 +85|55 +85|59 +85|71 +85|63 +85|26 +85|79 +85|77 +85|73 +85|72 +85|83 +85|34 +85|65 +85|37 +85|82 +85|31 +85|76 +85|94 +85|69 +83|42 +83|94 +83|49 +83|46 +83|81 +83|93 +83|91 +83|11 +83|56 +83|96 +83|47 +83|76 +83|75 +83|87 +83|86 +83|32 +83|18 +83|29 +83|22 +83|15 +83|88 +83|51 +83|28 +83|68 +15|26 +15|29 +15|75 +15|23 +15|22 +15|51 +15|65 +15|88 +15|85 +15|98 +15|77 +15|86 +15|92 +15|68 +15|11 +15|82 +15|95 +15|13 +15|34 +15|56 +15|78 +15|43 +15|91 +15|28 +13|82 +13|77 +13|85 +13|23 +13|26 +13|94 +13|83 +13|76 +13|65 +13|59 +13|52 +13|92 +13|63 +13|79 +13|55 +13|72 +13|73 +13|69 +13|34 +13|87 +13|81 +13|71 +13|37 +13|31 +23|37 +23|55 +23|42 +23|49 +23|83 +23|96 +23|72 +23|94 +23|93 +23|46 +23|81 +23|52 +23|47 +23|76 +23|69 +23|31 +23|73 +23|79 +23|18 +23|63 +23|59 +23|87 +23|32 +23|71 +65|71 +65|55 +65|94 +65|73 +65|83 +65|34 +65|31 +65|49 +65|81 +65|93 +65|87 +65|42 +65|59 +65|18 +65|23 +65|63 +65|69 +65|47 +65|37 +65|52 +65|72 +65|76 +65|79 +65|32 +32|29 +32|78 +32|51 +32|88 +32|98 +32|11 +32|13 +32|85 +32|95 +32|22 +32|43 +32|91 +32|46 +32|86 +32|92 +32|56 +32|15 +32|75 +32|77 +32|82 +32|96 +32|28 +32|18 +32|68 +11|92 +11|75 +11|34 +11|23 +11|26 +11|88 +11|85 +11|56 +11|91 +11|22 +11|51 +11|78 +11|77 +11|72 +11|65 +11|29 +11|86 +11|82 +11|68 +11|98 +11|43 +11|95 +11|13 +11|37 +47|98 +47|56 +47|29 +47|88 +47|15 +47|68 +47|78 +47|28 +47|96 +47|32 +47|13 +47|11 +47|46 +47|91 +47|43 +47|85 +47|95 +47|22 +47|51 +47|49 +47|18 +47|86 +47|93 +47|75 +26|55 +26|79 +26|63 +26|37 +26|94 +26|81 +26|83 +26|65 +26|73 +26|87 +26|69 +26|47 +26|59 +26|34 +26|52 +26|32 +26|71 +26|49 +26|76 +26|23 +26|72 +26|93 +26|31 +26|42 +76|11 +76|95 +76|49 +76|78 +76|68 +76|96 +76|42 +76|43 +76|88 +76|47 +76|28 +76|29 +76|93 +76|91 +76|15 +76|51 +76|18 +76|46 +76|56 +76|32 +76|22 +76|98 +76|75 +34|71 +34|94 +34|52 +34|37 +34|49 +34|63 +34|83 +34|81 +34|87 +34|96 +34|18 +34|32 +34|72 +34|23 +34|69 +34|79 +34|93 +34|47 +34|73 +34|31 +34|76 +34|55 +82|87 +82|37 +82|76 +82|26 +82|31 +82|63 +82|55 +82|94 +82|83 +82|42 +82|92 +82|73 +82|69 +82|81 +82|65 +82|77 +82|47 +82|72 +82|23 +82|59 +82|79 +69|49 +69|59 +69|93 +69|11 +69|87 +69|18 +69|71 +69|55 +69|32 +69|46 +69|31 +69|96 +69|81 +69|47 +69|42 +69|73 +69|28 +69|94 +69|63 +69|52 +75|31 +75|85 +75|82 +75|73 +75|13 +75|98 +75|51 +75|55 +75|65 +75|23 +75|59 +75|72 +75|52 +75|78 +75|69 +75|63 +75|95 +75|71 +75|34 +92|26 +92|47 +92|77 +92|69 +92|37 +92|34 +92|59 +92|72 +92|79 +92|63 +92|52 +92|65 +92|76 +92|23 +92|93 +92|81 +92|94 +92|83 +68|13 +68|85 +68|43 +68|26 +68|92 +68|72 +68|37 +68|75 +68|73 +68|95 +68|34 +68|98 +68|23 +68|29 +68|86 +68|77 +68|65 +28|34 +28|85 +28|86 +28|68 +28|13 +28|88 +28|22 +28|23 +28|95 +28|92 +28|98 +28|82 +28|75 +28|43 +28|77 +28|72 +49|91 +49|15 +49|29 +49|96 +49|92 +49|85 +49|13 +49|95 +49|18 +49|28 +49|22 +49|88 +49|51 +49|78 +49|98 +72|96 +72|71 +72|63 +72|55 +72|47 +72|52 +72|94 +72|15 +72|81 +72|18 +72|69 +72|59 +72|46 +72|79 +42|95 +42|46 +42|75 +42|49 +42|11 +42|78 +42|22 +42|93 +42|29 +42|56 +42|15 +42|86 +42|91 +37|15 +37|87 +37|46 +37|32 +37|73 +37|18 +37|42 +37|83 +37|76 +37|31 +37|96 +37|71 +46|29 +46|43 +46|88 +46|11 +46|13 +46|75 +46|51 +46|95 +46|15 +46|77 +46|56 +73|71 +73|47 +73|31 +73|59 +73|94 +73|32 +73|76 +73|87 +73|83 +73|49 +29|78 +29|77 +29|43 +29|91 +29|88 +29|65 +29|82 +29|92 +29|23 +78|73 +78|63 +78|94 +78|31 +78|23 +78|85 +78|65 +78|83 +87|98 +87|11 +87|95 +87|76 +87|28 +87|47 +87|75 +94|47 +94|51 +94|11 +94|22 +94|43 +94|56 +79|75 +79|29 +79|11 +79|68 +79|15 +56|78 +56|29 +56|77 +56|95 +88|77 +88|98 +88|73 +91|71 +91|59 +86|77 + +46,91,56,28,43,75,86,32,13,85,95,68,22,15,11 +34,23,69,77,81,83,65,47,76,94,55,37,93,52,63,73,42,59,31 +83,87,86,91,11,56,49,15,75 +81,76,32,18,96,28,11,68,22,91,51 +86,29,75,51,95,43,78,13,92,65,34,23,59 +22,29,88,51,95,78,13,82,92,77,26,65,34,72,73 +15,22,75,11,28,13,68,92,56,34,29,65,43 +71,56,94,68,81,96,32,11,52,76,18,42,83,15,47,46,31,28,63,55,79,87,49 +63,92,78,82,72,81,26,23,13,43,71,77,52 +91,13,34,98,73,82,95,72,37,69,43,65,86,75,59,77,78,85,92,51,88 +49,28,29,76,22,93,31,42,83,86,81 +78,85,92,72,69,59,52,71,63,79,87 +76,96,47,56,79,63,52,42,31,11,55 +63,69,79,83,13 +87,23,18,83,76,37,71,93,72,49,73,94,79,81,59,96,52 +51,26,34,72,69,71,63 +86,29,88,43,68,26,75,15,51,92,98,91,28,82,65,13,78 +75,87,93,28,42,47,83,49,46,11,81,96,18,68,22,86,91,56,32,15,94 +88,75,98,95,78,26,65,23,37,69,55 +55,73,18,63,94,87,47,32,46,69,42,28,93,76,96,81,31,49,71,15,79 +26,69,95,92,91,13,43,23,75 +11,98,46,78,75,29,51,92,85,28,91,15,86,96,82,26,77 +91,13,77,34,51 +96,46,15,28,11,56,68,22,86,29,91,88,75,51,98,95,43,78,85,82,92 +91,11,28,18,76,93,46,29,56,83,15,22,79,68,96,49,86,87,42 +11,56,22,86,29,91,88,75,51,98,95,43,78,92,77,26,65,34,72 +76,42,47,93,49,32,46,15,28,11,56,22,29,88,75,51,98,95,43 +96,86,75,11,49,15,18,28,78,56,46,22,93,98,91,42,51,43,88,29,95 +29,98,51,91,34,59,82,73,77,13,52,78,37,69,72,26,65,43,85 +88,75,95,78,13,85,77,65,59,52,71 +68,88,29,18,87,32,76,81,83,94,96,46,75 +15,32,13,96,47,29,78 +37,63,83,71,95,92,72,77,52,43,65 +85,82,92,77,26,65,34,23,37,69,73,59,52,71,31,63,79,81,87,94,76 +79,83,87,94,47,11,56,86,29,91,88 +65,15,77,98,82,11,46,68,43 +37,92,95,68,77,51,22,86,91,82,85,13,65,23,56,88,98 +31,77,76,59,81,47,63,71,37,92,94 +65,98,34,26,29,82,52,37,69 +76,93,49,96,46,28,51 +75,42,94,96,68,46,32,98,47,95,88 +95,43,13,85,82,92,77,26,65,23,72,59,52,55,71,31,63,79,83 +65,69,73,71,93 +46,11,68,29,98,95,78,85,82,26,65 +78,18,96,88,91,11,29,28,13,92,98,46,68,22,56,32,43,86,85,15,82,95,75 +82,92,26,65,34,23,72,37,73,59,55,71,63,79,81,87,94 +92,11,15,34,77,98,95 +43,13,26,78,11,29,51,91,96,15,82,56,46,68,28,77,75,86,92 +93,81,83,49,46,52,56,71,68 +51,15,49,95,88,28,29,43,96,98,32,11,68,93,46,22,42,56,47,76,18,75,86 +88,75,51,98,43,78,85,82,92,77,26,65,34,23,72,37,59,52,71 +86,71,56,22,42,94,15 +96,93,76,91,18,22,42,56,43 +85,82,92,77,26,65,34,23,72,69,73,59,52,55,71,31,63,79,83,81,87,94,76 +81,47,49,32,46,15,28,11,68,86,29,91,51 +63,79,83,81,87,94,76,47,49,32,18,96,46,28,11,56,22,29,91 +87,42,79,88,46,83,22,56,49,76,96,47,32 +37,52,31,94,42,93,49,32,96,46,15 +11,68,22,86,91,88,51,98,95,13,85,82,92,77,65,34,72 +56,15,88,47,22,91,95,11,29,96,42,28,86,32,68,51,93,98,76,94,46,75,49 +82,56,68,91,51,96,78,86,75,85,15,18,88,28,49 +18,96,55,83,59,79,94,73,46,63,47,31,15,76,71,11,28,32,87 +47,93,49,96,15,28,56,22,29,91,43,78,13 +13,85,23,69,73,52,55,31,63,83,87 +49,32,18,56,68,86,91,88,75,51,78,85,82 +79,81,87,94,93,49,32,18,46,15,28,11,56,68,29,91,88 +83,37,34,92,13,23,78,31,79,82,77 +52,83,28,81,63,55,76,15,42,59,71,11,46,87,79,93,73,94,47,18,49 +73,23,51,95,29,86,26,72,78,75,77,91,13,85,82,92,69,98,65,88,59 +32,18,96,46,11,56,68,22,29,91,75,51,98,95,43,13,92 +69,73,93,18,47,55,83,94,71,63,37,76,52,34,49,72,81,79,87,32,31 +18,68,88,95,13,85,77 +92,82,95,85,77,18,46,86,15 +15,11,56,68,22,86,29,88,75,51,98,95,43,78,13,85,92,77,26,65,34 +22,56,98,85,28,29,91,18,92,13,95,77,68,88,43 +31,85,26,52,71,73,77,59,95,34,98,55,79 +92,72,82,68,51,75,86,11,56 +51,47,91,56,28,11,98 +55,81,76,15,11,32,49,71,73,47,59,52,46,31,63,79,28,96,94 +47,49,96,28,75,95,13 +55,83,81,76,93,96,28,68,22 +11,56,68,22,91,75,51,98,78,13,82,77,26,65,34,23,72 +71,79,11,83,15,18,31,86,47,22,96,87,81,94,93,68,49,63,56 +79,59,37,71,49,31,76,47,23,55,63,83,87,73,18,93,32,69,96 +37,71,72,26,87,55,92,85,65,31,13,79,69,73,81,59,94 +94,68,71,49,52,63,55,83,32,87,93,56,76,47,15,11,81,96,79,28,42,46,18 +56,47,31,81,22,96,18,29,79 +18,28,68,22,29,88,51 +51,95,85,92,26,65,34,23,72,73,59,55,71,31,63 +26,87,34,65,23,47,72,31,83,77,92 +72,59,87,55,52,34,71,37,93,49,31,79,42 +28,71,55,22,76,46,93,15,18,79,32,63,81,56,68 +29,91,88,75,51,98,95,43,78,13,82,77,65,34,23,72,37,69,73,59,52 +52,59,96,32,42,83,15,55,11,46,63,81,49,18,93,76,31,87,71 +65,86,72,34,11,77,56,43,95,29,75,51,13 +42,32,76,49,29,46,88,91,93,96,81 +65,77,63,71,85,95,83,69,73 +42,47,93,32,18,96,15,28,11,56,68,22,86,29,91,88,75,51,98,95,43 +96,46,11,56,68,22,91,78,13,82,26 +92,26,78,82,69,79,52,23,77,87,37,63,65,72,81,73,34,83,13 +34,23,72,37,69,52,55,71,31,63,79,83,81,87,94,76,42,93,49,32,18 +96,63,91,28,87,22,56,42,15,86,76 +88,98,95,85,82,77,26,65,34,72,69,73,59,52,55 +83,18,81,68,46,42,47,71,63,28,94,11,56,55,22,79,87,31,15,76,93,32,96 +37,42,73,87,32 +73,59,86,29,88,37,85 +81,32,94,63,28,47,42,83,96,79,18,55,59,46,93,87,76,11,15,31,49 +63,79,83,81,87,94,76,42,93,49,32,18,96,46,15,28,11,56,68,22,86,29,91 +65,34,73,59,52,94,93 +81,76,47,49,32,18,28 +52,55,71,31,63,79,83,81,18,96,46,15,11,56,68 +59,63,76,23,94,49,34,52,87,18,73,83,47,31,79 +59,37,87,42,31,81,83,15,94,18,52,46,32 +77,34,23,72,37,69,59,52,55,71,63,87,94,76,47 +88,18,87,91,94,76,75,46,51,22,15,32,11,49,47 +76,55,22,56,42,32,47 +94,55,26,23,63,85,77,52,79,59,69,92,65,81,13,37,83,73,71 +47,93,49,18,96,28,68,86,91,88,75,51,98,95,43,78,13 +59,55,23,83,31,82,37,13,65,63,85,52,71,78,79,87,73,92,77,81,69 +96,46,15,28,11,56,68,22,86,29,91,75,51,98,95,43,78,13,85,82,92,77,26 +49,18,96,46,15,28,86,29,91,75,51,98,78,13,82 +59,52,83,81,87,18,28 +55,47,81,94,79,92,72,23,65,34,69,87,59,77,26,73,31,76,37,42,52 +72,85,92,37,51,43,86,95,29 +87,94,47,93,49,46,15,28,11,56,22,86,29,91,88,51,98 +88,75,51,98,95,43,78,13,85,82,92,77,26,23,72,37,69,73,52,55,71 +76,28,11,86,98,95,43 +11,29,77,34,15,75,95,88,51,13,92,86,78,56,85,98,91,65,26,43,28 +29,91,51,98,95,78,85,82,77,26,23,72,37,69,73,59,52 +32,86,11,95,98,85,18,28,96,51,78,15,92 +88,43,78,26,65,34,23,72,73,59,71 +73,92,43,98,59,65,75,37,88,95,78,69,85 +86,42,96,94,15 +92,73,55,81,87,94,47 +94,72,26,73,87,47,63,42,59,49,55,81,76 +13,85,82,92,77,34,23,72,37,69,73,59,55,71,79,83,81,87,94 +55,13,77,71,52,65,95,63,43,51,85,31,72 +79,47,96,81,94,42,52,76,31,73,63,71,18,93,46,28,55 +59,52,31,83,81,94,76,42,47,93,49,32,18,96,15,28,11 +96,95,86,75,15,43,76,56,42 +96,83,63,46,47,71,28,15,56,22,86 +47,93,49,32,18,96,46,15,28,11,68,22,86,29,91,88,75,51,98,95,43,78,13 +15,88,81,56,68,46,28,47,22,51,49,91,42 +31,63,81,87,47,68,22 +22,86,91,88,75,51,98,95,43,78,13,85,82,92,77,26,65,34,23,72,37,69,73 +51,96,75,46,93,56,22,68,18,42,94,49,91,47,81,29,15,11,76,86,88 +94,42,47,93,18,96,46,15,28,11,56,22,29,91,88,51,95 +96,43,51,56,15,11,42,29,32,22,68,95,18,28,76,75,91,88,98,93,49,86,46 +11,86,68,92,22,65,46 +56,22,91,75,43,13,85,82,92,77,65,23,72 +81,79,69,59,76,83,94,23,42,26,55 +37,26,92,79,31,63,76,69,82,72,81,59,77,73,42,65,23,71,83,34,55,94,52 +69,59,71,63,81,42,49,32,96,46,28 +77,92,23,79,72,83,43,34,37,26,69,31,55,59,73,81,78,63,13 +32,72,47,46,42,55,69,73,93,83,76,37,63,52,96,31,79,59,18,81,49,94,71 +42,94,79,73,47,81,32 +87,42,93,32,96,46,28,11,86,91,88,75,98 +29,56,76,88,15,22,46,81,86,42,11,28,93,87,91,51,32,75,49 +32,28,22,91,95,85,92 +37,96,83,42,47,71,94,72,87,32,63,31,46 +83,18,87,76,93,47,79,81,88 +77,65,23,72,37,59,52,55,71,31,63,79,83,81,87,94,76,42,93 +65,86,23,43,59,37,98,26,34,91,72,82,92 +88,78,85,34,69,52,71 +69,73,59,52,71,31,63,79,83,81,87,94,76,42,47,93,49,32,96,15,28 +28,11,22,86,29,91,75,51,78,85,82,92,77,65,23 +82,65,13,26,23,29,72,95,98,75,92,77,86,69,68 +91,51,95,82,77,73,55 +82,92,77,26,73,52,55,71,83,94,42 +55,31,63,79,83,32,96,28,56,68,22 +42,49,32,96,15,28,11,56,68,22,86,51,78 +63,23,73,81,83,69,71,65,82,77,55,42,31,94,76,87,72,34,79,26,59 +98,95,78,92,77,26,34,23,69,73,59,52,55,63,79 +15,79,63,83,69,71,94,37,18 +15,28,11,22,86,29,91,88,75,51,98,95,43,78,85,82,92,77,26,65,34 +15,47,42,79,94,29,68,46,18 +75,51,98,95,43,13,82,92,77,26,65,34,72,37,59,52,55,71,31 +32,11,56,91,88,75,51,98,95,78,92 +75,51,95,26,37 +28,49,11,93,32,29,76,18,75,51,56,88,15,98,86,47,46,95,42,91,94,22,96 +96,46,15,11,22,98,43,13,92,77,26 +79,81,13,71,34,92,63,69,73,43,78,82,59,72,55 +11,81,22,15,68,42,51,29,28,94,47,96,32,87,56,49,93,75,76,46,86,88,18 +68,22,29,91,98,43,78,85,82,92,77,65,34,23,72,37,69 +51,29,86,91,93,85,46,18,88,95,13 +93,81,26,69,76,55,77 +65,43,82,52,73,77,78,13,92,59,75,34,98 +47,18,96,46,15,28,11,56,22,86,29,91,51,98,95,43,13 +94,77,37,76,31,55,26,65,87,73,93,72,81 +26,65,34,23,72,37,69,73,55,31,63,83,87,76,93 +96,15,95,56,88,98,43,28,91,77,82,86,75 +46,47,81,63,93,37,94,73,72,87,31 +52,87,49,31,93,63,73 +46,49,18,28,31,15,93,11,87 +93,51,18,11,42,88,56,49,95,78,86,47,98,32,28 +59,94,79,31,76,72,77,37,42,87,83,26,82,34,23,69,63 +28,77,43,51,88,92,22,86,98 \ No newline at end of file diff --git a/src/2024/day5/part2.ts b/src/2024/day5/part2.ts new file mode 100644 index 0000000..d991599 --- /dev/null +++ b/src/2024/day5/part2.ts @@ -0,0 +1,83 @@ +import fs from 'fs'; + +type Rule = [number, number]; // [before, after] +type Sequence = number[]; + +function buildGraph(rules: Rule[]): Map> { + const graph = new Map>(); + + for (const [before, after] of rules) { + if (!graph.has(before)) graph.set(before, new Set()); + if (!graph.has(after)) graph.set(after, new Set()); + graph.get(before)!.add(after); + } + + return graph; +} + +function isValidSequence(sequence: Sequence, graph: Map>): boolean { + for (let i = 0; i < sequence.length; i++) { + for (let j = i + 1; j < sequence.length; j++) { + const before = sequence[i]; + const after = sequence[j]; + // If there's a rule saying after should come before before, sequence is invalid + if (graph.get(after)?.has(before)) { + return false; + } + } + } + return true; +} + +function topologicalSort(sequence: Sequence, graph: Map>): Sequence { + const nodes = new Set(sequence); + const result: number[] = []; + + while (nodes.size > 0) { + // Find a node with no incoming edges from remaining nodes + const node = Array.from(nodes).find(n => + Array.from(nodes).every(other => !graph.get(other)?.has(n)) + ); + + if (node === undefined) { + throw new Error("Cycle detected in graph"); + } + + result.push(node); + nodes.delete(node); + } + + return result; +} + +function part2(input: string): number { + // Parse input + const [rulesStr, sequencesStr] = input.trim().split('\n\n'); + + // Parse rules + const rules: Rule[] = rulesStr.split('\n').map(line => { + const [before, after] = line.split('|').map(Number); + return [before, after]; + }); + + // Parse sequences + const sequences: Sequence[] = sequencesStr.split('\n').map(line => + line.split(',').map(Number) + ); + + // Build dependency graph + const graph = buildGraph(rules); + + // Find invalid sequences and reorder them + const invalidSequences = sequences.filter(seq => !isValidSequence(seq, graph)); + const reorderedSequences = invalidSequences.map(seq => topologicalSort(seq, graph)); + + // Get middle numbers and sum them + return reorderedSequences.reduce((sum, seq) => { + const middle = Math.floor(seq.length / 2); + return sum + seq[middle]; + }, 0); +} + +const input = fs.readFileSync('part1.txt', 'utf8'); +console.log(part2(input)); \ No newline at end of file diff --git a/src/2024/day6/README.md b/src/2024/day6/README.md new file mode 100644 index 0000000..500385c --- /dev/null +++ b/src/2024/day6/README.md @@ -0,0 +1,182 @@ +--- Day 6: Guard Gallivant --- +The Historians use their fancy device again, this time to whisk you all away to the North Pole prototype suit manufacturing lab... in the year 1518! It turns out that having direct access to history is very convenient for a group of historians. + +You still have to be careful of time paradoxes, and so it will be important to avoid anyone from 1518 while The Historians search for the Chief. Unfortunately, a single guard is patrolling this part of the lab. + +Maybe you can work out where the guard will go ahead of time so that The Historians can search safely? + +You start by making a map (your puzzle input) of the situation. For example: + +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... +The map shows the current position of the guard with ^ (to indicate the guard is currently facing up from the perspective of the map). Any obstructions - crates, desks, alchemical reactors, etc. - are shown as #. + +Lab guards in 1518 follow a very strict patrol protocol which involves repeatedly following these steps: + +If there is something directly in front of you, turn right 90 degrees. +Otherwise, take a step forward. +Following the above protocol, the guard moves up several times until she reaches an obstacle (in this case, a pile of failed suit prototypes): + +....#..... +....^....# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#... +Because there is now an obstacle in front of the guard, she turns right before continuing straight in her new facing direction: + +....#..... +........># +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#... +Reaching another obstacle (a spool of several very long polymers), she turns right again and continues downward: + +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#......v. +........#. +#......... +......#... +This process continues for a while, but the guard eventually leaves the mapped area (after walking past a tank of universal solvent): + +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#v.. +By predicting the guard's route, you can determine which specific positions in the lab will be in the patrol path. Including the guard's starting position, the positions visited by the guard before leaving the area are marked with an X: + +....#..... +....XXXXX# +....X...X. +..#.X...X. +..XXXXX#X. +..X.X.X.X. +.#XXXXXXX. +.XXXXXXX#. +#XXXXXXX.. +......#X.. +In this example, the guard will visit 41 distinct positions on your map. + +Predict the path of the guard. How many distinct positions will the guard visit before leaving the mapped area? + +Your puzzle answer was 5453. + +The first half of this puzzle is complete! It provides one gold star: * + +--- Part Two --- +While The Historians begin working around the guard's patrol route, you borrow their fancy device and step outside the lab. From the safety of a supply closet, you time travel through the last few months and record the nightly status of the lab's guard post on the walls of the closet. + +Returning after what seems like only a few seconds to The Historians, they explain that the guard's patrol area is simply too large for them to safely search the lab without getting caught. + +Fortunately, they are pretty sure that adding a single new obstruction won't cause a time paradox. They'd like to place the new obstruction in such a way that the guard will get stuck in a loop, making the rest of the lab safe to search. + +To have the lowest chance of creating a time paradox, The Historians would like to know all of the possible positions for such an obstruction. The new obstruction can't be placed at the guard's starting position - the guard is there right now and would notice. + +In the above example, there are only 6 different positions where a new obstruction would cause the guard to get stuck in a loop. The diagrams of these six situations use O to mark the new obstruction, | to show a position where the guard moves up/down, - to show a position where the guard moves left/right, and + to show a position where the guard moves both up/down and left/right. + +Option one, put a printing press next to the guard's starting position: + +....#..... +....+---+# +....|...|. +..#.|...|. +....|..#|. +....|...|. +.#.O^---+. +........#. +#......... +......#... +Option two, put a stack of failed suit prototypes in the bottom right quadrant of the mapped area: + + +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +......O.#. +#......... +......#... +Option three, put a crate of chimney-squeeze prototype fabric next to the standing desk in the bottom right quadrant: + +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +.+----+O#. +#+----+... +......#... +Option four, put an alchemical retroencabulator near the bottom left corner: + +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +..|...|.#. +#O+---+... +......#... +Option five, put the alchemical retroencabulator a bit to the right instead: + +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +....|.|.#. +#..O+-+... +......#... +Option six, put a tank of sovereign glue right next to the tank of universal solvent: + +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +.+----++#. +#+----++.. +......#O.. +It doesn't really matter what you choose to use as an obstacle so long as you and The Historians can put it into position without the guard noticing. The important thing is having enough options that you can find one that minimizes time paradoxes, and in this example, there are 6 different positions you could choose. + +You need to get the guard stuck in a loop by adding a single new obstruction. How many different positions could you choose for this obstruction? + +Answer: 2188 diff --git a/src/2024/day6/part1.ts b/src/2024/day6/part1.ts new file mode 100644 index 0000000..dc87d62 --- /dev/null +++ b/src/2024/day6/part1.ts @@ -0,0 +1,90 @@ +import fs from 'fs'; + +export type Position = { + x: number; + y: number; + }; + + export type Direction = "^" | ">" | "v" | "<"; + + const DIRECTIONS: Direction[] = ["^", ">", "v", "<"]; + + export function parseInput(input: string): { map: string[][], startPos: Position, startDir: Direction } { + const lines = input.split("\n"); + const map = lines.map(line => line.split("")); + + // Find starting position + let startPos: Position = { x: 0, y: 0 }; + let startDir: Direction = "^"; + + for (let y = 0; y < map.length; y++) { + for (let x = 0; x < map[y].length; x++) { + if (DIRECTIONS.includes(map[y][x] as Direction)) { + startPos = { x, y }; + startDir = map[y][x] as Direction; + // map[y][x] = "."; // Replace direction with empty space + break; + } + } + } + + return { map, startPos, startDir }; + } + + export function getNextPosition(pos: Position, dir: Direction): Position { + switch (dir) { + case "^": return { x: pos.x, y: pos.y - 1 }; + case ">": return { x: pos.x + 1, y: pos.y }; + case "v": return { x: pos.x, y: pos.y + 1 }; + case "<": return { x: pos.x - 1, y: pos.y }; + } + } + + export function turnRight(dir: Direction): Direction { + const currentIndex = DIRECTIONS.indexOf(dir); + return DIRECTIONS[(currentIndex + 1) % 4]; + } + + export function isOutOfBounds(pos: Position, map: string[][]): boolean { + return pos.y < 0 || pos.y >= map.length || pos.x < 0 || pos.x >= map[0].length; + } + + export function hasObstacle(pos: Position, map: string[][]): boolean { + return ['#', 'O'].includes(map[pos.y][pos.x]); + } + + function part1(input: string): number { + const { map, startPos, startDir } = parseInput(input); + const visited = new Set(); + + let currentPos = startPos; + let currentDir = startDir; + + // Add starting position to visited + visited.add(`${currentPos.x},${currentPos.y}`); + + while (true) { + // Check position in front + const nextPos = getNextPosition(currentPos, currentDir); + + // Check if out of bounds + if (isOutOfBounds(nextPos, map)) { + break; + } + + // Check if obstacle ahead + if (hasObstacle(nextPos, map)) { + currentDir = turnRight(currentDir); + continue; + } + + // Move forward + currentPos = nextPos; + visited.add(`${currentPos.x},${currentPos.y}`); + } + + return visited.size; + } + + // const input = fs.readFileSync('part1.txt', 'utf8'); + // console.log(part1(input)); \ No newline at end of file diff --git a/src/2024/day6/part1.txt b/src/2024/day6/part1.txt new file mode 100644 index 0000000..5b7392c --- /dev/null +++ b/src/2024/day6/part1.txt @@ -0,0 +1,130 @@ +.......................#....#....................#....#..##.##.........................................#...........#...........#.. +.............#........................................#....................#............#.......................#............#.... +.........#.............#..#....#..........................#.......#.#.......#...............................................##.... +..........#....#....................................................#....................#...........#....#....................... +.............#...#....................#...........#.........#...........................#............#............................ +..........................................................................................#.....#.......#...................##.... +...............................................#.#......#..............#...#...................##...........#....#..#...#......... +..#.................#......#........#......................#.......#...............................#.............................. +.........................#...............#.....................................#....#..#..#............................#.......... +......................#.................................................................#..........#..............#...#......#.... +............................#.....##..........#.....................#..........#..#.......#....................................... +......#...###...........#.................................................................#....................................... +......................#.#..#................#..........#................#...........#............#..............#...#.........#... +....#.........................#..#...............................................#....#.............#............................. +..#............#...#............#..........#...............#.....#....##.........................#........#....................... +..................#............#..............#..........................................................#..#..#........#......... +.......#...#............#..........................................................#..............................#.............#. +..#................#....................#.#...#.................................#.........#....................#.........#........ +...................................#..#..........................#........#.....#.........................................#....... +...........##.................##............#................#...#..................................##...#........................ +..............#..................#....................................................................#........................... +.#...#....#..........##..................................#.............................................................#......#... +......#.....#................................#...............................................................#......#............. +..........#.............................................#............................##....#.........................#............ +..........#......#.................#...#........#...........#.#..........#........#....................................#.......... +.#..................#.#............................#.......................#.........#.......#.................................... +.....................#....#..................#..................................................##..#................#............ +......#......................#..#..............##.......#..................................................#..............#....... +..........................................................................#........#...#..............#.................#......... +....................##....#.......................#........#...................#.............##.......#........................... +.........#............#..................................................................................#........................ +........................................................................#................#........................#.............#. +............#.......#.................................#............#.............................................................. +.#.............#...............................................#....#.........#....#..................................#.#......... +.......................................#.....................................................................................#.... +........#........................................#...............#........................................................#....... +...............................#.........#........#...........#.......................................................#.........#. +.....................##...#....................................................#............#..................#..#..........##... +................#..........#...#............................................................#...##................................ +...................#........#...............................#.................#.........#......................................... +.................................................................................................................#................ +............................#......................................................#.....................................#......#. +......#................#............................#............................................................................. +..#...........#.....#.........#....#................#............................................#................................ +..................................#.......#.......#...........#.........#................#.....................................#.# +...............................................#............................................#.#.....................#.......##.... +..............................#...............#..............................#...#...#.............................#...........#.. +.......#...........#..........................................................#........#..#.........#.....#.....#...#............. +......................................................#.#............#.........#.#...........#........##.....#.................... +...........................................................................#..........#.........................#.........#....... +....#.........................#.....................................................................................#...#......#.. +...#.......#............................................................#...............................#....................##.#. +.................................#.....#................................................................#......................... +.....................................#........................#....................................#.............................. +..............#.......................................................................#.......#....................#..........#... +................................#..........................................#.................................#.....#.............. +...........#.......................................................#...............#.....................#..............#......#.. +..................................#..........................#.................................................................#.. +.............##......#....................................................#......#...............................................# +#.......#...................#..........#...................#.................#..................................................#. +.......#..........#.....##...........#.......................#............#..............................#.............#.......... +.............#...................##......................................................................................#........ +#.............................#...................................................................#............................... +.#...................................#....##..................................................................................#... +.................................#.#..................#..#..#..........................#.......#.................................. +..................#....#....#....................................................#....................................#........#.. +..........................................#...#.....#...........#.........#............#............#..........................#.. +...............................................................................#..............................................#... +.............................#......#....................#....#....................................................#.#.#.......... +................##......#........#........#...........#..........................#......................#..............#.......... +.....................#.....#..#.............................................................................................#..... +.......#..#...........#................................................................#.......................................... +#.......................................................................................................#...#..................... +.#..........................................................#.....................................................#............... +#.....#...#...............#..#......#........#..#...................#...................#.................#....................... +..#.................................................................................#.#......................#.................... +.......................#.............#..........#.......................#...................................#..................... +.................#......#......#...#.#.#.............................#.......................................................#.... +..#...........................................................................................................#................... +.................................#..................#.....#........#.........................................................#.... +.............#......................#...............................................#...#.............................#...#....#.. +.#............................#..........................#.#...........................#..........................#.#............. +.............................#.........#.................##............................#...................#...................... +.........#......#....#.........#..............................................................................................#..# +.....................#......#.................#..#..........................#..................#..............................#.#. +........................................................#......................#............#.......#......#...................... +........................................#....##.#........#......#..............................#..........#..........#.##......... +.............#.......#.......#......................#..........................................#.........#............#...#...#.#. +..................#........................................................#...............................#.....#.....#.......... +.........#.................#........#..............^..............#.................##......#...................................#. +....#...................#.#.................................#...#.................................................#............... +.................#.............................................#................#.....#......#.......#...................#........ +...............#...............................................................................#..#...............#............... +........................................##..........................................................................##............ +............................................#...................#........#...#...#....#.........#...................#............. +.....#...#...................#....................................#......#...........#...............#............................ +........................#..................#.....................#.................................................#......##...... +...#.................#....................#..................................#.#.........................#.......#................ +....................#............#..................................#...#......#....................#...............#............. +.....#...............#.......###........#............#......................#.......#....#........................................ +....................#........#...........#..................................#..#................#.............#................... +.....#..................#...................#.#..................................#.......#..#..................................... +..#..#.......#........................................#.................................................................#......... +.......#............#...#....#.......................#.#...................................#.........................#..#.#....... +..............#......................................................................................................#............ +............#....................#........................#.........#...#......................................................... +...................................#..............................#.........................................................#..... +......##...........#.#........................#.........................................................................#......... +..........................#.....................................................#..............................#..............#... +.........................#...#.....................#..#................#................................................#....#.... +...#.................................#...#.......#...............................................................#................ +..................................#....................................#...........................##..#.#.....................#.. +.#.............#.....#...............................................................#...............#...............#..#......... +.........#.............................#........................#.............#......................#........#............#...... +...................................#..#...#.....................................#................................................. +........................................##..........#..............................................#.....................#........ +......#...........................................#....#.....................#...................#..#.........#................... +.#....#.....#........##..................................#......#................................................#....#..##....... +..........#....................................................................##.........................#...........#........... +...........#.#......#.......#..................#............#...............................................#...#..............#.. +.................................................................#.......#.........#.....#........#....##....#..........#......... +.............................#......................................................#.....#...#...#....#...........#..........#.#. +.....................#........#.........................#......................................#................#...............#. +.........................................................................#...................................................#.... +..............#...#..#................#....................................#.......#....#.#...............#.........#............. +.......#......#.#............................................#.......#............................................................ +##......................................................#..#.................................##...................#..#...........# +...........#.#...#........#............#..............................#........................#.........................#..#..... +................................................##.........................................#.................#.................... +............................#....................................................#...#........................#.#......#........#. \ No newline at end of file diff --git a/src/2024/day6/sample.txt b/src/2024/day6/sample.txt new file mode 100644 index 0000000..b60e466 --- /dev/null +++ b/src/2024/day6/sample.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file diff --git a/src/2024/day7/README.md b/src/2024/day7/README.md new file mode 100644 index 0000000..dd42bc2 --- /dev/null +++ b/src/2024/day7/README.md @@ -0,0 +1,48 @@ +--- Day 7: Bridge Repair --- +The Historians take you to a familiar rope bridge over a river in the middle of a jungle. The Chief isn't on this side of the bridge, though; maybe he's on the other side? + +When you go to cross the bridge, you notice a group of engineers trying to repair it. (Apparently, it breaks pretty frequently.) You won't be able to cross until it's fixed. + +You ask how long it'll take; the engineers tell you that it only needs final calibrations, but some young elephants were playing nearby and stole all the operators from their calibration equations! They could finish the calibrations if only someone could determine which test values could possibly be produced by placing any combination of operators into their calibration equations (your puzzle input). + +For example: + +190: 10 19 +3267: 81 40 27 +83: 17 5 +156: 15 6 +7290: 6 8 6 15 +161011: 16 10 13 +192: 17 8 14 +21037: 9 7 18 13 +292: 11 6 16 20 +Each line represents a single equation. The test value appears before the colon on each line; it is your job to determine whether the remaining numbers can be combined with operators to produce the test value. + +Operators are always evaluated left-to-right, not according to precedence rules. Furthermore, numbers in the equations cannot be rearranged. Glancing into the jungle, you can see elephants holding two different types of operators: add (+) and multiply (*). + +Only three of the above equations can be made true by inserting operators: + +190: 10 19 has only one position that accepts an operator: between 10 and 19. Choosing + would give 29, but choosing * would give the test value (10 * 19 = 190). +3267: 81 40 27 has two positions for operators. Of the four possible configurations of the operators, two cause the right side to match the test value: 81 + 40 * 27 and 81 * 40 + 27 both equal 3267 (when evaluated left-to-right)! +292: 11 6 16 20 can be solved in exactly one way: 11 + 6 * 16 + 20. +The engineers just need the total calibration result, which is the sum of the test values from just the equations that could possibly be true. In the above example, the sum of the test values for the three equations listed above is 3749. + +Determine which equations could possibly be true. What is their total calibration result? + +Your puzzle answer was 20281182715321. + +The first half of this puzzle is complete! It provides one gold star: * + +--- Part Two --- +The engineers seem concerned; the total calibration result you gave them is nowhere close to being within safety tolerances. Just then, you spot your mistake: some well-hidden elephants are holding a third type of operator. + +The concatenation operator (||) combines the digits from its left and right inputs into a single number. For example, 12 || 345 would become 12345. All operators are still evaluated left-to-right. + +Now, apart from the three equations that could be made true using only addition and multiplication, the above example has three more equations that can be made true by inserting operators: + +156: 15 6 can be made true through a single concatenation: 15 || 6 = 156. +7290: 6 8 6 15 can be made true using 6 * 8 || 6 * 15. +192: 17 8 14 can be made true using 17 || 8 + 14. +Adding up all six test values (the three that could be made before using only + and * plus the new three that can now be made by also using ||) produces the new total calibration result of 11387. + +Using your new knowledge of elephant hiding spots, determine which equations could possibly be true. What is their total calibration result? \ No newline at end of file diff --git a/src/2024/day7/part1.ts b/src/2024/day7/part1.ts new file mode 100644 index 0000000..b03dd19 --- /dev/null +++ b/src/2024/day7/part1.ts @@ -0,0 +1,68 @@ +import fs from 'fs'; + +// Read and parse input +function parseInput(input: string): Array<[number, number[]]> { + return input.trim().split('\n').map(line => { + const [testValue, numbers] = line.split(': '); + return [ + parseInt(testValue), + numbers.split(' ').map(n => parseInt(n)) + ]; + }); +} + +// Generate all possible operator combinations +function* generateOperators(length: number): Generator { + const operators = ['+', '*']; + const total = Math.pow(operators.length, length); + + for (let i = 0; i < total; i++) { + const combination: string[] = []; + let num = i; + + for (let j = 0; j < length; j++) { + combination.push(operators[num % operators.length]); + num = Math.floor(num / operators.length); + } + + yield combination; + } +} + +// Evaluate expression from left to right +function evaluate(numbers: number[], operators: string[]): number { + let result = numbers[0]; + + for (let i = 0; i < operators.length; i++) { + const nextNum = numbers[i + 1]; + if (operators[i] === '+') { + result += nextNum; + } else { + result *= nextNum; + } + } + + return result; +} + +function part1(input: string): number { + const equations = parseInput(input); + let sum = 0; + + for (const [testValue, numbers] of equations) { + const operatorCount = numbers.length - 1; + + // Try all possible operator combinations + for (const operators of generateOperators(operatorCount)) { + if (evaluate(numbers, operators) === testValue) { + sum += testValue; + break; // Found a valid combination, move to next equation + } + } + } + + return sum; +} + +const input = fs.readFileSync('part1.txt', 'utf8'); +console.log(part1(input)); \ No newline at end of file diff --git a/src/2024/day7/part1.txt b/src/2024/day7/part1.txt new file mode 100644 index 0000000..5a4b936 --- /dev/null +++ b/src/2024/day7/part1.txt @@ -0,0 +1,850 @@ +234815: 36 8 815 40 55 +155271616759: 1 7 652 2 7 421 4 58 3 9 +8580336: 3 6 35 3 6 539 2 59 1 3 3 +189612: 65 20 4 236 9 61 7 508 +17087913891: 854 2 2 5 910 22 367 1 +4724307587818: 82 651 885 4 1 8 7 8 18 +25057637: 630 6 396 6 1 637 +7872602815: 9 26 602 6 4 543 354 +20319060825485: 3 47 1 8 3 6 712 52 7 9 2 +6937026: 78 5 4 9 33 2 6 92 2 1 3 6 +16490320: 1 290 2 97 5 7 176 +81202174: 5 76 5 6 5 36 20 1 94 80 +531135610: 111 491 16 25 55 613 +527172: 3 955 854 462 72 +2799: 8 26 71 9 +1558950: 134 7 4 5 5 71 27 949 +8043570: 4 72 22 8 279 +43643248: 433 2 86 56 513 727 8 +583307: 59 2 9 23 11 231 76 +10272379: 876 1 514 739 27 9 +3254637331: 1 3 32 9 35 6 6 7 5 1 5 42 +892457: 89 24 20 6 31 +133629334: 4 8 287 22 97 +5784660503: 310 311 8 78 6 782 3 +4400201: 65 236 957 270 11 +1091316: 3 421 319 8 9 +909346771: 606 3 5 1 9 3 3 5 5 90 6 +73663: 73 6 63 +507442: 481 26 418 22 4 +246922771: 96 83 500 24 99 51 +30421378: 33 8 9 13 33 9 37 +995887199: 238 5 48 55 317 +1374726: 1 9 701 77 3 1 582 3 39 +5993001673: 7 1 859 1 59 28 9 2 115 +32111: 6 37 5 25 8 +15749: 48 9 6 94 46 +163723896: 723 251 525 9 13 4 42 +14156664983203: 9 25 50 937 721 2 67 +173811792: 407 42 4 82 31 +1447453: 5 8 462 241 7 +29495757285: 7 373 488 449 4 9 285 +4212640331532: 9 320 904 6 6 307 5 +499870673: 49 98 70 667 7 +1604946219: 58 75 1 895 721 220 +765912480: 2 75 6 987 47 7 +86058621: 956 12 8 69 9 +41337379: 4 64 979 91 33 3 47 +9744108902: 73 1 819 132 902 +1530162: 98 67 5 155 +198731: 220 6 44 3 9 5 4 6 5 8 3 8 +84748076541: 789 7 5 7 73 5 7 3 994 3 +2529151157: 7 705 97 376 9 317 +2228: 66 7 88 7 4 +2651040266: 807 45 612 8 73 5 +1064798: 5 7 52 7 85 +37595457: 264 734 8 2 14 97 3 1 +263091920: 3 98 80 91 284 +4343335925: 40 274 2 693 998 7 +5450841: 840 721 9 1 6 75 +643963238282: 2 6 33 1 885 783 1 2 91 +171085643: 223 14 548 36 7 +45690309: 27 821 2 687 3 +5564104817: 7 643 728 813 4 +86673: 89 7 69 2 701 +87323: 32 5 3 7 64 29 6 8 3 +19873: 380 7 106 7 508 +1548: 5 8 93 740 656 +762: 9 1 1 5 712 +307671: 9 815 8 881 30 41 48 +2932551: 42 54 431 3 27 +23646: 5 91 4 9 +1653697595: 7 54 5 40 6 9 9 72 7 8 9 3 +17216951640: 538 40 8 52 898 1 642 +4346316390: 78 753 74 387 3 +527476015: 6 2 9 433 23 4 7 52 8 15 +7028573625: 481 902 8 9 3 4 34 5 45 +93857: 64 6 9 5 98 8 62 9 26 3 8 +45372094843: 8 95 597 948 40 +13074553: 81 571 79 66 6 2 43 5 3 +3472508224: 4 9 60 719 23 3 677 7 +1123: 88 5 5 8 339 +1376356816: 483 548 65 2 8 +314881054465: 55 465 65 66 64 886 1 +23746140: 6 5 1 1 9 2 8 524 8 4 9 3 +724670: 1 8 1 3 700 3 2 6 6 2 46 4 +169538: 31 94 44 534 4 +10188004277: 456 26 943 796 2 77 +53849072361: 8 42 71 3 9 6 7 462 2 7 7 +102705: 7 168 5 58 915 +6862448: 5 680 239 9 996 4 4 +56087: 4 821 71 8 61 891 52 +188280018: 260 57 93 72 18 +3521099170: 2 48 7 69 2 5 54 54 4 6 +11299837: 184 4 231 1 6 591 6 +574320039: 3 9 903 53 5 69 634 9 +48049: 6 249 32 240 1 +9770796: 48 836 175 2 386 4 63 +190310120618: 9 70 49 65 80 8 77 +90545520: 692 1 6 23 94 6 7 3 4 5 3 +473: 427 5 7 1 33 4 +283849008: 834 414 4 4 428 2 17 8 +1420968: 734 25 78 5 6 4 +11864163: 23 8 91 77 495 +39421369: 16 7 5 114 9 4 387 107 +1822914130: 870 65 62 24 322 +290186191: 50 1 34 4 90 21 9 413 +14426: 7 9 9 18 10 +3992: 5 790 3 37 2 +470473510: 5 512 5 2 42 875 1 4 3 3 +1933330256: 263 4 9 7 573 757 2 56 +391837988: 484 8 5 6 73 3 5 3 6 8 1 +41215: 3 3 55 31 4 281 7 7 +932574: 1 690 4 6 5 52 56 4 674 +40262525: 246 7 227 103 445 +19826832189: 30 27 6 5 91 70 8 4 189 +2847924866: 50 7 816 99 7 82 3 +14027830830: 50 783 74 9 4 2 807 6 +56176536: 656 2 728 1 9 6 7 9 3 5 6 +441309276: 696 5 704 7 756 1 9 +297893: 2 322 461 932 77 +53805752: 4 1 6 8 6 4 1 576 8 7 19 8 +31530: 6 1 6 2 7 7 8 35 948 9 2 1 +7174489: 76 944 89 +153772299459: 3 9 9 4 5 4 1 90 86 9 77 9 +7038084958: 4 840 30 521 2 6 8 9 6 2 +27975: 5 70 6 62 75 +1157576: 7 31 5 846 9 3 9 5 +335534490: 243 863 2 8 90 +23443: 5 8 34 26 4 +630040988: 2 70 7 53 866 8 +900135925: 899 417 2 716 92 5 +2834904: 78 49 93 1 1 24 +37470200672: 96 1 6 74 643 9 9 6 67 5 +1736124: 8 894 9 9 6 6 2 6 39 52 +7869680: 9 15 44 4 805 +447156020: 38 336 5 5 34 1 4 3 7 7 +14761454: 1 91 3 13 25 1 1 59 7 7 +3227583252: 76 847 1 22 42 6 5 +7402326818339: 78 1 937 268 1 8 337 2 +180366: 9 7 7 66 7 6 3 24 2 77 1 +103360506330: 40 706 575 5 366 +259: 50 5 4 6 1 +59315037424: 977 44 78 8 8 778 +111540286525988: 8 401 9 46 79 3 74 39 9 +168954: 3 56 9 5 4 +4697629: 6 33 20 8 3 7 3 65 5 8 9 7 +21028140: 3 6 9 6 6 41 850 5 9 3 7 +2137182330: 77 4 9 660 221 81 27 +1270333614966: 9 7 417 5 7 2 2 163 1 8 3 +1518169539: 581 937 16 953 9 +1226257202: 12 262 57 19 7 2 +51632376: 5 509 2 2 7 5 47 8 9 2 1 5 +13761434: 8 2 839 7 25 46 8 4 2 2 +36652742: 3 2 854 976 8 79 6 2 +48788351: 84 702 12 4 12 +327773952: 4 316 56 4 648 +399669886: 397 2 669 886 +6890073: 911 7 6 3 5 8 9 4 5 9 20 4 +113214: 7 98 55 8 3 +787574304: 787 574 263 4 37 +114516889: 426 22 76 6 831 424 +490344553: 7 3 709 95 91 3 7 8 65 8 +11283196: 4 24 15 50 91 5 95 2 6 +392017032: 33 5 2 3 5 2 7 93 9 5 85 7 +7736727: 4 73 36 373 351 +540982: 540 1 982 +79329: 93 853 1 +763474860: 9 42 6 822 4 5 2 9 4 6 6 +54: 8 5 6 1 8 +41329549210760: 738 56 8 7 49 210 758 +11664: 3 1 8 3 48 +2169144: 9 87 217 9 2 7 3 2 40 2 +149623: 95 75 21 +220739980: 4 69 480 3 996 3 2 76 4 +127746: 2 3 24 65 709 6 +31250248161: 5 1 5 5 82 4 5 6 5 74 6 4 +2287761: 1 462 165 30 726 135 +996: 2 902 95 +1422687066: 986 6 71 677 3 +157354426236: 7 253 1 74 688 3 2 3 9 4 +610084: 1 9 62 94 9 90 87 1 5 61 +80898918: 6 7 4 69 3 9 8 59 3 2 +10388417: 274 61 7 4 82 37 +144427107194: 8 29 5 81 865 549 7 +61515: 578 40 99 38 297 +34146279: 379 401 2 1 9 +9728167914: 8 9 382 1 2 28 4 6 1 5 8 +104134517: 5 4 9 752 65 6 35 29 +17556093509: 7 6 3 4 2 73 5 67 5 2 404 +138: 1 5 23 +147849564004: 75 4 84 9 9 6 3 9 3 778 4 +8847: 65 8 225 9 8 +5661042: 1 942 5 6 37 4 +3207509550: 858 63 66 6 14 566 +722647: 7 57 8 14 12 47 +13280839264: 4 649 53 39 429 494 4 +2933: 9 1 6 8 3 +2524032: 2 44 77 1 9 829 2 6 2 6 8 +801191452: 327 35 581 2 35 782 +20879018: 718 6 9 3 1 8 4 2 7 6 46 6 +178: 9 65 6 8 93 +3374254: 73 2 5 3 8 4 7 641 7 26 4 +15093: 958 13 94 96 13 +597125887: 3 9 334 15 2 5 9 48 3 6 8 +4661394: 465 63 7 8 32 3 96 +835995349: 3 7 1 2 864 7 8 5 771 4 8 +26987199: 5 526 766 2 9 8 5 6 79 9 +520177: 3 304 6 6 5 4 8 3 5 3 7 91 +20670045: 212 975 37 5 +3424427877418: 8 7 9 74 6 7 980 6 4 9 95 +4514904676: 3 145 728 776 9 6 93 7 +5168: 7 71 5 9 936 14 354 +1014610: 159 8 2 410 91 4 482 +118433: 4 67 7 7 87 4 3 3 4 6 76 7 +21913289376: 9 1 29 3 9 3 371 9 5 2 6 2 +1998: 8 4 72 2 55 901 +56185682: 280 8 9 2 21 4 735 2 82 +3523211998: 216 94 246 50 924 +15800687: 513 11 7 7 4 6 +211665306879: 529 1 4 62 3 24 6 687 9 +124169195: 5 2 3 9 4 2 6 1 6 8 1 192 +6415: 806 770 6 9 4 37 14 +63754677: 2 64 965 64 677 +241396: 264 2 353 274 +38766796: 6 985 370 3 1 3 5 +1375304961: 55 710 991 2 93 907 +890426953: 10 118 477 88 977 +16774347156: 223 503 29 647 4 +1705177: 7 8 988 6 438 22 +1217609: 25 487 5 5 8 +350630: 24 1 10 45 14 +12594: 203 6 25 16 6 +29996424973: 3 2 9 284 3 7 249 62 6 5 +42762313: 4 2 10 38 4 30 87 74 +1957877082: 42 6 91 567 706 +1535: 1 1 2 27 7 5 +6003072855: 2 75 9 4 772 90 8 50 5 +42639052854: 47 405 20 8 7 8 4 8 5 3 1 +62301: 1 3 692 7 3 +216116392: 41 8 147 8 6 1 4 7 9 5 +567: 5 55 507 +426745: 123 29 701 49 5 +9849: 9 1 8 9 4 937 +1435267: 88 4 50 78 6 2 3 4 1 4 7 +122151: 11 1 89 32 51 +18989349: 7 3 8 95 68 3 58 1 8 3 3 +57454838427: 820 783 395 9 7 714 +1831360: 6 3 31 288 72 +55892870: 9 46 89 2 870 +723110784: 64 3 1 188 2 814 781 +578540160: 1 96 475 1 54 8 8 8 198 +965197554: 6 959 19 75 54 +5076765233: 5 640 841 9 2 5 9 4 6 +358902: 75 1 281 2 90 2 +434: 8 5 7 9 15 +511: 1 7 3 7 +18974676: 4 6 41 34 9 9 2 8 722 1 7 +9740211: 304 9 89 6 6 4 3 +120617043418: 6 5 768 1 8 629 5 570 6 +1792345: 255 1 7 3 41 1 3 +10447: 38 8 225 97 1 +7032854752: 59 596 2 2 5 2 8 9 4 2 2 +32524773: 114 28 60 1 3 773 +105636707: 99 957 3 643 4 273 +26939: 49 6 4 178 483 7 476 7 +18925509213: 4 626 8 5 5 4 61 3 1 1 2 5 +53911: 9 657 8 655 9 8 8 48 +16540603: 8 15 8 8 99 612 7 5 2 72 +635047284121: 9 588 2 1 1 6 1 67 412 1 +3498179460048: 6 420 770 6 876 98 21 +55940162: 932 27 7 59 6 10 +2673431161: 220 1 6 9 9 1 6 2 1 2 56 9 +992: 9 86 6 +34751772: 5 78 21 1 3 90 5 4 7 6 9 6 +11754337: 532 486 2 8 923 8 22 6 +399393: 414 104 403 918 9 1 +21013231642: 337 29 623 64 642 +3335004: 371 16 1 8 9 1 14 5 5 8 4 +3363268: 267 407 5 998 8 +5822561565: 5 6 7 439 6 46 5 8 1 85 9 +242218567: 225 10 1 7 65 1 85 6 7 +3936275: 7 1 792 3 2 7 +11537194338144: 7 350 404 14 872 2 9 +1719584: 3 13 679 516 83 4 +1314269: 31 51 61 159 769 +647: 48 3 3 31 103 +341416031: 7 5 2 11 1 92 3 9 6 9 62 +1452753: 21 79 6 50 97 96 81 +6804: 3 92 4 8 1 59 16 1 5 9 4 +2973255928: 1 6 5 8 45 2 648 5 6 85 5 +145350: 1 1 741 3 3 8 5 599 6 51 +6276679: 69 74 6 5 15 4 +58725315257: 7 6 73 7 5 9 6 7 933 4 7 7 +198748813: 29 7 74 1 5 298 826 8 +4224050454: 3 2 3 4 8 61 2 1 899 9 7 +545644: 4 26 603 771 548 +2037239837: 794 8 453 354 2 989 +99372320: 75 828 4 30 8 5 8 +156820: 736 2 94 2 20 +64309027437: 12 95 8 375 49 560 +18351: 594 43 79 303 2 9 8 1 +879044880: 54 4 859 63 657 3 8 35 +13148172: 4 2 7 531 1 1 77 7 3 7 9 9 +375445666: 75 5 371 74 16 9 495 +33108201: 45 34 93 44 73 +2383405: 611 39 6 36 6 2 6 +338437376: 87 389 7 3 73 +377102966: 9 2 960 4 8 3 6 6 8 7 79 +5207: 8 1 483 856 4 +421240244: 6 6 8 62 6 9 9 7 9 179 7 1 +6833185492: 96 12 12 632 8 6 92 +8995831: 6 47 3 33 630 137 +64063: 78 2 8 63 +189629583557: 1 838 58 2 1 958 355 7 +667400891: 71 95 704 139 7 4 891 +535715: 707 151 314 72 5 +35859722: 3 8 3 6 9 436 1 3 3 7 464 +17619000: 4 9 6 518 7 7 7 5 9 5 839 +1384137216: 7 6 70 1 2 3 64 329 4 6 1 +153820: 5 53 3 955 20 +166606210: 85 196 586 35 1 +5116097: 56 216 91 28 413 +625625: 1 3 992 7 6 6 121 6 1 5 +1098088311: 5 3 23 71 604 237 +14318248: 1 18 8 61 9 99 967 +12041646: 90 475 818 99 87 933 +27827184050: 1 902 474 6 308 4 9 +22218206396758: 728 4 657 835 305 7 1 +29293466: 82 166 4 538 9 427 3 3 +2043: 43 4 3 3 4 1 250 3 1 1 3 +552330: 70 408 87 423 19 +2564011611: 87 2 9 25 34 32 8 723 +38646: 37 4 48 597 8 9 584 +7137998: 8 5 56 3 417 17 82 1 8 +44663: 1 3 2 2 661 2 +2277256255: 190 22 7 8 7 256 247 8 +1003001: 9 2 8 2 969 3 291 5 500 +850006705952: 389 8 66 6 51 5 7 855 5 +14221542: 505 88 32 8 734 +24228423565: 42 506 57 35 68 +246743: 96 239 7 7 707 +3160602: 61 5 9 39 6 5 70 8 6 3 3 2 +43848294: 8 597 23 40 7 57 6 21 7 +862506240: 8 776 819 8 120 +4969: 16 289 6 270 69 +1433578: 4 9 7 1 6 822 4 6 6 +556935795: 370 92 1 35 98 7 3 9 5 +438347796: 4 383 254 223 96 +101329722098: 30 4 298 966 6 20 98 +1077664776293: 58 9 9 88 9 2 388 1 48 2 +89178: 12 9 68 501 2 +13972204: 12 17 55 876 1 4 +169687678: 459 7 69 88 808 6 +339338795540: 5 67 836 72 83 8 2 9 4 1 +70824884424: 8 4 8 4 1 2 1 19 276 1 3 8 +135972749: 520 871 3 8 959 49 +407463489597: 407 463 4 89 599 +14318725: 894 53 9 3 10 8 7 4 2 79 +545272096067: 9 810 543 66 366 798 +583: 2 5 7 160 59 315 +257321712: 1 56 566 8 997 +1725165348: 9 32 8 1 6 594 53 7 4 3 5 +1438277: 7 3 3 4 9 9 69 2 9 1 1 77 +3771839519: 6 4 16 92 9 1 9 71 800 +1758340: 9 39 667 1 5 +5275488825: 50 71 978 6 361 2 4 25 +26053872450: 6 691 89 7 2 6 447 +9450: 6 5 3 7 84 5 6 +95311557: 8 27 7 54 34 63 2 9 2 3 +804519: 18 41 9 359 3 +66106: 3 2 8 5 37 9 8 9 936 65 1 +68453598: 4 5 64 90 93 6 44 246 +951596: 2 7 9 62 85 7 6 297 2 4 4 +98538336263: 438 6 5 9 832 77 8 3 8 +911484: 3 303 2 48 4 +1640: 3 5 46 9 74 1 +103459686: 1 2 661 6 51 3 9 1 8 3 2 6 +115965921: 5 85 1 4 42 6 2 97 6 3 9 3 +386223718: 8 378 22 3 715 4 +19649: 7 8 6 4 97 8 761 +5027443270: 26 32 798 90 1 745 46 +1920817: 10 48 4 817 +23557514: 7 3 76 60 96 8 33 +127832584428: 81 6 29 907 4 428 +701077: 308 2 5 8 222 +5414824290: 2 2 82 941 43 30 +345251177859: 639 354 54 1 2 5 859 +18396970: 42 2 16 7 6 968 +184177: 3 2 365 29 83 +508375: 993 232 415 +82711: 7 697 552 3 4 185 +5249836800: 4 488 8 6 3 2 39 475 2 1 +275554044: 13 9 9 6 4 6 693 4 47 18 +624: 3 6 51 9 3 +576516693: 7 88 260 8 2 1 4 9 9 9 3 1 +84680467: 53 8 7 98 75 202 4 4 +739388: 671 68 33 5 53 +819857339208: 9 8 3 7 83 1 2 566 8 53 8 +9710: 4 967 3 +66557741426: 976 458 36 7 8 18 517 +108308520: 2 3 4 5 86 2 678 32 8 5 8 +204651: 3 65 9 1 97 454 +2508: 3 4 18 3 5 76 +11483: 12 944 96 20 39 +2455900: 9 156 646 2 599 +1878400: 24 9 9 6 8 936 1 4 8 5 1 4 +272387: 894 9 7 299 297 +99168: 3 4 2 30 956 210 +1572619563: 4 90 8 1 8 316 6 24 3 9 +1293: 10 5 6 787 +19662: 9 4 7 455 97 +9368735930490: 41 815 4 74 492 77 90 +17389031: 3 2 43 407 16 2 7 +46884: 726 5 2 554 6 +522: 46 351 5 59 61 +379558743: 8 8 456 67 12 70 7 46 +13246734: 79 5 219 469 331 3 31 +6531950815: 258 4 4 803 3 11 5 9 9 7 +10632348: 9 7 6 9 59 7 77 6 4 3 81 +1906238880: 33 40 5 538 468 120 +115357: 4 6 753 782 7 +527211: 31 6 2 1 8 8 4 7 582 8 1 9 +1645989082: 636 5 862 3 82 +2396: 572 4 4 61 31 +568779: 74 486 2 85 82 +643028: 20 38 56 564 2 66 +1743917: 4 612 89 8 941 +6515470: 33 1 7 5 8 1 1 496 8 1 6 8 +6703746465: 26 5 1 6 111 9 9 1 7 7 7 6 +5540262: 708 94 32 949 7 +277206086: 59 4 8 8 9 327 5 89 7 3 1 +980162: 31 2 69 29 4 26 226 +3542490: 328 4 27 8 8 2 +652680: 9 67 6 1 109 84 +3471: 8 7 59 64 96 7 +42356: 846 50 18 5 33 +19653579138: 42 8 194 2 603 5 4 9 1 4 +8832693: 120 763 26 93 +4956: 642 3 2 6 963 24 38 73 +18061244: 50 17 9 4 41 +3844233: 1 9 375 1 139 7 8 8 9 9 +10332: 6 4 8 5 708 2 +56814705894: 590 4 7 278 1 4 8 9 8 3 +56961: 31 8 3 4 8 3 6 8 440 3 7 4 +4349305992772: 2 17 465 2 5 992 77 2 1 +29479: 1 4 7 245 2 8 6 5 9 +786181051: 751 34 526 4 476 410 +165789: 1 11 63 34 507 3 +118010445: 6 6 411 66 5 4 3 5 6 1 2 5 +8316103: 87 3 924 48 53 +73226602495: 70 6 73 94 50 3 99 49 5 +2011100: 783 4 2 321 356 +146136086: 940 2 777 600 86 +19534951825: 7 18 5 1 8 89 998 7 9 24 +100230: 6 94 2 2 8 +3663504: 6 6 4 2 9 54 7 6 6 206 78 +7682131090: 2 12 61 6 2 8 9 5 66 37 +213463262: 843 3 3 3 4 9 5 7 5 9 46 6 +72035028: 2 26 987 32 98 8 117 6 +1069: 212 6 1 849 1 +50223360920: 928 5 33 328 920 +18815: 13 133 96 4 558 +95388581922: 5 1 76 5 2 9 6 5 5 7 585 +2270164: 2 7 9 293 52 +5441524: 3 2 85 1 61 564 34 2 6 +1076: 61 3 3 4 4 4 +2282157: 319 7 7 1 54 +1254937608: 5 6 699 4 724 4 8 1 5 4 2 +9735: 8 2 243 2 4 7 +79989614: 7 99 896 1 3 +527207309: 673 45 3 87 980 54 2 +3223380: 9 53 7 14 18 465 +99930: 4 7 9 866 64 +5470080: 85 454 13 787 80 +564252: 24 2 191 26 49 +5871813984: 85 6 91 6 189 8 13 98 4 +80735566: 77 286 703 52 757 +20499203: 8 298 87 668 287 +12093339: 2 4 157 29 68 5 +16792883: 1 2 836 604 2 73 7 6 +977491676593: 9 2 1 5 74 916 76 593 +12702104: 1 270 210 4 1 +34581205: 836 5 3 4 4 3 3 6 9 57 49 +10643489: 435 70 559 347 9 7 +406030588798: 8 5 1 3 68 3 698 2 7 7 9 +100340500685: 96 237 948 441 5 +43203: 4 300 236 80 3 +322535925: 616 68 27 81 95 +119242816: 18 9 719 33 39 64 1 64 +2776175: 58 673 67 71 4 +7194: 7 133 59 +701907953: 2 83 4 62 8 51 8 953 +14629123: 11 9 3 18 8 8 2 5 9 3 4 22 +4237: 3 56 7 60 7 +334: 186 62 77 9 +8716: 7 32 222 57 1 +378970: 5 1 5 2 178 211 93 638 +2261424: 44 16 2 1 1 5 4 5 2 678 8 +3700: 415 7 1 33 761 +671175: 22 7 48 83 40 535 +1487916050680: 31 331 2 98 725 675 4 +1514704135: 249 14 381 639 9 1 1 +1715: 232 2 4 432 975 70 +52378: 59 443 20 2 64 +32: 7 1 4 +71295537753: 3 3 86 5 501 31 89 8 99 +34542: 18 327 42 +2832644: 5 57 113 2 70 3 4 5 9 7 4 +546019: 26 1 3 70 17 +663752103951: 75 885 2 103 951 +39361407117: 9 7 7 5 5 9 6 4 4 7 57 115 +1112816: 314 8 443 +608225: 3 37 9 18 5 +10620: 99 5 48 9 7 94 1 +53963240114: 593 91 240 11 2 +77831461: 9 54 7 8 94 5 5 5 7 3 2 1 +280640: 8 4 4 76 2 659 7 3 353 5 +22673927: 249 5 760 89 287 +1774013122: 17 740 131 2 4 +16021618208: 4 326 68 717 26 42 6 8 +130387: 2 5 5 4 6 21 5 2 34 28 75 +48101318: 79 1 168 8 3 3 6 +1541632303: 1 5 5 2 116 581 6 14 82 +154225: 726 79 737 25 +1924560677: 3 8 8 2 78 9 99 8 630 47 +9573007: 327 287 4 34 3 +386463: 4 7 57 6 5 6 6 9 3 5 8 63 +61654284961: 139 9 666 74 961 +7623216066: 4 4 146 6 5 93 843 24 7 +181746: 64 284 520 2 784 +466308168: 20 778 5 1 27 831 +3829539362: 981 933 16 1 13 3 +1289888927: 82 3 6 4 5 8 50 1 55 873 +34260323963010: 9 93 6 352 98 6 5 6 968 +1960810915502: 15 70 29 95 657 690 +331976860905: 331 976 854 6 906 +5380055: 4 890 96 11 +26233385: 2 2 2 2 639 81 4 1 9 85 1 +20269389221: 558 55 9 913 357 221 +2282784237833: 9 8 4 8 1 6 79 2 3 6 1 833 +56888859: 23 1 82 694 712 50 6 +1611982: 3 730 12 366 59 1 2 +1759460: 2 4 8 4 4 3 141 7 2 278 +222805681: 5 906 9 7 1 6 5 7 9 4 4 81 +1624808255: 3 5 15 95 4 3 4 224 19 +14585438833: 3 73 9 74 3 64 24 33 +23412: 2 971 398 7 5 +21366575784883: 7 3 3 6 6 575 78 1 4 88 3 +712636287: 8 95 5 51 937 +1167610: 5 2 417 4 1 10 +1780908850: 6 7 7 4 1 37 4 617 50 +55250: 4 3 1 1 2 5 2 10 4 836 74 +78589: 68 77 3 66 8 437 8 +222410760: 4 5 8 564 8 9 5 14 331 8 +16554988493568: 361 4 441 966 32 841 +1380842766: 17 23 6 1 9 1 66 446 7 +481725: 9 308 539 36 6 90 45 +31247442135: 31 247 409 33 1 36 +1450717: 68 4 267 3 269 +923769: 91 21 483 56 698 +73630440: 32 62 39 8 780 +1529044: 5 729 8 5 418 +245940029: 9 7 72 8 2 347 29 5 5 2 9 +960620: 7 3 8 1 3 1 286 3 50 6 5 7 +82802721: 4 43 8 6 329 52 1 +472539559: 9 450 7 4 5 50 7 49 +452196: 1 1 44 21 95 1 +2383008: 8 74 6 6 58 5 672 6 96 +435601: 50 87 6 2 38 1 3 4 5 7 4 +1892: 33 5 6 8 68 +529904695: 58 9 3 52 7 557 95 786 +155303680: 4 76 114 9 4 4 7 2 5 4 1 4 +6325771: 8 94 9 375 8 25 68 +14435769: 5 3 75 5 233 882 8 4 89 +84250: 337 25 3 +8388299868: 9 21 5 6 9 285 2 7 5 86 8 +5591700: 7 40 48 135 436 +42310630: 861 7 156 6 45 799 20 +3907126: 4 559 5 145 345 +7181817627778: 718 181 762 77 77 +114: 7 1 2 97 6 +5012536329654: 5 808 4 31 87 2 4 9 6 57 +14994272: 9 6 7 2 1 3 1 36 62 14 68 +88: 1 2 29 +37171659: 2 5 1 1 106 258 716 5 9 +125547: 5 7 55 40 149 +516724523: 5 4 87 6 7 52 523 +8547254016: 4 7 120 1 63 68 4 21 68 +6496977509286: 42 8 4 946 17 70 5 3 73 +202045216: 92 6 5 9 415 2 3 9 8 54 8 +2142699050: 4 3 4 2 24 4 6 7 49 3 3 2 +262828: 6 900 4 713 30 8 6 32 7 +56968: 477 623 2 322 4 8 +7933170: 182 161 80 1 27 +284718813594: 823 37 21 935 92 +609005: 71 975 6 80 8 852 5 5 +7664: 4 9 177 8 7 1 5 8 8 4 1 9 +24519673876: 7 2 9 6 4 21 246 3 72 4 +4891: 9 152 6 5 61 +34731871: 6 9 8 9 88 78 4 72 2 7 99 +11940400213: 4 219 6 7 6 3 7 26 8 9 7 7 +177088003: 9 89 925 29 239 +15730032: 8 857 8 222 +48884514: 23 16 5 5 748 13 9 7 7 6 +5006910: 64 88 79 1 1 587 795 +950: 79 3 621 88 7 +1081631943: 5 739 7 6 9 7 2 6 90 3 7 3 +114117: 8 7 89 7 94 17 +2364516: 67 8 1 77 308 +121264948855: 137 910 78 9 977 +49509: 7 1 3 72 5 1 6 6 +831885228070: 31 996 9 1 692 45 35 2 +46568189: 2 291 8 818 9 +569680268: 831 34 537 646 629 +1544813181: 9 461 92 6 6 8 29 607 3 +638078: 637 44 4 633 1 +4399562753007: 5 12 6 726 2 75 300 4 +13364: 47 30 588 363 13 +1064634076: 4 2 5 85 7 3 3 5 3 18 4 2 +639290655837: 7 9 4 1 3 117 331 9 80 8 +672708: 5 87 1 9 20 333 46 2 +1373522976: 4 19 997 97 2 7 2 68 8 +30840121: 3 63 9 89 33 1 583 4 +2560185: 79 5 9 1 7 396 3 79 3 4 +22388150824: 4 5 6 27 319 4 1 753 2 +4314628: 1 6 1 52 16 3 4 2 6 47 2 6 +9774960642281: 701 71 3 327 71 2 281 +2516788936: 459 596 92 99 38 +62838: 57 5 8 3 8 +1718832: 2 96 895 43 1 +559683: 5 1 74 7 5 4 2 1 2 6 9 27 +63466: 37 7 4 48 5 4 260 82 +995672: 86 87 81 79 46 65 2 +193009700410: 5 5 772 969 18 86 11 +167097580: 780 1 42 3 51 5 +1476176: 9 53 392 607 176 +6416866958: 3 558 9 62 33 48 20 +2330944: 23 2 9 960 983 +2411883980: 9 21 223 73 3 114 38 7 +2047338: 2 909 78 580 9 +11510262: 9 62 316 892 26 9 +2643171379: 5 755 90 5 2 7 7 79 +87781: 2 5 9 12 8 15 46 6 4 8 9 +10228522: 6 96 2 852 5 +11702763: 86 81 7 585 691 3 +5144841: 211 1 7 236 41 +6088926: 5 2 709 8 926 +21656183: 7 3 652 4 186 +47638073: 7 9 708 356 3 7 24 970 +29882356081: 3 59 21 4 2 980 1 7 4 6 1 +49010208: 3 631 1 39 346 +1337526446: 47 87 56 994 48 +6239232904: 6 93 2 4 81 90 1 +1438200579: 3 7 5 36 850 3 1 4 4 9 8 +34066: 5 2 5 94 9 5 38 2 +1057: 9 128 3 8 1 410 217 8 3 +676708124: 901 1 2 555 75 5 +33205: 52 7 7 44 8 5 1 +66709: 61 7 5 907 413 85 +2475718: 6 5 14 82 7 7 4 624 6 1 1 +7578683: 748 8 2 16 683 +786430: 21 898 949 27 415 5 +44135460: 9 23 5 6 7 8 5 8 2 1 6 359 +1518946204: 166 6 9 5 4 1 565 3 2 4 2 +1147031: 6 2 7 2 1 8 82 5 291 1 9 2 +206732821: 8 934 52 5 89 61 +36449490: 4 4 96 6 40 4 4 3 7 9 1 20 +228783753138: 7 772 448 9 86 1 35 2 3 +86: 6 50 16 5 9 +1235045568965: 722 3 64 55 8 619 6 43 +712856718: 710 93 192 1 5 711 7 +5968984: 6 2 87 40 8 891 52 38 +517974543: 899 1 2 57 4 8 8 23 3 1 3 +74817: 6 6 8 811 6 +282697802451: 2 7 221 9 6 7 290 4 90 5 +271822863: 3 254 64 243 531 68 3 +13512: 19 23 6 7 11 +448211577679: 9 7 2 786 5 1 4 1 3 8 6 79 +1968308: 98 2 250 580 8 +60939986905: 132 91 6 9 2 2 621 41 +4774983552: 4 99 7 5 32 8 9 18 174 6 +258648: 33 3 887 7 439 +5747901413760: 83 2 5 3 7 2 66 120 874 +398710669: 9 81 2 645 63 49 +4389: 2 55 77 +21212: 4 265 6 2 +6870: 3 3 7 9 8 3 2 77 3 9 4 930 +42565865: 96 301 442 2 87 734 +36842511: 491 75 9 8 509 +165093115: 5 330 9 311 8 +194448: 45 231 9 4 521 5 6 6 8 1 +94388: 94 23 8 56 94 +149952019: 22 1 43 640 18 +5989030: 9 144 8 7 699 +921784868160: 30 3 4 42 490 1 1 32 65 +124045837347: 886 14 58 37 342 5 +140597144622: 28 40 71 80 57 341 6 +13288550: 5 9 6 7 56 3 6 1 7 5 407 2 +489624800: 65 4 887 8 800 +31824: 53 22 47 1 9 1 9 +9761568: 16 2 5 610 15 67 3 +5400210715: 4 46 2 60 2 332 9 9 754 +30660861688: 8 8 7 73 8 5 190 8 22 4 8 +10629: 32 8 6 3 771 +20252735: 441 7 4 509 9 134 +2560350: 6 73 76 38 61 +7806036: 218 6 79 84 67 +360894478: 3 2 37 14 5 7 7 42 8 8 8 6 +27699620: 92 29 3 39 3 18 +48933: 39 9 933 +439188456: 2 6 3 66 4 303 89 6 97 4 +3058662635: 30 582 4 625 6 8 9 61 +514470819: 69 46 8 9 162 74 1 9 +1362: 160 7 239 +8011206990: 84 425 243 762 85 +67314: 7 48 4 3 2 28 +143: 2 7 10 +77330133: 6 45 123 863 4 4 8 9 57 +14652470880098: 8 9 27 3 3 960 785 92 6 +68484217991: 796 48 907 948 263 +8888898: 634 85 14 4 994 +12403276: 317 6 96 4 79 +1545635: 18 7 72 223 553 +66790: 4 3 3 9 506 +22531835: 17 2 686 966 851 +26907: 8 640 261 5 +1810285: 112 2 49 466 79 +268104: 7 8 8 5 7 1 6 751 +42855: 7 13 9 6 21 +445: 44 2 3 +8047: 3 60 4 5 381 2 85 1 +1021: 23 7 6 256 585 +1064: 6 1 1 5 2 74 5 7 1 13 71 6 +402479220: 40 194 53 921 9 1 +589455408: 66 1 4 4 87 2 2 82 1 6 8 +33724689: 3 844 371 586 8 +179340: 8 911 46 20 200 +1210205016: 7 68 7 3 70 1 3 9 48 54 4 +9564: 3 372 5 6 4 3 6 242 4 36 +20285637: 25 5 2 4 2 9 9 81 59 8 93 +94121829: 98 337 3 551 78 5 9 +2922: 15 943 16 3 +16854333: 87 9 4 6 3 607 7 5 32 4 6 +7846808: 8 34 727 2 5 260 5 6 8 +366220: 30 8 93 7 29 56 9 2 998 +232546579763: 5 40 4 78 33 770 44 2 +1364793638489: 5 30 9 1 562 2 41 31 19 +191227442: 1 558 6 6 57 3 6 485 45 +31440: 9 38 1 9 953 3 3 3 9 4 2 1 +563024: 639 8 8 11 1 +968436: 97 1 61 9 9 2 +54483: 883 474 5 4 3 +35696: 92 5 92 1 4 +48802840301: 610 8 2 840 304 +4640985990: 94 2 6 4 4 7 2 8 2 6 6 264 +65759: 14 58 7 8 745 +93478: 93 48 1 +1165: 89 22 9 39 7 +5626467081: 6 35 7 15 68 31 6 9 62 +25705178: 265 97 178 +96427474: 2 4 7 926 6 6 865 4 7 2 2 +95871699683: 5 9 7 11 3 5 88 3 3 96 8 6 +2461274295: 9 1 5 54 82 2 6 77 9 1 2 +75444566: 443 96 281 92 4 5 68 +1731805708: 91 62 989 189 785 +1725: 542 2 3 18 73 +3322698: 55 3 6 4 583 80 4 29 2 +217862649: 2 323 3 90 4 69 61 9 +299653325193: 930 6 92 35 77 5 7 +3245407825: 67 78 361 2 575 31 +51713982: 1 29 1 348 39 35 529 9 +130867: 99 9 4 1 9 42 36 727 +393375: 96 7 5 38 7 5 +9640088670: 7 38 7 549 8 1 4 67 1 5 6 +27977912: 3 14 9 1 8 3 1 1 2 656 9 8 +22015896015: 9 7 6 5 5 3 2 965 7 60 7 8 +59170: 888 6 8 6 78 6 9 +252687: 1 55 349 5 11 676 +2688762662709: 233 56 447 969 461 +91885664071: 7 38 9 2 84 8 8 639 1 70 +70719660: 208 69 284 5 764 33 +6090205: 608 95 50 567 89 +279814: 5 9 659 842 2 76 2 9 96 +13035: 5 6 60 8 165 +8830: 629 98 69 8 864 +164203534: 3 910 6 1 1 6 1 3 3 45 8 2 +1180515208: 3 3 5 12 96 386 91 598 +201379689098: 6 3 693 342 63 50 2 96 +1081964: 7 7 951 81 964 +8349602882: 710 4 2 7 7 3 2 78 9 1 4 9 +106930151: 8 1 193 5 3 9 7 4 7 6 3 +63797700657: 6 82 9 9 21 5 629 8 2 1 6 +19513155835: 272 15 717 8 37 +325472: 1 4 53 8 4 7 +42251: 61 68 8 69 2 +950015391: 1 8 9 5 5 4 999 7 3 6 391 +2488834474356: 8 9 3 6 166 1 31 45 3 6 +9609639416: 901 7 592 63 8 1 418 +452711265134: 8 2 813 94 44 87 4 4 +3163159356: 12 345 949 27 3 +520669356: 5 78 512 7 9 855 17 9 +21850454: 910 50 33 2 22 52 +2735378: 7 421 2 52 460 218 +31544: 624 49 12 69 887 +1785141121: 3 404 263 4 5 317 6 28 +3813649: 55 43 545 895 7 7 9 49 +125764604056: 85 4 2 6 17 4 6 470 866 +5353720042: 6 2 55 1 8 3 1 7 2 888 5 2 +2046728: 1 64 6 208 8 +12731308: 615 115 18 6 799 +75530: 83 1 1 70 13 +5585555520: 445 4 2 8 530 74 +60290: 7 861 5 3 9 +6005643840: 6 565 16 913 3 5 7 5 6 8 +13786986: 8 3 846 7 3 97 278 +541296749: 737 8 39 641 8 83 9 +677: 50 5 514 99 9 +5929: 1 41 35 24 970 +133575: 3 1 8 970 66 552 2 207 +23813165: 25 7 866 61 1 53 5 +321: 26 7 48 40 51 +7572679: 9 1 4 1 4 6 666 601 7 2 7 +21428764753409: 6 3 6 6 566 4 752 5 9 1 2 +800864113: 6 3 8 64 682 3 77 377 4 +581925765: 2 6 169 20 7 869 576 2 +11566767: 5 102 9 836 99 +2124521: 59 5 8 9 29 492 +44315624521: 39 5 4 88 833 24 519 +73836378: 424 347 58 3 +6324: 190 54 9 2 21 12 +41389305103: 643 190 4 99 44 65 +2264807129: 7 4 7 6 5 5 3 98 4 4 9 13 +9834346: 1 7 128 4 66 1 344 +8041812452: 1 44 89 555 6 411 4 52 +359572559043: 2 870 6 87 3 3 5 87 80 3 +619646: 61 889 71 4 3 +30132: 6 3 818 2 2 18 +3867753588: 82 818 955 71 45 395 +42019719768251: 2 95 1 614 4 3 8 9 4 2 5 4 +67142400: 5 538 4 312 20 +4859: 35 78 43 +63062582: 76 975 6 2 585 +66641931: 66 625 7 9 929 +7652445: 19 49 41 7 2 2 365 78 +49162643986: 42 153 8 9 5 3 849 8 8 +8804602981773: 38 662 7 5 298 1 7 73 +4113176424: 892 50 50 2 8 7 329 +1117038228: 2 797 7 4 8 3 8 140 2 86 +13544236: 281 482 17 19 +69460233: 3 1 193 6 465 2 8 8 3 3 1 \ No newline at end of file diff --git a/src/2024/day7/part2.ts b/src/2024/day7/part2.ts new file mode 100644 index 0000000..aa2ef88 --- /dev/null +++ b/src/2024/day7/part2.ts @@ -0,0 +1,80 @@ +import fs from 'fs'; + +type Operator = '+' | '*' | '||'; + +interface Equation { + target: number; + numbers: number[]; +} + +function evaluate(numbers: number[], operators: Operator[]): number { + let result = numbers[0]; + + for (let i = 0; i < operators.length; i++) { + const op = operators[i]; + const next = numbers[i + 1]; + + switch (op) { + case '+': + result += next; + break; + case '*': + result *= next; + break; + case '||': + // Convert both numbers to strings and concatenate, then back to number + result = parseInt(`${result}${next}`); + break; + } + } + + return result; +} + +function generateOperatorCombinations(length: number): Operator[][] { + const operators: Operator[] = ['+', '*', '||']; + const results: Operator[][] = []; + + function generate(current: Operator[], remaining: number) { + if (remaining === 0) { + results.push([...current]); + return; + } + + for (const op of operators) { + generate([...current, op], remaining - 1); + } + } + + generate([], length); + return results; +} + +function parseInput(input: string): Equation[] { + return input.trim().split('\n').map(line => { + const [target, nums] = line.split(': '); + return { + target: parseInt(target), + numbers: nums.split(' ').map(Number) + }; + }); +} + +function canBeSolved(equation: Equation): boolean { + const numOperators = equation.numbers.length - 1; + const combinations = generateOperatorCombinations(numOperators); + + return combinations.some(operators => + evaluate(equation.numbers, operators) === equation.target + ); +} + +function part2(input: string): number { + const equations = parseInput(input); + return equations + .filter(canBeSolved) + .reduce((sum, eq) => sum + eq.target, 0); +} + +const input = fs.readFileSync('part1.txt', 'utf8'); +console.log(part2(input)); \ No newline at end of file