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
|
{
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"#lang iracket/lang #:require racket\n",
"(require advent-of-code\n",
" threading\n",
" rebellion/collection/entry\n",
" rebellion/collection/multidict)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [
{
"data": {
"text/html": [
"<code>'(acc . 49)</code>"
],
"text/plain": [
"'(acc . 49)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define raw-instructions (~> (fetch-aoc-input (find-session) 2020 8) (string-split \"\\n\")))\n",
"\n",
"(define instruction-set (for/hash ([instruction (in-list raw-instructions)] [i (in-naturals)])\n",
" (match-define (list op val) (string-split instruction))\n",
" (values i (cons (string->symbol op) (string->number val)))))\n",
"\n",
"(hash-ref instruction-set 0)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [
{
"data": {
"text/html": [
"<code>1949</code>"
],
"text/plain": [
"1949"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define (execute [instruction (hash-ref instruction-set 0)] [acc 0] [line 0] [visited (set)])\n",
" (match instruction\n",
" [_\n",
" #:when (set-member? visited line)\n",
" acc]\n",
" [(cons 'acc n)\n",
" (define new-line (add1 line))\n",
" (execute (hash-ref instruction-set new-line) (+ acc n) new-line (set-add visited line))]\n",
" [(cons 'jmp n)\n",
" (define new-line (+ n line))\n",
" (execute (hash-ref instruction-set new-line) acc new-line (set-add visited line))]\n",
" [(cons 'nop _)\n",
" (define new-line (add1 line))\n",
" (execute (hash-ref instruction-set new-line) acc new-line (set-add visited line))]))\n",
"\n",
"(execute)\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
},
"nbformat": 4,
"nbformat_minor": 2
}
|