1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Advent of Code 2022\n",
"#### Day 3: Rucksack Reorganization\n",
"\n",
"**Part 1.** Every elf has a rucksack with two compartments. What's the total priority value of the items that appear in both compartments of each sack?\n",
"\n",
"**Part 2.** Each group of three elves is carrying exactly one item in common. What's the total priority value of those common items?\n",
"\n",
"To ease the conversion between strings, lists and sets I bring in some utility functions from `relation`. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"(require racket\n",
" advent-of-code\n",
" threading\n",
" (only-in relation ->list ->set ->char))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The items in each elf's inventory are represented by a string of alphabetic (case-sensitive) characters, each with a \"priority value\":\n",
"* Lowercase item types `a` through `z` have priorities 1 through 26.\n",
"* Uppercase item types `A` through `Z` have priorities 27 through 52."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"(define priority\n",
" (for/hash ([i (in-naturals 1)]\n",
" [c (in-sequences (inclusive-range 97 122) (inclusive-range 65 90))])\n",
" (values (->char c) i)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Part 1\n",
"\n",
"The front half and back half of the rucksack have one item in common we need to identify. For each rucksack in the inventory, we apply the following steps:\n",
"\n",
"1. Split the bag into the two halves,\n",
"2. find the common item in both halves (the intersection of the sets),\n",
"3. then look up that common item's value. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [
{
"data": {
"text/html": [
"<code>7845</code>"
],
"text/plain": [
"7845"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define raw-inventory (~> (fetch-aoc-input (find-session) 2022 3) string-split))\n",
"\n",
"(for/sum ([bag (in-list raw-inventory)])\n",
" (define len (/ (string-length bag) 2))\n",
" (~>> bag\n",
" ->list\n",
" ((λ (xs) (list (take xs len) (drop xs len)))) ; step 1\n",
" (apply set-intersect _) ; step 2\n",
" set-first\n",
" (hash-ref priority))) ; step 3\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Part 2\n",
"\n",
"Now we need to take three rucksacks at a time and find the common item they all share. The procedure is simpler than Part 1's, especially with the `in-slice` function that automatically forms groups of three."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [
{
"data": {
"text/html": [
"<code>2790</code>"
],
"text/plain": [
"2790"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(for/sum ([bags (in-slice 3 raw-inventory)])\n",
" (~>> bags (map ->set) (apply set-intersect) set-first (hash-ref priority)))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Racket (Trusted)",
"language": "racket",
"name": "racket-trusted"
},
"language_info": {
"codemirror_mode": "scheme",
"file_extension": ".rkt",
"mimetype": "text/x-racket",
"name": "Racket",
"pygments_lexer": "racket",
"version": "8.7"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "7454d72389401259f8ab87ac90deac92d19baedf4a52e60301852b1f4c653c5c"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|