aboutsummaryrefslogtreecommitdiff
path: root/racket/aoc2020/day-09/day-09.ipynb
blob: e6f712b00bc8c9852d0dd628e8db782f58e2706f (plain)
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Advent of Code 2020\n",
    "#### Day 9: Encoding Error\n",
    "\n",
    "In a list of integers, each number after the 25th should be the sum of two of the previous 25 numbers.\n",
    "\n",
    "1. What's the first number in the list that does not have this property?\n",
    "2. The \"encryption weakness\" is the sum of the extrema in a contiguous range of numbers that sums up to the invalid number in part 1.  Find the encryption weakness.\n",
    "\n",
    "I'm using structural pattern matching for this solution, so no extra packages are required beyond the usual."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "vscode": {
     "languageId": "racket"
    }
   },
   "outputs": [],
   "source": [
    "#lang iracket/lang #:require racket\n",
    "\n",
    "(require advent-of-code\n",
    "         threading)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The data is just a list of integers, so it's straightforward to process."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "vscode": {
     "languageId": "racket"
    }
   },
   "outputs": [],
   "source": [
    "(define preamble\n",
    "  (~> (fetch-aoc-input (find-session) 2020 9) (string-split \"\\n\") (map string->number _)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Part 1\n",
    "\n",
    "First, we bind the first 25 values to `head` and the 26th to `x`.\n",
    "\n",
    "In the `match` syntax, `list-no-order` binds `a` and `b` to the first pair of numbers from anywhere in the first 25 values that satisfies the `#:when` guard.  The exact pair doesn't matter; all we need to know is if it's valid and we can move on to the next test.\n",
    "\n",
    "If nothing satisfies the first clause, we've found our invalid number.  We're guaranteed to have an invalid number in the set, so we don't need to guard against `match-define-values` failing when there's fewer than 26 values to work with."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "vscode": {
     "languageId": "racket"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<code>1038347917</code>"
      ],
      "text/plain": [
       "1038347917"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(define (find-invalid-number xs)\n",
    "  (match-define-values (head (list x _ ...)) (split-at xs 25))\n",
    "  (match head\n",
    "    [(list-no-order a b _ ...)\n",
    "     #:when (= x (+ a b))\n",
    "     (find-invalid-number (rest xs))]\n",
    "    [_ x]))\n",
    "\n",
    "(define target-sum (find-invalid-number preamble))\n",
    "target-sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Part 2\n",
    "\n",
    "We can find the range with another match statement, this time looking for a sub-list that's at least two elements long and that satisfies the guard.  Everything after this is just arithmetic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "vscode": {
     "languageId": "racket"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<code>137394018</code>"
      ],
      "text/plain": [
       "137394018"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(define (find-contiguous-range xs)\n",
    "  (match xs\n",
    "    [(list _ ... x ..2 _ ...)\n",
    "     #:when (= (apply + x) target-sum)\n",
    "     x]))\n",
    "\n",
    "(define target-range (find-contiguous-range preamble))\n",
    "(+ (apply max target-range) (apply min target-range))"
   ]
  }
 ],
 "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
 },
 "nbformat": 4,
 "nbformat_minor": 2
}