diff options
| author | 2018-12-04 00:50:47 +0100 | |
|---|---|---|
| committer | 2018-12-04 00:50:47 +0100 | |
| commit | 9389bb9fee5c7972e7aad80961ef3a9dc8916c43 (patch) | |
| tree | 38afb168dfb8ada2d12ce158bfe21484cbd96c99 | |
| download | 2018-9389bb9fee5c7972e7aad80961ef3a9dc8916c43.tar.gz 2018-9389bb9fee5c7972e7aad80961ef3a9dc8916c43.tar.bz2 2018-9389bb9fee5c7972e7aad80961ef3a9dc8916c43.zip | |
Added day 1 to 3 2018
| -rw-r--r-- | 2018/.gitignore | 3 | ||||
| -rw-r--r-- | 2018/Pipfile | 11 | ||||
| -rw-r--r-- | 2018/aoc/__init__.py | 0 | ||||
| -rw-r--r-- | 2018/aoc/__main__.py | 15 | ||||
| -rw-r--r-- | 2018/aoc/day1.py | 18 | ||||
| -rw-r--r-- | 2018/aoc/day2.py | 28 | ||||
| -rw-r--r-- | 2018/aoc/day3.py | 55 | ||||
| -rw-r--r-- | 2018/inputs/day1.txt | 1029 | ||||
| -rw-r--r-- | 2018/inputs/day2.txt | 250 | ||||
| -rw-r--r-- | 2018/inputs/day3.txt | 1349 |
10 files changed, 2758 insertions, 0 deletions
diff --git a/2018/.gitignore b/2018/.gitignore new file mode 100644 index 0000000..aa6cef2 --- /dev/null +++ b/2018/.gitignore | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | __pycache__/ | ||
| 2 | *.pyc | ||
| 3 | .idea/ | ||
diff --git a/2018/Pipfile b/2018/Pipfile new file mode 100644 index 0000000..b9ba84f --- /dev/null +++ b/2018/Pipfile | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | [[source]] | ||
| 2 | url = "https://pypi.org/simple" | ||
| 3 | verify_ssl = true | ||
| 4 | name = "pypi" | ||
| 5 | |||
| 6 | [packages] | ||
| 7 | |||
| 8 | [dev-packages] | ||
| 9 | |||
| 10 | [requires] | ||
| 11 | python_version = "3.7" | ||
diff --git a/2018/aoc/__init__.py b/2018/aoc/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/2018/aoc/__init__.py | |||
diff --git a/2018/aoc/__main__.py b/2018/aoc/__main__.py new file mode 100644 index 0000000..81eb718 --- /dev/null +++ b/2018/aoc/__main__.py | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | import sys | ||
| 2 | from importlib import import_module | ||
| 3 | |||
| 4 | day = import_module(f'.{sys.argv[1]}', 'aoc') | ||
| 5 | |||
| 6 | |||
| 7 | try: | ||
| 8 | input_file = open(sys.argv[2]).read() | ||
| 9 | except (OSError, IndexError): | ||
| 10 | input_file = sys.stdin.read() | ||
| 11 | |||
| 12 | day.main(input_file.splitlines()) | ||
| 13 | |||
| 14 | |||
| 15 | |||
diff --git a/2018/aoc/day1.py b/2018/aoc/day1.py new file mode 100644 index 0000000..9b08ab6 --- /dev/null +++ b/2018/aoc/day1.py | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | from itertools import cycle | ||
| 2 | |||
| 3 | |||
| 4 | def main(input_file: list): | ||
| 5 | input_file = [int(i) for i in input_file] | ||
| 6 | print(f'Frequency: {sum(input_file)}') | ||
| 7 | |||
| 8 | frequency = 0 | ||
| 9 | frequencies = { frequency } | ||
| 10 | for i in cycle(input_file): | ||
| 11 | frequency += i | ||
| 12 | |||
| 13 | if frequency in frequencies: | ||
| 14 | break | ||
| 15 | |||
| 16 | frequencies.add(frequency) | ||
| 17 | |||
| 18 | print(f'First repeating frequency: {frequency}') | ||
diff --git a/2018/aoc/day2.py b/2018/aoc/day2.py new file mode 100644 index 0000000..5e9d07d --- /dev/null +++ b/2018/aoc/day2.py | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | from collections import Counter | ||
| 2 | |||
| 3 | |||
| 4 | def any_combination(ids: list) -> iter: | ||
| 5 | for id1 in ids: | ||
| 6 | for id2 in ids: | ||
| 7 | yield id1, id2 | ||
| 8 | |||
| 9 | |||
| 10 | def main(input_file: list): | ||
| 11 | twicers = 0 | ||
| 12 | tricers = 0 | ||
| 13 | |||
| 14 | for box_id in input_file: | ||
| 15 | char_count = Counter(box_id).values() | ||
| 16 | twicers += 1 if 2 in char_count else 0 | ||
| 17 | tricers += 1 if 3 in char_count else 0 | ||
| 18 | |||
| 19 | common_letters = '' | ||
| 20 | for id1, id2 in any_combination(input_file): | ||
| 21 | difference = [i for i, c in enumerate(id1) if c != id2[i]] | ||
| 22 | if len(difference) == 1: | ||
| 23 | index = difference[0] | ||
| 24 | common_letters = id1[:index] + id1[index + 1:] | ||
| 25 | break | ||
| 26 | |||
| 27 | print(f'Checksum {twicers * tricers}') | ||
| 28 | print(f'Common {common_letters}') \ No newline at end of file | ||
diff --git a/2018/aoc/day3.py b/2018/aoc/day3.py new file mode 100644 index 0000000..935ea33 --- /dev/null +++ b/2018/aoc/day3.py | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | import re | ||
| 2 | from collections import Counter | ||
| 3 | |||
| 4 | pattern = re.compile(r'^#(?P<id>\d+) @ (?P<xy>\d+,\d+): (?P<wh>\d+x\d+)$') | ||
| 5 | |||
| 6 | |||
| 7 | def to_tuple(string, separator) -> tuple: | ||
| 8 | return tuple(int(_) for _ in string.split(separator)) | ||
| 9 | |||
| 10 | |||
| 11 | def parse_claims(claims: list) -> iter: | ||
| 12 | for claim in claims: | ||
| 13 | matches = pattern.match(claim).groupdict() | ||
| 14 | yield { | ||
| 15 | 'id': matches.get('id'), | ||
| 16 | 'xy': to_tuple(matches.get('xy'), ','), | ||
| 17 | 'wh': to_tuple(matches.get('wh'), 'x') | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | def every_coordinate(claim: dict) -> iter: | ||
| 22 | start_x, start_y = claim.get('xy') | ||
| 23 | width, height = claim.get('wh') | ||
| 24 | for x in range(width): | ||
| 25 | for y in range(height): | ||
| 26 | yield f'{start_x + x},{start_y + y}' | ||
| 27 | |||
| 28 | |||
| 29 | def main(input_file: list): | ||
| 30 | claims = list(parse_claims(input_file)) | ||
| 31 | |||
| 32 | coordinates = [] | ||
| 33 | for claim in claims: | ||
| 34 | for coordinate in every_coordinate(claim): | ||
| 35 | coordinates.append(coordinate) | ||
| 36 | |||
| 37 | coordinate_claims = Counter(coordinates) | ||
| 38 | multiple_claimed = [c for c, n in coordinate_claims.items() if n > 1] | ||
| 39 | print(f'Nr of squares claimed by 2 or more: {len(multiple_claimed)}') | ||
| 40 | |||
| 41 | fabric_claims = set() | ||
| 42 | overlapping_claimes = set() | ||
| 43 | for claim in claims: | ||
| 44 | fabric_claims.add(claim.get('id')) | ||
| 45 | |||
| 46 | for coordinate in every_coordinate(claim): | ||
| 47 | if coordinate_claims[coordinate] > 1: | ||
| 48 | overlapping_claimes.add(claim.get('id')) | ||
| 49 | break | ||
| 50 | |||
| 51 | |||
| 52 | non_overlapping_claims = fabric_claims - overlapping_claimes | ||
| 53 | print(f'Claim that does not overlap: {non_overlapping_claims.pop()}') | ||
| 54 | |||
| 55 | |||
diff --git a/2018/inputs/day1.txt b/2018/inputs/day1.txt new file mode 100644 index 0000000..9390ccc --- /dev/null +++ b/2018/inputs/day1.txt | |||
| @@ -0,0 +1,1029 @@ | |||
| 1 | -7 | ||
| 2 | +16 | ||
| 3 | +5 | ||
| 4 | +11 | ||
| 5 | +18 | ||
| 6 | -14 | ||
| 7 | +11 | ||
| 8 | +14 | ||
| 9 | -2 | ||
| 10 | +13 | ||
| 11 | -12 | ||
| 12 | +10 | ||
| 13 | +1 | ||
| 14 | +16 | ||
| 15 | +17 | ||
| 16 | +5 | ||
| 17 | -8 | ||
| 18 | +17 | ||
| 19 | +15 | ||
| 20 | -17 | ||
| 21 | +7 | ||
| 22 | -1 | ||
| 23 | -3 | ||
| 24 | -8 | ||
| 25 | -12 | ||
| 26 | -1 | ||
| 27 | -14 | ||
| 28 | -19 | ||
| 29 | +2 | ||
| 30 | -19 | ||
| 31 | +5 | ||
| 32 | +10 | ||
| 33 | +1 | ||
| 34 | -9 | ||
| 35 | -18 | ||
| 36 | -3 | ||
| 37 | +8 | ||
| 38 | +3 | ||
| 39 | +1 | ||
| 40 | +5 | ||
| 41 | +7 | ||
| 42 | -2 | ||
| 43 | -21 | ||
| 44 | -2 | ||
| 45 | +11 | ||
| 46 | +10 | ||
| 47 | +19 | ||
| 48 | +8 | ||
| 49 | -15 | ||
| 50 | +19 | ||
| 51 | -3 | ||
| 52 | +7 | ||
| 53 | -1 | ||
| 54 | -13 | ||
| 55 | +5 | ||
| 56 | +17 | ||
| 57 | -18 | ||
| 58 | +7 | ||
| 59 | +9 | ||
| 60 | +1 | ||
| 61 | -6 | ||
| 62 | +13 | ||
| 63 | -3 | ||
| 64 | +12 | ||
| 65 | +17 | ||
| 66 | +1 | ||
| 67 | +10 | ||
| 68 | +9 | ||
| 69 | -17 | ||
| 70 | -15 | ||
| 71 | +14 | ||
| 72 | +13 | ||
| 73 | +15 | ||
| 74 | +12 | ||
| 75 | +2 | ||
| 76 | -19 | ||
| 77 | +11 | ||
| 78 | -3 | ||
| 79 | +10 | ||
| 80 | +17 | ||
| 81 | -6 | ||
| 82 | +11 | ||
| 83 | -2 | ||
| 84 | +13 | ||
| 85 | +17 | ||
| 86 | +16 | ||
| 87 | +4 | ||
| 88 | -16 | ||
| 89 | +14 | ||
| 90 | -10 | ||
| 91 | -2 | ||
| 92 | -13 | ||
| 93 | -11 | ||
| 94 | -1 | ||
| 95 | -5 | ||
| 96 | -5 | ||
| 97 | +15 | ||
