Skip to content

Commit 48288f9

Browse files
committed
More comfortable with Python and Jupiter.
1 parent 4db080a commit 48288f9

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.ipynb_checkpoints/
2+
*.swp
3+

aoc2020.ipynb

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Advent of Code 2020\n",
8+
"\n",
9+
"[Website](https://adventofcode.com/2020/)"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"## Day 2\n",
17+
"\n",
18+
"### How many passwords are valid?"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 26,
24+
"metadata": {},
25+
"outputs": [
26+
{
27+
"name": "stdout",
28+
"output_type": "stream",
29+
"text": [
30+
"591\n"
31+
]
32+
}
33+
],
34+
"source": [
35+
"import re\n",
36+
"\n",
37+
"valid = 0\n",
38+
"\n",
39+
"with open('day02-input.txt') as input_file:\n",
40+
" for line in input_file:\n",
41+
" pw = re.split(r'-|:? ', str.strip(line))\n",
42+
" matched = re.findall(pw[2], pw[3])\n",
43+
" if len(matched) >= int(pw[0]) and len(matched) <= int(pw[1]):\n",
44+
" # print(pw)\n",
45+
" valid = valid + 1\n",
46+
"\n",
47+
"print(valid)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"### New validation rules"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 53,
60+
"metadata": {},
61+
"outputs": [
62+
{
63+
"name": "stdout",
64+
"output_type": "stream",
65+
"text": [
66+
"335\n"
67+
]
68+
}
69+
],
70+
"source": [
71+
"import operator\n",
72+
"\n",
73+
"valid_pos = 0\n",
74+
"\n",
75+
"with open('day02-input.txt') as input_file:\n",
76+
" for line in input_file:\n",
77+
" pw = re.split(r'-|:? ', str.strip(line))\n",
78+
" pos0 = int(pw[0]) - 1\n",
79+
" pos1 = int(pw[1]) - 1\n",
80+
" ltr = pw[2]\n",
81+
" pw_str = pw[3]\n",
82+
" if operator.xor(pw_str[pos0] == ltr, pw_str[pos1] == ltr):\n",
83+
" valid_pos = valid_pos + 1\n",
84+
"\n",
85+
"print(valid_pos)"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": []
94+
}
95+
],
96+
"metadata": {
97+
"kernelspec": {
98+
"display_name": "Python 3",
99+
"language": "python",
100+
"name": "python3"
101+
},
102+
"language_info": {
103+
"codemirror_mode": {
104+
"name": "ipython",
105+
"version": 3
106+
},
107+
"file_extension": ".py",
108+
"mimetype": "text/x-python",
109+
"name": "python",
110+
"nbconvert_exporter": "python",
111+
"pygments_lexer": "ipython3",
112+
"version": "3.8.1"
113+
}
114+
},
115+
"nbformat": 4,
116+
"nbformat_minor": 4
117+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy