Skip to content

Commit 1bafc16

Browse files
committed
Added days 2020-02 and 2020-03
1 parent 63af5e1 commit 1bafc16

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

2020/02-Password Philosophy.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools, collections
3+
4+
from compass import *
5+
6+
7+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
8+
def lmap(func, *iterables):
9+
return list(map(func, *iterables))
10+
11+
12+
def ints(s: str):
13+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
14+
15+
16+
def positive_ints(s: str):
17+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
18+
19+
20+
def floats(s: str):
21+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
22+
23+
24+
def positive_floats(s: str):
25+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
26+
27+
28+
def words(s: str):
29+
return re.findall(r"[a-zA-Z]+", s)
30+
31+
32+
test_data = {}
33+
34+
test = 1
35+
test_data[test] = {
36+
"input": """1-3 a: abcde
37+
1-3 b: cdefg
38+
2-9 c: ccccccccc""",
39+
"expected": ["2", "1"],
40+
}
41+
42+
test = "WD"
43+
input_file = os.path.join(
44+
os.path.dirname(__file__),
45+
"Inputs",
46+
os.path.basename(__file__).replace(".py", ".WD.txt"),
47+
)
48+
test_data[test] = {
49+
"input": open(input_file, "r+").read(),
50+
"expected": ["Unknown", "Unknown"],
51+
}
52+
53+
test = "Twitter"
54+
input_file = os.path.join(
55+
os.path.dirname(__file__),
56+
"Inputs",
57+
os.path.basename(__file__).replace(".py", ".Twitter.txt"),
58+
)
59+
test_data[test] = {
60+
"input": open(input_file, "r+").read(),
61+
"expected": ["447", "Unknown"],
62+
}
63+
64+
# -------------------------------- Control program execution ------------------------- #
65+
66+
case_to_test = "Twitter"
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+
if part_to_test == 1:
79+
valid_password = 0
80+
for string in puzzle_input.split("\n"):
81+
_, letter, password = string.split(" ")
82+
min_c, max_c = positive_ints(string)
83+
if (
84+
collections.Counter(password)[letter[:1]] >= min_c
85+
and collections.Counter(password)[letter[:1]] <= max_c
86+
):
87+
valid_password = valid_password + 1
88+
89+
puzzle_actual_result = valid_password
90+
91+
92+
else:
93+
valid_password = 0
94+
for string in puzzle_input.split("\n"):
95+
_, letter, password = string.split(" ")
96+
letter = letter[:1]
97+
min_c, max_c = positive_ints(string)
98+
if password[min_c - 1] == letter:
99+
if password[max_c - 1] == letter:
100+
pass
101+
else:
102+
valid_password = valid_password + 1
103+
else:
104+
if password[max_c - 1] == letter:
105+
valid_password = valid_password + 1
106+
puzzle_actual_result = valid_password
107+
108+
109+
# -------------------------------- Outputs / results --------------------------------- #
110+
111+
print("Case :", case_to_test, "- Part", part_to_test)
112+
print("Expected result : " + str(puzzle_expected_result))
113+
print("Actual result : " + str(puzzle_actual_result))

2020/03-Toboggan Trajectory.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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": """..##.......
38+
#...#...#..
39+
.#....#..#.
40+
..#.#...#.#
41+
.#...##..#.
42+
..#.##.....
43+
.#.#.#....#
44+
.#........#
45+
#.##...#...
46+
#...##....#
47+
.#..#...#.#""",
48+
"expected": ["7", "336"],
49+
}
50+
51+
test = "real"
52+
input_file = os.path.join(
53+
os.path.dirname(__file__),
54+
"Inputs",
55+
os.path.basename(__file__).replace(".py", ".txt"),
56+
)
57+
test_data[test] = {
58+
"input": open(input_file, "r+").read(),
59+
"expected": ["153", "2421944712"],
60+
}
61+
62+
# -------------------------------- Control program execution ------------------------- #
63+
64+
case_to_test = 1
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+
if part_to_test == 1:
77+
maze = grid.Grid()
78+
maze.text_to_dots(puzzle_input)
79+
position = 0
80+
width, height = maze.get_size()
81+
82+
nb_trees = 0
83+
while position.imag > -height:
84+
if maze.dots[position].terrain == "#":
85+
nb_trees = nb_trees + 1
86+
position = position + south + east * 3
87+
position = position.real % width + 1j * position.imag
88+
89+
puzzle_actual_result = nb_trees
90+
91+
92+
else:
93+
maze = grid.Grid()
94+
maze.text_to_dots(puzzle_input)
95+
position = 0
96+
width, height = maze.get_size()
97+
98+
nb_trees = 0
99+
score = 1
100+
for direction in [1 - 1j, 3 - 1j, 5 - 1j, 7 - 1j, 1 - 2j]:
101+
while position.imag > -height:
102+
if maze.dots[position].terrain == "#":
103+
nb_trees = nb_trees + 1
104+
position = position + direction
105+
position = position.real % width + 1j * position.imag
106+
score = score * nb_trees
107+
nb_trees = 0
108+
position = 0
109+
110+
puzzle_actual_result = score
111+
112+
113+
# -------------------------------- Outputs / results --------------------------------- #
114+
115+
print("Case :", case_to_test, "- Part", part_to_test)
116+
print("Expected result : " + str(puzzle_expected_result))
117+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)