-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01.py
More file actions
89 lines (77 loc) · 1.8 KB
/
01.py
File metadata and controls
89 lines (77 loc) · 1.8 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
f = open('01.input', 'r')
content = [int(x.strip()) for x in f.readlines()]
# O(n^2)
def part1slow(arr):
for x in arr:
for y in arr:
if x + y == 2020:
return x * y
# O(n)
def part1(arr):
rests = set()
for x in arr:
rest = 2020 - x
if rest in rests:
return rest * x
else:
rests.add(x)
print("Part 1 solution: %d" % part1(content))
# O(n^3)
def part2slow(arr):
for x in arr:
for y in arr:
for z in arr:
if x + y + z == 2020:
return x * y * z
# O(n^2 + n) => O(n^2)
def part2(arr):
rests = {}
for x in arr:
for y in arr:
rests[x + y] = x * y
for x in arr:
rest = 2020 - x
if rest in rests:
return x * rests[rest]
print("Part 2 solution: %d" % part2(content))
def part1functional(arr):
return [
a * b
for (a,b)
in filter(
lambda (a,b): a in arr and b in arr,
zip(range(0, 1010, 1), range(2020, 1010, -1))
)
][0]
print part1functional(content)
# def part1(arr):
# rests = {}
# for x in arr:
# rest = 2020 - x
# if x in rests:
# return rest * x
# else:
# rests[rest] = True
# return "not found"
#
#
# print("Part 1: %d" % part1(content))
#
# from itertools import permutations
# perm = permutations(content, 2)
# newList = []
#
# for x in perm:
# newList.append((x[0]+x[1], x))
#
# def part2(arr1, arr2):
# rests = {}
# for x in arr1:
# rests[x[0]] = x[1]
#
# for x in arr2:
# rest = 2020 - x
# if rest in rests:
# return (x * rests[rest][0] * rests[rest][1])
# return "not found"
# print("Part 2: %d" % part2(newList, content))