mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2026-04-03 03:15:23 +00:00
move to 2020 fodler
This commit is contained in:
43
2020/solutions/10/README.md
Normal file
43
2020/solutions/10/README.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# 10
|
||||
|
||||
# First
|
||||
|
||||
The first one is really easy. Just sort, make the diff and count.
|
||||
First I take the list and sort it:
|
||||
|
||||
```python
|
||||
[16, 10, 15, 5, 1, 11, 7, 19, 6, 12, 4]
|
||||
[0, 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, 22] # Sorted and added the wall plug (0) and the phone (biggest + 3)
|
||||
[1, 3, 1, 1, 1, 3, 1, 1, 3, 1, 3, 3] # The size of each step
|
||||
```
|
||||
|
||||
Now we can simply count how many `1` and `3` there are with `l.count(1)`.
|
||||
|
||||
## Second
|
||||
|
||||
This is where it gets tricky.
|
||||
|
||||
First lets find all the consecutive `1`s ad only they can be removed. If we have more than 1 consecutive `1` we can remove one of it. However we need to be careful not to remove to many or the step will be higher than `3` and the chain breaks.
|
||||
|
||||
```python
|
||||
[1, 1, 1, 1] # We can transform this example by adding 2 numbers together and "joining" them.
|
||||
|
||||
[1, 2, 1] # Valid
|
||||
[1, 1, 2] # Valid
|
||||
[1, 3] # Valid
|
||||
[4] # Invalid because we can jump a max of 3 steps at a time.
|
||||
```
|
||||
|
||||
Now we could iterate but I wanted to find a formula. Not sure this is correct but here we go.
|
||||
|
||||
Basically we take the length of the consecutive `1` and compute `2**(l-1)` to get all possible combinations.
|
||||
Now we need to subtract the possible `4` which can only be achieved if we have at least 4 numbers -> `floor(l/4)`
|
||||
For a grand total of `2**(l-1) - floor(l/4)`
|
||||
|
||||
<details>
|
||||
<summary>Solutions</summary>
|
||||
<ol>
|
||||
<li>2475</li>
|
||||
<li>442136281481216</li>
|
||||
</ol>
|
||||
</details>
|
||||
107
2020/solutions/10/data.txt
Normal file
107
2020/solutions/10/data.txt
Normal file
@@ -0,0 +1,107 @@
|
||||
48
|
||||
171
|
||||
156
|
||||
51
|
||||
26
|
||||
6
|
||||
80
|
||||
62
|
||||
65
|
||||
82
|
||||
130
|
||||
97
|
||||
49
|
||||
31
|
||||
142
|
||||
83
|
||||
75
|
||||
20
|
||||
154
|
||||
119
|
||||
56
|
||||
114
|
||||
92
|
||||
33
|
||||
140
|
||||
74
|
||||
118
|
||||
1
|
||||
96
|
||||
44
|
||||
128
|
||||
134
|
||||
121
|
||||
64
|
||||
158
|
||||
27
|
||||
17
|
||||
101
|
||||
59
|
||||
12
|
||||
89
|
||||
88
|
||||
145
|
||||
167
|
||||
11
|
||||
3
|
||||
39
|
||||
43
|
||||
105
|
||||
16
|
||||
170
|
||||
63
|
||||
111
|
||||
2
|
||||
108
|
||||
21
|
||||
146
|
||||
77
|
||||
45
|
||||
52
|
||||
32
|
||||
127
|
||||
147
|
||||
76
|
||||
58
|
||||
37
|
||||
86
|
||||
129
|
||||
57
|
||||
133
|
||||
120
|
||||
163
|
||||
138
|
||||
161
|
||||
139
|
||||
71
|
||||
9
|
||||
141
|
||||
168
|
||||
164
|
||||
124
|
||||
157
|
||||
95
|
||||
25
|
||||
38
|
||||
69
|
||||
87
|
||||
155
|
||||
135
|
||||
15
|
||||
102
|
||||
70
|
||||
34
|
||||
42
|
||||
24
|
||||
50
|
||||
68
|
||||
169
|
||||
10
|
||||
55
|
||||
117
|
||||
30
|
||||
81
|
||||
151
|
||||
100
|
||||
162
|
||||
148
|
||||
31
2020/solutions/10/medium.txt
Normal file
31
2020/solutions/10/medium.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
28
|
||||
33
|
||||
18
|
||||
42
|
||||
31
|
||||
14
|
||||
46
|
||||
20
|
||||
48
|
||||
47
|
||||
24
|
||||
23
|
||||
49
|
||||
45
|
||||
19
|
||||
38
|
||||
39
|
||||
11
|
||||
1
|
||||
32
|
||||
25
|
||||
35
|
||||
8
|
||||
17
|
||||
7
|
||||
9
|
||||
4
|
||||
2
|
||||
34
|
||||
10
|
||||
3
|
||||
54
2020/solutions/10/python/main.py
Normal file
54
2020/solutions/10/python/main.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from os.path import join, dirname
|
||||
from typing import List, Optional, Set, Tuple
|
||||
from itertools import combinations, count
|
||||
from math import floor, prod
|
||||
|
||||
|
||||
def parse(s: str) -> List[int]:
|
||||
numbers: List[int] = sorted(map(int, s.strip().split('\n')))
|
||||
numbers.insert(0, 0) # The wall
|
||||
numbers.append(numbers[-1] + 3) # Phone itself
|
||||
return numbers
|
||||
|
||||
|
||||
def diff(l: List[int]) -> List[int]:
|
||||
return [
|
||||
l[x] - l[x-1]
|
||||
for x in range(1, len(l))
|
||||
]
|
||||
|
||||
|
||||
def calc(d: List[int]) -> int:
|
||||
one = d.count(1)
|
||||
three = d.count(3)
|
||||
return one * three
|
||||
|
||||
|
||||
def find_valid_permutations(d: List[int]) -> int:
|
||||
i = 0
|
||||
l = len(d)
|
||||
slices: List[int] = []
|
||||
while i < l:
|
||||
if d[i] != 3:
|
||||
try:
|
||||
n = d.index(3, i + 1) # Find the next three
|
||||
diff = n - i
|
||||
if diff > 1:
|
||||
slices.append(diff)
|
||||
i = n
|
||||
continue
|
||||
except:
|
||||
pass
|
||||
i += 1
|
||||
return prod([
|
||||
2**(s-1) - floor(s/4)
|
||||
for s in slices
|
||||
])
|
||||
|
||||
|
||||
data = join(dirname(__file__), '../data.txt')
|
||||
with open(data) as f:
|
||||
numbers: List[int] = parse(f.read())
|
||||
d = diff(numbers)
|
||||
print(calc(d))
|
||||
print(find_valid_permutations(d))
|
||||
11
2020/solutions/10/test.txt
Normal file
11
2020/solutions/10/test.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
16
|
||||
10
|
||||
15
|
||||
5
|
||||
1
|
||||
11
|
||||
7
|
||||
19
|
||||
6
|
||||
12
|
||||
4
|
||||
Reference in New Issue
Block a user