Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this user
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
aborrego3
/
Advent-of-Code-2020
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Projects
Security
Insights
Files
master
days
etc
day01.py
day02.py
day03.py
day04.py
day05.py
day06.py
day07.py
day08.py
day09.py
day10.py
day11.py
day12.py
day13.py
day14.py
day15.py
day16.py
day17.py
day18.py
day19.py
day20.py
day21.py
day22.py
day23.py
day24.py
day25.py
input
rust
.gitignore
README.md
requirements.txt
Breadcrumbs
Advent-of-Code-2020
/
days
/
day19.py
Blame
Blame
Latest commit
History
History
36 lines (29 loc) · 975 Bytes
Breadcrumbs
Advent-of-Code-2020
/
days
/
day19.py
Top
File metadata and controls
Code
Blame
36 lines (29 loc) · 975 Bytes
Raw
from etc.utils import file_to_lines from etc.d19 import CompositePattern, TextPattern import regex as re RE_PAT = re.compile(r"(\d+): (.*)") RE_VAL = re.compile(r'"(.*)"') data = file_to_lines("input/day19.txt", spl="\n\n") patterns = list(data[0].split("\n")) lines = list(data[1].split("\n")) # Parsing rules rules = {} for pat in patterns: i, text = RE_PAT.match(pat).groups() m = RE_VAL.match(text) if m: text = m.group(1) rules[int(i)] = TextPattern(text) else: rules[int(i)] = CompositePattern(text) def count_matches(regex, lines): return sum(int(bool(regex.match(line))) for line in lines) # Part 1 re_0_text = rules[0].to_regex(rules) re_part_1 = re.compile(re_0_text + "$") print("Part 1:", count_matches(re_part_1, lines)) # Part 2 r42 = rules[42].to_regex(rules) r31 = rules[31].to_regex(rules) re_part_2 = re.compile(f"({r42})+(?<rec>({r42})(?&rec)?({r31}))$") print("Part 2:", count_matches(re_part_2, lines))
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
You can’t perform that action at this time.