Skip to content

Commit 798045e

Browse files
authored
AoC
1 parent 778f22d commit 798045e

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

ipynb/Advent-2022.ipynb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,9 @@
17781778
"source": [
17791779
"#### Part 1: Determine which pairs of packets are already in the right order. What is the sum of the indices of those pairs?\n",
17801780
"\n",
1781-
"The notion of \"right order\" is almost the same as \"less than\", except that an integer is equal to a singleton list. Also, I was confused for a bit because the rules don't say whether two identical lists are in the right order or not; the rules just say you should \"continue checking\" in that case. I decided that I should not define a Boolean truth predicate; rather it should be a *ternary* comparison function. By convention, a Python comparison function, `compare(left, right)`, returns a negative number if `left < right`, zero if they are equal, and a positive number if `left > right`. I'll implement that:"
1781+
"The notion of \"right order\" is almost the same as \"less than\", except that an integer can match against a singleton list. Also, I was confused for a bit because the rules don't say whether two identical lists are in the right order or not; the rules just say you should \"continue checking\" in that case. I decided that I should not define a Boolean truth predicate; rather it should be a *ternary* comparison function. \n",
1782+
"\n",
1783+
"By convention, a Python comparison function, `compare(left, right)`, returns a negative number if `left < right`, zero if they are equal, and a positive number if `left > right`. I'll implement that:"
17821784
]
17831785
},
17841786
{
@@ -1791,18 +1793,18 @@
17911793
" \"\"\"Are the two packets in the right order? \n",
17921794
" Negative number for right order; 0 for equal; positive for wrong order.\"\"\"\n",
17931795
" types = (type(left), type(right))\n",
1794-
" if types == (int, int): return left - right\n",
1795-
" elif types == (int, list): return compare([left], right)\n",
1796-
" elif types == (list, int): return compare(left, [right])\n",
1797-
" elif types == (list, list): return first_true(map(compare, left, right), \n",
1798-
" default=len(left) - len(right))"
1796+
" if types == (int, int): return left - right\n",
1797+
" elif types == (int, list): return compare([left], right)\n",
1798+
" elif types == (list, int): return compare(left, [right])\n",
1799+
" elif types == (list, list): return first_true(map(compare, left, right), \n",
1800+
" default=len(left) - len(right))"
17991801
]
18001802
},
18011803
{
18021804
"cell_type": "markdown",
18031805
"metadata": {},
18041806
"source": [
1805-
"The call to `first_true` (from my [AdventUtils](AdventUtils.ipynb) or from [itertools recipes](https://docs.python.org/3/library/itertools.html#itertools-recipes)) says to `compare` respective successive elements of the two lists one by one, and to return the first true (i.e. non-zero) comparison, or if one of the two lists is exhausted first, return the comparison of the list lengths. \n",
1807+
"The call to `first_true` (from my [AdventUtils](AdventUtils.ipynb)) says to `compare` respective successive elements of the two lists one by one, and to return the first truthy (i.e. non-zero) comparison, or if one of the two lists is exhausted first, return the comparison of the list lengths. \n",
18061808
"\n",
18071809
"I threw in some test cases to gain some confidence that I got it right: "
18081810
]
@@ -1850,9 +1852,9 @@
18501852
"source": [
18511853
"#### Part 2: Organize all of the packets into the correct order. What is the decoder key for the distress signal?\n",
18521854
"\n",
1853-
"This should be easy; all I have to do is sort using `right_order`, then look up the resulting indices of the two divider packets (taking care to use 1-based rather than 0-based indexing). I tried to sort using `key=right_order` and got an error message reminding me how foolish I am: `right_order` is not a key function (of one argument), it is a comparison function (of two arguments).\n",
1855+
"This should be easy; all I have to do is sort using `compare`, then look up the resulting indices of the two divider packets (taking care to use 1-based rather than 0-based indexing). I tried to sort using `key=compare` and got an error message reminding me how foolish I am: `compare` is not a key function (of one argument), it is a comparison function (of two arguments).\n",
18541856
"\n",
1855-
"In Python 2, the `sorted` function did accept a two-argument `cmp` function, but in Python 3 that functionality is gone. I had to look it up to find that the `functools.cmp_to_key` function converts a comparison function to a key function, and that a `cmp_to_key` comparison function returns -1/0/+1, not True/None/False like my `right_order` does. Once I correct for that, it works fine."
1857+
"In Python 2, the `sorted` function accepted a two-argument `cmp` function, but in Python 3 that functionality is gone. I had to look it up to find that the `functools.cmp_to_key` function converts a comparison function to a key function. "
18561858
]
18571859
},
18581860
{

0 commit comments

Comments
 (0)