Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f964d7b

Browse files
authored
2019 - Day 2 (#12)
1 parent 6b674e5 commit f964d7b

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

2019/02/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,1,13,23,27,1,6,27,31,1,31,10,35,1,35,6,39,1,39,13,43,2,10,43,47,1,47,6,51,2,6,51,55,1,5,55,59,2,13,59,63,2,63,9,67,1,5,67,71,2,13,71,75,1,75,5,79,1,10,79,83,2,6,83,87,2,13,87,91,1,9,91,95,1,9,95,99,2,99,9,103,1,5,103,107,2,9,107,111,1,5,111,115,1,115,2,119,1,9,119,0,99,2,0,14,0

2019/02/solution.ipynb

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"source": [
6+
"# Advent of Code 2019 - Day 2\n",
7+
"\n",
8+
"## Part 1"
9+
],
10+
"metadata": {}
11+
},
12+
{
13+
"cell_type": "code",
14+
"source": [
15+
"def process_program(program):\n",
16+
" for idx in range(0, len(program), 4):\n",
17+
" if program[idx] == 1:\n",
18+
" # do add\n",
19+
" (idx_1, idx_2, idx_3) = program[idx + 1:idx + 4]\n",
20+
" program[idx_3] = program[idx_1] + program[idx_2]\n",
21+
" elif program[idx] == 2:\n",
22+
" # do multiply\n",
23+
" (idx_1, idx_2, idx_3) = program[idx + 1:idx + 4]\n",
24+
" program[idx_3] = program[idx_1] * program[idx_2]\n",
25+
" elif program[idx] == 99:\n",
26+
" # end program\n",
27+
" return program\n",
28+
"\n",
29+
"def str_to_list(string):\n",
30+
" return [int(i) for i in string.split(',')]\n",
31+
"\n",
32+
"print(process_program(str_to_list('1,0,0,0,99')))\n",
33+
"print(process_program(str_to_list('2,3,0,3,99')))\n",
34+
"print(process_program(str_to_list('2,4,4,5,99,0')))\n",
35+
"print(process_program(str_to_list('1,1,1,4,99,5,6,0,99')))\n",
36+
"print(process_program(str_to_list('1,9,10,3,2,3,11,0,99,30,40,50')))"
37+
],
38+
"outputs": [
39+
{
40+
"output_type": "stream",
41+
"name": "stdout",
42+
"text": [
43+
"[2, 0, 0, 0, 99]\n",
44+
"[2, 3, 0, 6, 99]\n",
45+
"[2, 4, 4, 5, 99, 9801]\n",
46+
"[30, 1, 1, 4, 2, 5, 6, 0, 99]\n",
47+
"[3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]\n"
48+
]
49+
}
50+
],
51+
"execution_count": 1,
52+
"metadata": {
53+
"collapsed": false,
54+
"outputHidden": false,
55+
"inputHidden": false
56+
}
57+
},
58+
{
59+
"cell_type": "code",
60+
"source": [
61+
"program = []\n",
62+
"with open('input.txt', 'r') as f:\n",
63+
" for line in f:\n",
64+
" program = [int(i) for i in line.split(',')]\n",
65+
"\n",
66+
"program[1] = 12\n",
67+
"program[2] = 2\n",
68+
"processed_program = process_program(program)\n",
69+
"\n",
70+
"print(f'Program: {processed_program}')\n",
71+
"print(f'Solution: {processed_program[0]}')"
72+
],
73+
"outputs": [
74+
{
75+
"output_type": "stream",
76+
"name": "stdout",
77+
"text": [
78+
"Program: [3706713, 12, 2, 2, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 10, 48, 1, 6, 19, 50, 1, 13, 23, 55, 1, 6, 27, 57, 1, 31, 10, 61, 1, 35, 6, 63, 1, 39, 13, 68, 2, 10, 43, 272, 1, 47, 6, 274, 2, 6, 51, 548, 1, 5, 55, 549, 2, 13, 59, 2745, 2, 63, 9, 8235, 1, 5, 67, 8236, 2, 13, 71, 41180, 1, 75, 5, 41181, 1, 10, 79, 41185, 2, 6, 83, 82370, 2, 13, 87, 411850, 1, 9, 91, 411853, 1, 9, 95, 411856, 2, 99, 9, 1235568, 1, 5, 103, 1235569, 2, 9, 107, 3706707, 1, 5, 111, 3706708, 1, 115, 2, 3706710, 1, 9, 119, 0, 99, 2, 0, 14, 0]\n",
79+
"Solution: 3706713\n"
80+
]
81+
}
82+
],
83+
"execution_count": 2,
84+
"metadata": {
85+
"collapsed": false,
86+
"outputHidden": false,
87+
"inputHidden": false
88+
}
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"source": [
93+
"## Part 2"
94+
],
95+
"metadata": {}
96+
},
97+
{
98+
"cell_type": "code",
99+
"source": [
100+
"program = []\n",
101+
"with open('input.txt', 'r') as f:\n",
102+
" for line in f:\n",
103+
" program = [int(i) for i in line.split(',')]"
104+
],
105+
"outputs": [],
106+
"execution_count": 3,
107+
"metadata": {
108+
"collapsed": false,
109+
"outputHidden": false,
110+
"inputHidden": false
111+
}
112+
},
113+
{
114+
"cell_type": "code",
115+
"source": [
116+
"import copy\n",
117+
"target = 19690720\n",
118+
"\n",
119+
"for noun in range(0, 99):\n",
120+
" for verb in range(0, 99):\n",
121+
" curr_program = copy.deepcopy(program)\n",
122+
" curr_program[1] = noun\n",
123+
" curr_program[2] = verb\n",
124+
" processed_program = process_program(curr_program)\n",
125+
" if processed_program[0] == target:\n",
126+
" print(f'noun: {noun}')\n",
127+
" print(f'verb: {verb}')\n",
128+
" print(f'solution: {100 * noun + verb}')"
129+
],
130+
"outputs": [
131+
{
132+
"output_type": "stream",
133+
"name": "stdout",
134+
"text": [
135+
"noun: 86\n",
136+
"verb: 9\n",
137+
"solution: 8609\n"
138+
]
139+
}
140+
],
141+
"execution_count": 4,
142+
"metadata": {
143+
"collapsed": false,
144+
"outputHidden": false,
145+
"inputHidden": false
146+
}
147+
}
148+
],
149+
"metadata": {
150+
"kernel_info": {
151+
"name": "advent-of-code"
152+
},
153+
"language_info": {
154+
"name": "python",
155+
"version": "3.8.0",
156+
"mimetype": "text/x-python",
157+
"codemirror_mode": {
158+
"name": "ipython",
159+
"version": 3
160+
},
161+
"pygments_lexer": "ipython3",
162+
"nbconvert_exporter": "python",
163+
"file_extension": ".py"
164+
},
165+
"kernelspec": {
166+
"name": "advent-of-code",
167+
"language": "python",
168+
"display_name": "advent-of-code"
169+
},
170+
"nteract": {
171+
"version": "0.15.0"
172+
}
173+
},
174+
"nbformat": 4,
175+
"nbformat_minor": 4
176+
}

0 commit comments

Comments
 (0)