Skip to content

Commit 55681fc

Browse files
committed
Added 2021-01, 2021-02, 2021-03
1 parent 7675ea5 commit 55681fc

File tree

9 files changed

+2467
-0
lines changed

9 files changed

+2467
-0
lines changed

2021/01-Sonar Sweep.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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": """199
38+
200
39+
208
40+
210
41+
200
42+
207
43+
240
44+
269
45+
260
46+
263""",
47+
"expected": ["7", "5"],
48+
}
49+
50+
test = "real"
51+
input_file = os.path.join(
52+
os.path.dirname(__file__),
53+
"Inputs",
54+
os.path.basename(__file__).replace(".py", ".txt"),
55+
)
56+
test_data[test] = {
57+
"input": open(input_file, "r+").read(),
58+
"expected": ["1766", "1797"],
59+
}
60+
61+
62+
# -------------------------------- Control program execution ------------------------- #
63+
64+
case_to_test = "real"
65+
part_to_test = 2
66+
67+
# -------------------------------- Initialize some variables ------------------------- #
68+
69+
puzzle_input = test_data[case_to_test]["input"]
70+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
71+
puzzle_actual_result = "Unknown"
72+
73+
74+
# -------------------------------- Actual code execution ----------------------------- #
75+
76+
# Conver integer to 36-character binary
77+
# str_value = "{0:>036b}".format(value)
78+
# Convert binary string to number
79+
# value = int(str_value, 2)
80+
81+
82+
if part_to_test == 1:
83+
val = ints(puzzle_input)
84+
puzzle_actual_result = sum(
85+
[1 if val[n] > val[n - 1] else 0 for n in range(1, len(val))]
86+
)
87+
88+
89+
else:
90+
val = ints(puzzle_input)
91+
puzzle_actual_result = sum(
92+
[
93+
1 if sum(val[n - 2 : n + 1]) > sum(val[n - 3 : n]) else 0
94+
for n in range(3, len(val))
95+
]
96+
)
97+
# puzzle_actual_result = [(sum(val[n-2:n+1]) , sum(val[n-3:n])) for n in range(3, len(val))]
98+
99+
100+
# -------------------------------- Outputs / results --------------------------------- #
101+
102+
print("Case :", case_to_test, "- Part", part_to_test)
103+
print("Expected result : " + str(puzzle_expected_result))
104+
print("Actual result : " + str(puzzle_actual_result))
105+
# Date created: 2021-12-01 08:11:26.495595
106+
# Part 1: 2021-12-01 08:15:45
107+
# Part 2: 2021-12-01 08:20:37

2021/02-Dive.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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": """forward 5
38+
down 5
39+
forward 8
40+
up 3
41+
down 8
42+
forward 2""",
43+
"expected": ["150", "900"],
44+
}
45+
46+
test = "real"
47+
input_file = os.path.join(
48+
os.path.dirname(__file__),
49+
"Inputs",
50+
os.path.basename(__file__).replace(".py", ".txt"),
51+
)
52+
test_data[test] = {
53+
"input": open(input_file, "r+").read(),
54+
"expected": ["1962940", "1813664422"],
55+
}
56+
57+
58+
# -------------------------------- Control program execution ------------------------- #
59+
60+
case_to_test = "real"
61+
part_to_test = 2
62+
63+
# -------------------------------- Initialize some variables ------------------------- #
64+
65+
puzzle_input = test_data[case_to_test]["input"]
66+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
67+
puzzle_actual_result = "Unknown"
68+
69+
70+
# -------------------------------- Actual code execution ----------------------------- #
71+
72+
# Conver integer to 36-character binary
73+
# str_value = "{0:>036b}".format(value)
74+
# Convert binary string to number
75+
# value = int(str_value, 2)
76+
77+
dirs = {"forward": 1, "down": -1j, "up": +1j}
78+
79+
position = 0
80+
aim = 0
81+
if part_to_test == 1:
82+
for string in puzzle_input.split("\n"):
83+
direction, delta = string.split(" ")
84+
position += dirs[direction] * int(delta)
85+
86+
puzzle_actual_result = int(abs(position.imag) * abs(position.real))
87+
88+
89+
else:
90+
for string in puzzle_input.split("\n"):
91+
direction, delta = string.split(" ")
92+
if direction == "down" or direction == "up":
93+
aim += dirs[direction] * int(delta)
94+
else:
95+
position += int(delta)
96+
position += int(delta) * abs(aim.imag) * 1j
97+
98+
print(string, aim, position)
99+
100+
puzzle_actual_result = int(abs(position.imag) * abs(position.real))
101+
102+
103+
# -------------------------------- Outputs / results --------------------------------- #
104+
105+
print("Case :", case_to_test, "- Part", part_to_test)
106+
print("Expected result : " + str(puzzle_expected_result))
107+
print("Actual result : " + str(puzzle_actual_result))
108+
# Date created: 2021-12-02 07:43:32.238803
109+
# Part 1: 2021-12-02 07:46:00
110+
# Part 2: 2021-12-02 07:50:10

2021/03-Binary Diagnostic.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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": """00100
38+
11110
39+
10110
40+
10111
41+
10101
42+
01111
43+
00111
44+
11100
45+
10000
46+
11001
47+
00010
48+
01010""",
49+
"expected": ["198", "230"],
50+
}
51+
52+
test = "real"
53+
input_file = os.path.join(
54+
os.path.dirname(__file__),
55+
"Inputs",
56+
os.path.basename(__file__).replace(".py", ".txt"),
57+
)
58+
test_data[test] = {
59+
"input": open(input_file, "r+").read(),
60+
"expected": ["3985686", "2555739"],
61+
}
62+
63+
64+
# -------------------------------- Control program execution ------------------------- #
65+
66+
case_to_test = "real"
67+
part_to_test = 2
68+
69+
# -------------------------------- Initialize some variables ------------------------- #
70+
71+
puzzle_input = test_data[case_to_test]["input"]
72+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
73+
puzzle_actual_result = "Unknown"
74+
75+
76+
# -------------------------------- Actual code execution ----------------------------- #
77+
78+
# Conver integer to 36-character binary
79+
# str_value = "{0:>036b}".format(value)
80+
# Convert binary string to number
81+
# value = int(str_value, 2)
82+
83+
84+
length_binary = len(puzzle_input.split("\n")[0])
85+
86+
gamma = [0] * length_binary
87+
epsilon = [0] * length_binary
88+
counts = [0] * length_binary
89+
90+
91+
def count_binary(source):
92+
zero = [0] * len(source[0])
93+
ones = [0] * len(source[0])
94+
for string in source:
95+
for i in range(length_binary):
96+
zero[i] += 1 - int(string[i])
97+
ones[i] += int(string[i])
98+
99+
return (zero, ones)
100+
101+
102+
if part_to_test == 1:
103+
for string in puzzle_input.split("\n"):
104+
for i in range(length_binary):
105+
counts[i] += int(string[i])
106+
107+
for i in range(length_binary):
108+
if counts[i] >= len(puzzle_input.split("\n")) // 2:
109+
gamma[i] = 1
110+
else:
111+
epsilon[i] = 1
112+
113+
gamma = int("".join(map(str, gamma)), 2)
114+
epsilon = int("".join(map(str, epsilon)), 2)
115+
116+
puzzle_actual_result = (gamma, epsilon, gamma * epsilon)[2]
117+
118+
119+
else:
120+
oxygen = puzzle_input.split("\n")
121+
co2 = puzzle_input.split("\n")
122+
123+
for i in range(length_binary):
124+
if len(oxygen) != 1:
125+
zero, ones = count_binary(oxygen)
126+
127+
if ones[i] >= zero[i]:
128+
oxygen = [n for n in oxygen if int(n[i]) == 1]
129+
else:
130+
oxygen = [n for n in oxygen if int(n[i]) == 0]
131+
132+
if len(co2) != 1:
133+
zero, ones = count_binary(co2)
134+
if ones[i] >= zero[i]:
135+
co2 = [n for n in co2 if int(n[i]) == 0]
136+
else:
137+
co2 = [n for n in co2 if int(n[i]) == 1]
138+
139+
if len(oxygen) != 1 or len(co2) != 1:
140+
print("error")
141+
142+
oxygen = int("".join(map(str, oxygen)), 2)
143+
co2 = int("".join(map(str, co2)), 2)
144+
145+
puzzle_actual_result = (oxygen, co2, oxygen * co2)[2]
146+
147+
# -------------------------------- Outputs / results --------------------------------- #
148+
149+
print("Case :", case_to_test, "- Part", part_to_test)
150+
print("Expected result : " + str(puzzle_expected_result))
151+
print("Actual result : " + str(puzzle_actual_result))
152+
# Date created: 2021-12-03 08:08:06.750713
153+
# Part 1: 2021-12-03 08:14:39
154+
# Part 2: 2021-12-03 08:25:28

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