-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.py
executable file
·59 lines (45 loc) · 2.23 KB
/
day01.py
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
#!/usr/bin/env python3
"""
--- Day 1: Report Repair ---
After saving Christmas five years in a row, you've decided to take a vacation at
a nice resort on a tropical island. Surely, Christmas will go on without you.
The tropical island has its own currency and is entirely cash-only. The gold
coins used there have a little picture of a starfish; the locals just call them
stars. None of the currency exchanges seem to have heard of them, but somehow,
you'll need to find fifty of these coins by the time you arrive so you can pay
the deposit on your room.
To save your vacation, you need to get all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day
in the Advent calendar; the second puzzle is unlocked when you complete the
first. Each puzzle grants one star. Good luck!
Before you leave, the Elves in accounting just need you to fix your expense
report (your puzzle input); apparently, something isn't quite adding up.
Specifically, they need you to find the two entries that sum to 2020 and then
multiply those two numbers together.
--- Part Two ---
The Elves in accounting are thankful for your help; one of them even offers you
a starfish coin they had left over from a past vacation. They offer you a second
one if you can find three numbers in your expense report that meet the same
criteria.
"""
import argparse
from functools import reduce
from itertools import permutations
parser = argparse.ArgumentParser(epilog=__doc__,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('input', type=argparse.FileType('rt'))
parser.add_argument('-n',
type=int,
default=3,
help='How many numbers to search that sum up to 2020')
args = parser.parse_args()
# sorting may decrease runtime, but that is pure luck with the naive approach
input_sorted = sorted([int(line) for line in args.input.readlines()])
# build permutations and check if their sum adds up.
# note: only gets one matching pair, stops calculation afterwards
magic_entries = next(
filter(lambda v: sum(v) == 2020,
permutations(input_sorted, args.n))
)
print(magic_entries)
print(reduce(lambda a, b: a*b, magic_entries, 1))