Skip to content

Commit b99833b

Browse files
committed
Added day 2020-09
1 parent 9726372 commit b99833b

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

2020/09-Encoding Error.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """35
38+
20
39+
15
40+
25
41+
47
42+
40
43+
62
44+
55
45+
65
46+
95
47+
102
48+
117
49+
150
50+
182
51+
127
52+
219
53+
299
54+
277
55+
309
56+
576""",
57+
"expected": ["127", "62"],
58+
}
59+
60+
test = "real"
61+
input_file = os.path.join(
62+
os.path.dirname(__file__),
63+
"Inputs",
64+
os.path.basename(__file__).replace(".py", ".txt"),
65+
)
66+
test_data[test] = {
67+
"input": open(input_file, "r+").read(),
68+
"expected": ["1504371145", "183278487"],
69+
}
70+
71+
72+
# -------------------------------- Control program execution ------------------------- #
73+
74+
case_to_test = "real"
75+
part_to_test = 2
76+
77+
# -------------------------------- Initialize some variables ------------------------- #
78+
79+
puzzle_input = test_data[case_to_test]["input"]
80+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
81+
puzzle_actual_result = "Unknown"
82+
83+
84+
# -------------------------------- Actual code execution ----------------------------- #
85+
86+
preamble = 25 if case_to_test == "real" else 5
87+
88+
numbers = ints(puzzle_input)
89+
sums = []
90+
for vals in itertools.combinations(numbers[:preamble], 2):
91+
sums.append(sum(vals))
92+
93+
i = 0
94+
while True:
95+
sums = []
96+
for vals in itertools.combinations(numbers[i : i + preamble], 2):
97+
sums.append(sum(vals))
98+
if numbers[i + preamble] not in sums:
99+
puzzle_actual_result = numbers[i + preamble]
100+
break
101+
i += 1
102+
103+
if part_to_test == 2:
104+
invalid_number = puzzle_actual_result
105+
puzzle_actual_result = "Unknown"
106+
107+
for a in range(len(numbers)):
108+
number_sum = numbers[a]
109+
if number_sum < invalid_number:
110+
for b in range(1, len(numbers) - a):
111+
number_sum += numbers[a + b]
112+
print(a, b, number_sum, invalid_number)
113+
if number_sum == invalid_number:
114+
puzzle_actual_result = min(numbers[a : a + b + 1]) + max(
115+
numbers[a : a + b + 1]
116+
)
117+
break
118+
if number_sum > invalid_number:
119+
break
120+
if puzzle_actual_result != "Unknown":
121+
break
122+
123+
# -------------------------------- Outputs / results --------------------------------- #
124+
125+
print("Case :", case_to_test, "- Part", part_to_test)
126+
print("Expected result : " + str(puzzle_expected_result))
127+
print("Actual result : " + str(puzzle_actual_result))
128+
# Date created: 2020-12-09 06:14:55.183250
129+
# Solve part 1: 2020-12-09 06:20:49
130+
# Solve part 2: 2020-12-09 06:29:07

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy