aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-04 00:36:08 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-04 00:36:08 -0500
commitea073be078534d6d7b27babacc29d4b9e4e98879 (patch)
tree36c884c066e6a5a5b23cc50865a7296c30d4b19e
parentbeb9bc443b3e206ea7a15be7be5f2be45317d9d7 (diff)
downloadgleam_aoc-ea073be078534d6d7b27babacc29d4b9e4e98879.tar.gz
gleam_aoc-ea073be078534d6d7b27babacc29d4b9e4e98879.zip
day 4 complete
-rw-r--r--2022/day-04/day-04.ipynb108
1 files changed, 99 insertions, 9 deletions
diff --git a/2022/day-04/day-04.ipynb b/2022/day-04/day-04.ipynb
index ef18f45..44c8980 100644
--- a/2022/day-04/day-04.ipynb
+++ b/2022/day-04/day-04.ipynb
@@ -5,30 +5,120 @@
"metadata": {},
"source": [
"### Advent of Code 2022\n",
- "#### Day 4:\n",
+ "#### Day 4: Camp Cleanup\n",
"\n",
- "**Part 1.**\n",
+ "Each elf in a pair of elves is assigned a range of ID numbers.\n",
"\n",
- "**Part 2.**"
+ "**Part 1.** How many pairs have one elf assigned to a range completely contained by the other's?\n",
+ "\n",
+ "**Part 2.** How many pairs have overlapping assignments?\n",
+ "\n",
+ "Since this problem heavily uses ranges, I'm using `rebellion/base/range` to do the heavy lifting."
]
},
{
"cell_type": "code",
"execution_count": 1,
- "metadata": {},
+ "metadata": {
+ "vscode": {
+ "languageId": "racket"
+ }
+ },
"outputs": [],
"source": [
"#lang iracket/lang #:require rackjure\n",
- "(require advent-of-code)"
+ "(require advent-of-code\n",
+ " relation\n",
+ " rebellion/base/range)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##### Part 1\n",
+ "\n",
+ "All the assignments look like `\"11-22,33-44\"`, so we write a function to extract the numbers from the string with a regex, decompose the values with structural matching, and construct a pair of [`rebellion` ranges](https://docs.racket-lang.org/rebellion/Ranges.html).\n",
+ "\n",
+ "Once we have the two ranges, we can use a predicate that tests if one completely contains the other or vice versa to count the corresponding assignments."
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 2,
+ "metadata": {
+ "vscode": {
+ "languageId": "racket"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "<code>550</code>"
+ ],
+ "text/plain": [
+ "550"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(define assignments (~> (fetch-aoc-input (find-session) 2022 4) string-split))\n",
+ "\n",
+ "(define (parse-range str)\n",
+ " (match str\n",
+ " [(regexp #px\"(\\\\d+)-(\\\\d+),(\\\\d+)-(\\\\d+)\" (list _ a b c d))\n",
+ " (values (closed-range (->number a) (->number b)) (closed-range (->number c) (->number d)))]))\n",
+ "\n",
+ "(define (one-encloses-the-other? str)\n",
+ " (define-values (range1 range2) (parse-range str))\n",
+ " (or (range-encloses? range1 range2) (range-encloses? range2 range1)))\n",
+ "\n",
+ "(count one-encloses-the-other? assignments)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
"metadata": {},
- "outputs": [],
"source": [
- "(fetch-aoc-input (find-session) 2022 4)"
+ "##### Part 2\n",
+ "\n",
+ "The procedure for part 2 is the same, just using the predicate for overlapping ranges instead."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "vscode": {
+ "languageId": "racket"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "<code>931</code>"
+ ],
+ "text/plain": [
+ "931"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(define (one-overlaps-the-other? str)\n",
+ " (define-values (range1 range2) (parse-range str))\n",
+ " (range-overlaps? range1 range2))\n",
+ "\n",
+ "(count one-overlaps-the-other? assignments)\n"
]
}
],
@@ -42,7 +132,7 @@
"codemirror_mode": "scheme",
"file_extension": ".rkt",
"mimetype": "text/x-racket",
- "name": "racket",
+ "name": "Racket",
"pygments_lexer": "racket",
"version": "8.7"
},