2020 AoC, Days 1-4
This commit is contained in:
commit
ad04dffb69
9 changed files with 2782 additions and 0 deletions
76
2020/Day 1/Solution.py
Normal file
76
2020/Day 1/Solution.py
Normal file
|
@ -0,0 +1,76 @@
|
|||
target = 2020
|
||||
input_file = "/tmp/aocinput"
|
||||
|
||||
def find_sum_of_two():
|
||||
large_n = []
|
||||
small_n = []
|
||||
|
||||
with open(input_file) as file:
|
||||
line = file.readline()
|
||||
while line and line != "\n":
|
||||
halftarget_count = 0
|
||||
|
||||
number = int(line)
|
||||
if number > target/2:
|
||||
large_n.append(number)
|
||||
elif number == target/2:
|
||||
halftarget_count += 1
|
||||
else:
|
||||
small_n.append(number)
|
||||
|
||||
if halftarget_count == 2:
|
||||
print(f"Found a sum : {target/2}*2 = {target}\nAnswer is {(target/2)**2}")
|
||||
break
|
||||
|
||||
line = file.readline()
|
||||
|
||||
for big_one in large_n:
|
||||
complement = target-big_one
|
||||
if complement in small_n:
|
||||
print(f"Found a sum : {big_one}+{complement} = {target}\nAnswer is {big_one*complement}")
|
||||
return
|
||||
|
||||
print("No sum found that can reach {target}")
|
||||
|
||||
def sum_search(target_sum, inputs):
|
||||
lower = []
|
||||
upper = []
|
||||
|
||||
for num in inputs:
|
||||
if num < target_sum/2:
|
||||
lower.append(num)
|
||||
else:
|
||||
upper.append(num)
|
||||
|
||||
search_list = lower if len(lower) < len(upper) else upper
|
||||
iter_list = upper if len(lower) < len(upper) else lower
|
||||
|
||||
for num in iter_list:
|
||||
if target_sum - num in search_list:
|
||||
return num, target_sum-num
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def find_sum_of_three():
|
||||
data = []
|
||||
subtracted_to_target = []
|
||||
with open(input_file) as file:
|
||||
line = file.readline()
|
||||
while line and line != "\n":
|
||||
data.append(int(line))
|
||||
subtracted_to_target.append((target-data[-1],data[-1]))
|
||||
|
||||
line = file.readline()
|
||||
|
||||
data.sort()
|
||||
|
||||
for sub_target in subtracted_to_target:
|
||||
result = sum_search(sub_target[0],data)
|
||||
if result:
|
||||
print(f"Sum found : {result[0]}+{result[1]}+{sub_target[1]} = {target}\nResult is {result[0]*result[1]*sub_target[1]}")
|
||||
return
|
||||
|
||||
print("No sum found")
|
||||
|
||||
|
200
2020/Day 1/input.txt
Normal file
200
2020/Day 1/input.txt
Normal file
|
@ -0,0 +1,200 @@
|
|||
1322
|
||||
1211
|
||||
1427
|
||||
1428
|
||||
1953
|
||||
1220
|
||||
1629
|
||||
1186
|
||||
1354
|
||||
1776
|
||||
1906
|
||||
1849
|
||||
1327
|
||||
1423
|
||||
401
|
||||
1806
|
||||
1239
|
||||
1934
|
||||
1256
|
||||
1223
|
||||
1504
|
||||
1365
|
||||
1653
|
||||
1706
|
||||
1465
|
||||
1810
|
||||
1089
|
||||
1447
|
||||
1983
|
||||
1505
|
||||
1763
|
||||
1590
|
||||
1843
|
||||
1534
|
||||
1886
|
||||
1842
|
||||
1878
|
||||
1785
|
||||
1121
|
||||
1857
|
||||
1496
|
||||
1696
|
||||
1863
|
||||
1944
|
||||
1692
|
||||
1255
|
||||
1572
|
||||
1767
|
||||
1509
|
||||
1845
|
||||
1479
|
||||
1935
|
||||
1507
|
||||
1852
|
||||
1193
|
||||
1797
|
||||
1573
|
||||
1317
|
||||
1266
|
||||
1707
|
||||
1819
|
||||
925
|
||||
1976
|
||||
1908
|
||||
1571
|
||||
1646
|
||||
1625
|
||||
1719
|
||||
1980
|
||||
1970
|
||||
1566
|
||||
1679
|
||||
1484
|
||||
1818
|
||||
1985
|
||||
1794
|
||||
1699
|
||||
1530
|
||||
1645
|
||||
370
|
||||
1658
|
||||
1345
|
||||
1730
|
||||
1340
|
||||
1281
|
||||
1722
|
||||
1623
|
||||
1148
|
||||
1545
|
||||
1728
|
||||
1325
|
||||
1164
|
||||
1462
|
||||
1893
|
||||
1736
|
||||
160
|
||||
1543
|
||||
1371
|
||||
1930
|
||||
1162
|
||||
2010
|
||||
1302
|
||||
1967
|
||||
1889
|
||||
1547
|
||||
1335
|
||||
1416
|
||||
1359
|
||||
1622
|
||||
1682
|
||||
1701
|
||||
1939
|
||||
1697
|
||||
1436
|
||||
1367
|
||||
1119
|
||||
1741
|
||||
1466
|
||||
1997
|
||||
1856
|
||||
1824
|
||||
1323
|
||||
1478
|
||||
1963
|
||||
1832
|
||||
1748
|
||||
1260
|
||||
1244
|
||||
1834
|
||||
1990
|
||||
1567
|
||||
1147
|
||||
1588
|
||||
1694
|
||||
1487
|
||||
1151
|
||||
1347
|
||||
1315
|
||||
1502
|
||||
546
|
||||
730
|
||||
1742
|
||||
1869
|
||||
1277
|
||||
1224
|
||||
1169
|
||||
1708
|
||||
1661
|
||||
174
|
||||
1207
|
||||
1801
|
||||
1880
|
||||
1390
|
||||
1747
|
||||
1215
|
||||
1684
|
||||
1498
|
||||
1965
|
||||
1933
|
||||
1693
|
||||
1129
|
||||
1578
|
||||
1189
|
||||
1251
|
||||
1727
|
||||
1440
|
||||
1178
|
||||
746
|
||||
1564
|
||||
944
|
||||
1822
|
||||
1225
|
||||
1523
|
||||
1575
|
||||
1185
|
||||
37
|
||||
1866
|
||||
1766
|
||||
1737
|
||||
1800
|
||||
1633
|
||||
1796
|
||||
1161
|
||||
1932
|
||||
1583
|
||||
1395
|
||||
1288
|
||||
1991
|
||||
229
|
||||
1875
|
||||
1540
|
||||
1876
|
||||
1191
|
||||
1858
|
||||
1713
|
||||
1725
|
||||
1955
|
||||
1250
|
||||
1987
|
||||
1724
|
32
2020/Day 2/Solution.py
Normal file
32
2020/Day 2/Solution.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
input_file = "/tmp/aocinput"
|
||||
|
||||
def letter_count():
|
||||
valid_passwords = 0
|
||||
|
||||
with open(input_file) as passwords:
|
||||
line = passwords.readline()
|
||||
while line and line != "\n":
|
||||
elements = line.split(" ")
|
||||
min_req, max_req = elements[0].split("-")
|
||||
password_count = elements[2].count(elements[1][0])
|
||||
if int(min_req) <= password_count <= int(max_req):
|
||||
valid_passwords += 1
|
||||
|
||||
line = passwords.readline()
|
||||
|
||||
print(f"There are {valid_passwords} valid passwords")
|
||||
|
||||
def letter_position():
|
||||
valid_passwords = 0
|
||||
|
||||
with open(input_file) as passwords:
|
||||
line = passwords.readline()
|
||||
while line and line != "\n":
|
||||
elements = line.split(" ")
|
||||
first_pos, second_pos = elements[0].split("-")
|
||||
if (elements[2][int(first_pos) - 1] == elements[1][0]) ^ (elements[2][int(second_pos) - 1] == elements[1][0]):
|
||||
valid_passwords += 1
|
||||
|
||||
line = passwords.readline()
|
||||
|
||||
print(f"There are {valid_passwords} valid passwords")
|
1000
2020/Day 2/input.txt
Normal file
1000
2020/Day 2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
36
2020/Day 3/Solution.py
Normal file
36
2020/Day 3/Solution.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
input_file = "/tmp/aocinput"
|
||||
|
||||
map_table = []
|
||||
|
||||
with open(input_file) as slope_map:
|
||||
line = slope_map.readline()
|
||||
while line and line != "\n":
|
||||
map_table.append(line)
|
||||
line = slope_map.readline()
|
||||
|
||||
def check_for_trees(direction=(3,1)):
|
||||
x_pos = 0
|
||||
y_pos = 0
|
||||
|
||||
width = len(map_table[0])
|
||||
height = len(map_table)
|
||||
|
||||
tree_count = 0
|
||||
|
||||
while y_pos < height-1:
|
||||
x_pos,y_pos = ((x_pos+direction[0])%(width-1),y_pos+direction[1])
|
||||
if map_table[y_pos][x_pos] == "#":
|
||||
tree_count += 1
|
||||
|
||||
print(f"We came close to {tree_count} trees by going {direction}.")
|
||||
|
||||
return tree_count
|
||||
|
||||
def check_multiple_directions(directions):
|
||||
result = 1
|
||||
for direction in directions:
|
||||
result *= check_for_trees(direction)
|
||||
|
||||
print(f"Result is {result}.")
|
||||
|
||||
check_multiple_directions([(1,1),(3,1),(5,1),(7,1),(1,2)])
|
323
2020/Day 3/input.txt
Normal file
323
2020/Day 3/input.txt
Normal file
|
@ -0,0 +1,323 @@
|
|||
.#......#..####.....#..#.......
|
||||
#.#...#...#..#.#...#.#...##.##.
|
||||
#.#....#..........#...##.....##
|
||||
#.#.#.....##......#.#.......###
|
||||
..#..###....#.#....#.#.#..#....
|
||||
.......#.#....##..##...#...#...
|
||||
..#..#..#..###.......#.....#.#.
|
||||
.#.......#...##...##.##......##
|
||||
#.#.##..##.#..#....#..###..#.#.
|
||||
#.....#.#.........#.....##.#.#.
|
||||
..#.#....##..#...#...##........
|
||||
......#....#..##.#.#......###..
|
||||
.......#.......#......##...#...
|
||||
.##.....#.......#...###.....##.
|
||||
.#...#.##..##.#..##....#.......
|
||||
..#......##...#..#...#.#.##.###
|
||||
.##.##.....##....#..#......#.#.
|
||||
.#.....#..###..#.##.#.....##.#.
|
||||
......##..........#..........#.
|
||||
.##....#.....#..##.#..#.#..###.
|
||||
..##.......#....#...##...#..#..
|
||||
.##...#.....#.###.#.#..#...#.#.
|
||||
.....##.#.##..##...#...........
|
||||
..#..###.##.#.#.###...###..#.#.
|
||||
.#........#..#.#........#.#...#
|
||||
....##.......#....#.#.##.#.....
|
||||
....##........######..###..#.#.
|
||||
#.#.#............#.......#..#..
|
||||
...##...#.##.....#.#..#......#.
|
||||
......#.##.#....##..#.#..###...
|
||||
##.....#.#....#....#.##.#.###..
|
||||
#..#..#..##.#..##.##.##.#.##...
|
||||
.###.####..#..#........#.....##
|
||||
.......##..#.......#...........
|
||||
.##...#............#.#.##...#..
|
||||
....##.....#...##..#..#.#..###.
|
||||
...#.....#####.#..#...##....##.
|
||||
#.....#.#.#....##.......##.#.#.
|
||||
......#.#..#.##.#######......#.
|
||||
#.##...##....#..###.#.......#..
|
||||
.....##...#....#...#....##.##.#
|
||||
....###......#...###..#......##
|
||||
..#...##..##.######..#.#......#
|
||||
......##....#....##..#......##.
|
||||
.#...#..##..#.###.#......#....#
|
||||
##....##..#..####.#.....#...#..
|
||||
.#.......#...#.......##......#.
|
||||
......#...#...#........#.......
|
||||
.#........#.###...#..####.#..#.
|
||||
##...#.#............#.....###..
|
||||
.....###.#.##...........###..#.
|
||||
.#.#...#.....#.#.##..##...####.
|
||||
..##.......#..#.##.#....#.....#
|
||||
.#..#.#..####.....###.#.....#..
|
||||
..#..###.....####..#.##.#.#.##.
|
||||
.###..#.....#......#...####....
|
||||
...#.#..#.#..#...#...#....##.##
|
||||
..###....#.##.....#..........#.
|
||||
###...#####......##............
|
||||
..###.....#........##.#...#..#.
|
||||
..##.##.#.....##........##..#.#
|
||||
##..#.#...#.#..#..###.#....#..#
|
||||
....#..#.#.....#..#####...#....
|
||||
....#.........#......##.##.....
|
||||
.#...####.##......##..##.#..#.#
|
||||
...#...#.##..#...##..###...#...
|
||||
###...#.....#.##.###.###..#.#..
|
||||
..#......#.###.....#..##.#...#.
|
||||
#.....##.########...#####....#.
|
||||
........##..#..##..##.#........
|
||||
....#.######....##..#..#.##..#.
|
||||
#.......#..##..#..#.#.#..##.##.
|
||||
...#.#..#..#.......#......###.#
|
||||
.#.#..#.#..#.##.#.............#
|
||||
#....#.##.#.#.....#..#.#..#....
|
||||
...###..#...#....#.........#.#.
|
||||
.#..#.....##..#.#..#.#.......#.
|
||||
..#...##...#......#......####..
|
||||
....#..#.......#.......#.#..#..
|
||||
#...#..#...........#.#..#.....#
|
||||
#...#.#.......#...#....###....#
|
||||
.#..#.#.##....#......#........#
|
||||
..#...#..##..#..#..#..#...#.#..
|
||||
..#.#.........#....#....##.....
|
||||
##.....##.#.#.#.........##.....
|
||||
.##...#.##...........#...#...##
|
||||
.##..##.#.#..........##..##....
|
||||
#....#....#.#...#.#..#....#.#..
|
||||
####....##.....#..##.###.......
|
||||
#..#....#......##.#.#....#.....
|
||||
.....#....#.###.##.........###.
|
||||
#.......#.####..#..#..##.......
|
||||
##.#.......#..##..#....#..#.#..
|
||||
..###...#.#...#.....##.##.####.
|
||||
....#...#.#....#..#..#.....#.##
|
||||
#.....##.#.#..#.##..#..##......
|
||||
................###..#....##...
|
||||
..#.##.....#..........##.#...#.
|
||||
..#.#..#.#....#.#.#..#..#..#.#.
|
||||
#...#..##.#.#...#..#...#..#....
|
||||
#..#.#.........#..###........#.
|
||||
.#...#.............#..###..#..#
|
||||
#.........#.#..#...#.#.....#..#
|
||||
....#..#..#.#.#...#...#.....##.
|
||||
##...###.#.####..#......#...#..
|
||||
..#..##...#.#......#.#.......#.
|
||||
#......###....##.#.##..........
|
||||
#####....###..#...............#
|
||||
##.#...####....#....#...#....#.
|
||||
.#.......#..#.....#...#.....###
|
||||
...#..#.#.#....##......##...#..
|
||||
...#.....#...#.##.#..#.#....#..
|
||||
#...###....#...#.#....#........
|
||||
.#.......#........#...##.##.##.
|
||||
.....#....#...##.....##...###.#
|
||||
....#....#.#..#...##.##.##.....
|
||||
.......#............#...#.#..#.
|
||||
.#............#.....##.......#.
|
||||
........#....#....##......##.##
|
||||
.......##..#.#..#.##..###..##.#
|
||||
#..##..##.........####.#.###...
|
||||
#....#..#...##...#.............
|
||||
#...#...###..........##..#..#..
|
||||
....#...#..#.....##...#........
|
||||
#.....#......#.#.....#...#..#..
|
||||
..#.....#.....#....#..#........
|
||||
..#..#.....#.#.........#..###..
|
||||
................###..#.#....#..
|
||||
#.....#.....#.#.#.#.#..#...#.#.
|
||||
#....#....#.#..........#.#....#
|
||||
....#..#......#..##.#...##.....
|
||||
..#.#...#.####....#.#..#.#..#..
|
||||
.........##......#.....##......
|
||||
##.#.###.#.....#.....####.#..#.
|
||||
.....#.....#..#....#..###.#....
|
||||
##..#.#...#.##....#....#.......
|
||||
.....#......#.#...##..#.#......
|
||||
....##..#...#...##..##.#....#.#
|
||||
............#..........##.#....
|
||||
##..#..#.##..##..#.#....#.#.#..
|
||||
.......#.#...#...#.#...#..#....
|
||||
#....#.#...#...#........#..#...
|
||||
...........#.......#...##..###.
|
||||
.#..##......#.##.........##..#.
|
||||
...#...#...###.#.##....##.#..#.
|
||||
#...#..#.#.#.....##..#.......#.
|
||||
.##..#.###.##......#.#....#.#.#
|
||||
..#....#.......#..#..#.#.#.##..
|
||||
#...#...###...###.........#....
|
||||
.#.#...#.....##.#.#..#....#.##.
|
||||
.........#.#.##.....#.#.###....
|
||||
...#.#...#......#...####......#
|
||||
...##..##....##......##...###..
|
||||
###...#..#.......##.....#....#.
|
||||
...#..#..#..###...##.##..#..#..
|
||||
...#......#......##..#.#.##..#.
|
||||
...#.........#....#.#....#.#...
|
||||
##................#..#.#.....#.
|
||||
....#.##...#..#.##...##.#.....#
|
||||
......#..##.##..###.#..#.##.##.
|
||||
.#.#...###.....###.....##...###
|
||||
.##.....#.#.#..#..###..#..#..#.
|
||||
#.......#..#..#....##.....#....
|
||||
...#.#.##..#..#......##.##...#.
|
||||
....##.#......#...#..#..#......
|
||||
.####.#..#.....#..##.#...##..##
|
||||
..#..#...#..........###..#....#
|
||||
.#.#.##.##...#............#....
|
||||
........##..##......#.##..#.###
|
||||
...#.#....###......##.......#..
|
||||
..##...#...#.#..#.....#.....#..
|
||||
##..#...###..#..#.#.#...#...#..
|
||||
.....#..#....##.....##.....###.
|
||||
....##...###.#..#.#....##..#..#
|
||||
#......#...#....#......#...##..
|
||||
....#.##...#.#......#.#.##...#.
|
||||
.......#.....#...#####...#.#...
|
||||
...#.....##.#............#.....
|
||||
...#.#........#.#.#..#.........
|
||||
....###......#.#.#..#.####.#..#
|
||||
#.....#.#.#.....#.#.#.....#..#.
|
||||
..##.##......#...#.#...........
|
||||
###..###....#.#####......###...
|
||||
..##..............##.#.#....#.#
|
||||
#..#...#..........#..#.#.#..###
|
||||
##.###............#....#.#...#.
|
||||
#.#..#.#..##.#.#....#...#......
|
||||
#....#...#..##.....#..#.#..###.
|
||||
..#.....#.#....#.#..#.##.#..##.
|
||||
...##...#.#.##...#....###....#.
|
||||
......###.####.......#..#.#.#.#
|
||||
.#..............##........#....
|
||||
...##.##...##....#..#.......#..
|
||||
.....#.....#....###...#..#..#.#
|
||||
.#.....#..#.....#......#.....##
|
||||
#.#.##.#..#..#.....#.##..###...
|
||||
..#......#...##.###..#.#...#..#
|
||||
......#.....#...##......#......
|
||||
##.#........#..........#.....#.
|
||||
#........##.#............##....
|
||||
...#......##...#.#.....##......
|
||||
...##.......#....#.#..#.#.###..
|
||||
..#....##..##.##.....###....#..
|
||||
..#...#.#...#.....#..........#.
|
||||
......#...#...#.#.##.#...#.#.#.
|
||||
.#...#......#.##........#......
|
||||
.##.##..#....#...#.#...##......
|
||||
#..#......#.#...........#....#.
|
||||
....##.#....#...#..#....#.#..##
|
||||
#....##.##....#.#..##.#........
|
||||
.##.##.#....##.....#..#....#..#
|
||||
...#...#.....###.#.##..........
|
||||
....#...#....##.......###......
|
||||
#.........#......#.#.......#...
|
||||
#..........#..##..#.#..........
|
||||
.....#.......#..##.##....##...#
|
||||
........................#.#....
|
||||
#..#.........#.............#..#
|
||||
#..#.....#.......#....#....#.#.
|
||||
..##..##.......##....#...#.....
|
||||
.##......#..##......#.###......
|
||||
...#.#........#.......##..###..
|
||||
..##...###.###......#...#....##
|
||||
#...#...#.....###.#.#.#..#.....
|
||||
#....#.........#..##...#...##..
|
||||
#..###..#.#.#.##.#..#.#....#.##
|
||||
#...#.#.....#.###.#.......#....
|
||||
..##..#..#....#.#...........#.#
|
||||
#.........#.#......#...##......
|
||||
.######......#..#....#.#.#....#
|
||||
##..#.#..####.###.........#....
|
||||
###########.....##.##...#..#...
|
||||
#...##.#.#....#.#....#......#..
|
||||
...#..##..#..##..#......#....#.
|
||||
.#....#...#....#.#..##....##...
|
||||
#..#.#............#....#.#...#.
|
||||
...#...#..#.#.##......#..#.#...
|
||||
#.#...##.....#..#.##......####.
|
||||
.#.#..##..#.....#.#..#.##......
|
||||
#.#.##......##.....#..#.#..#...
|
||||
#..##...#.##.#.......#.##......
|
||||
..#.......#.#.#...##..##...#...
|
||||
.#...#..#..#.#.........#..##...
|
||||
#..#.......#....#.#...#.###...#
|
||||
.......#..#.......##.#.#...#.#.
|
||||
.#.................###.#..###..
|
||||
..........#.#.....##..#####...#
|
||||
#......#.#..##.#.#...#.##.#....
|
||||
#......#.#..##.##.#...#....#...
|
||||
....#..#......#....#....#######
|
||||
.#...#......#....###......#.###
|
||||
#.#....#.#...#.###......#..#..#
|
||||
.###......#.#...#.####.#..####.
|
||||
######.#.....###.#...#.#.....#.
|
||||
.#.###....#..#.#.....#.....####
|
||||
.......###.#.........#..#......
|
||||
#...#.....##.#......####.......
|
||||
..#.#..##.#.#...#...#..##..##..
|
||||
.....#...##.....#...##......##.
|
||||
##..#..#.##..#.#......#.....#..
|
||||
##.........#.#.##.#..#.#....#.#
|
||||
.#........###...#.........#....
|
||||
...#..#.#..#....####...........
|
||||
#.#....#..##..####.#...#.##....
|
||||
.#.....#.......#..........#..##
|
||||
...#.......#...###..#.....#..##
|
||||
.........#.###.#..##...#.##...#
|
||||
.#..........##..####...#..#.#.#
|
||||
.#...##...#............##...#.#
|
||||
...#....#.#..........#.#..#.#..
|
||||
.#.#...##....##.#.#.#....#.....
|
||||
....#..#.....#.#..#.#..#.##.###
|
||||
.....#.#.....#..#......#.#.#...
|
||||
.....#.#.#..###..#.#..###...#..
|
||||
#.......####...#.#..#......##.#
|
||||
....#..#..###......###.##....#.
|
||||
##.....#.....#.............#..#
|
||||
#..#..#...##.....##..#..#.#....
|
||||
.....#.#.###...#...............
|
||||
#.#.#.....#.#..#.#...#.......#.
|
||||
..##.##............#....#..##..
|
||||
#....##...#.....#.###...#.#....
|
||||
#...##.#.........#...#....#....
|
||||
##.##.#...#.#...###..#....##..#
|
||||
....#....##..#..#.......#...##.
|
||||
.#...#...#..#.....#..###.#..#.#
|
||||
....#..###......#....##....#...
|
||||
#.#.....#....##.#..#.#...###...
|
||||
.......#............#......#...
|
||||
.##..#.###.#.............###...
|
||||
..##...##.#.#.#.....#........##
|
||||
....#.###....#..#..#...#...#..#
|
||||
.....#...#...#..#....#.....##..
|
||||
###.#.#.....#......####.....#..
|
||||
#.#.###............#......#....
|
||||
..#.....#..#..#..#....#......#.
|
||||
#...######...#....#.##...##.#.#
|
||||
##.#.#.#..##......##.#..#.#...#
|
||||
............#.#..#.##....#.....
|
||||
......#............#.#...#..#.#
|
||||
.#..##...##..#.#.#..###.....##.
|
||||
#.###.#...........#...#....#...
|
||||
....##.....#...##...#...###.#.#
|
||||
.####.#.#.....#.#..#.#.##......
|
||||
.#...##......###...#..##..#.#..
|
||||
.#......#...#....##.....##..#..
|
||||
..........##.....###.##.#...#.#
|
||||
.#........##.#..............#..
|
||||
#...###..#...#.....#....#.....#
|
||||
...#......#..#...#...#..###.#..
|
||||
.#...##..#........#.......#.#..
|
||||
.#.#.##.........##.##......#..#
|
||||
#...#.#.#...#.....#.#...#.#..#.
|
||||
#.#..#...#...#...##..........#.
|
||||
.#...........#....#..#.#..#.#..
|
||||
#.......#......#..#...#........
|
||||
.....#..#...##..###..##........
|
||||
......#...#.....#..#.#.#....##.
|
||||
....##..##..##....###.##.......
|
||||
.#........##.#.#...#..#........
|
||||
.....##...##...#......#..#...#.
|
||||
..#.....#....###.#..##....#..#.
|
||||
......#..#...####.#.....##.####
|
78
2020/Day 4/Solution.py
Normal file
78
2020/Day 4/Solution.py
Normal file
|
@ -0,0 +1,78 @@
|
|||
import re
|
||||
|
||||
input_file = "/tmp/aocinput"
|
||||
|
||||
height_re = re.compile(r'^([0-9]+)(in|cm)$')
|
||||
passport_id_re = re.compile(r'^[0-9]{9}$')
|
||||
color_re = re.compile(r'^#[0-9a-f]{6}$')
|
||||
|
||||
def check_limits(to_test, min_valid, max_valid):
|
||||
return False if (to_test < min_valid or to_test > max_valid) else True
|
||||
|
||||
def validate_passport(record):
|
||||
for field in record:
|
||||
if field[0] == "byr":
|
||||
if not check_limits(int(field[1]), 1920, 2002):
|
||||
return False
|
||||
elif field[0] == "iyr":
|
||||
if not check_limits(int(field[1]), 2010, 2020):
|
||||
return False
|
||||
elif field[0] == "eyr":
|
||||
if not check_limits(int(field[1]), 2020, 2030):
|
||||
return False
|
||||
elif field[0] == "hgt":
|
||||
regex_match = height_re.match(field[1])
|
||||
if not regex_match:
|
||||
return False
|
||||
if regex_match.groups()[1] == "cm" and not check_limits(int(regex_match.groups()[0]), 150,193):
|
||||
return False
|
||||
elif regex_match.groups()[1] == "in" and not check_limits(int(regex_match.groups()[0]),59,76):
|
||||
return False
|
||||
elif field[0] == "hcl":
|
||||
regex_match = color_re.match(field[1])
|
||||
if not regex_match:
|
||||
return False
|
||||
elif field[0] == "ecl":
|
||||
if field[1] not in ["amb","blu","brn","gry","grn","hzl","oth"]:
|
||||
return False
|
||||
elif field[0] == "pid":
|
||||
regex_match = passport_id_re.match(field[1])
|
||||
if not regex_match:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
records = [[]]
|
||||
|
||||
valid_passports = 0
|
||||
|
||||
with open(input_file) as passports:
|
||||
line = passports.readline()
|
||||
current_index = 0
|
||||
while line:
|
||||
if line == "\n":
|
||||
records.append([])
|
||||
current_index += 1
|
||||
line = passports.readline()
|
||||
continue
|
||||
|
||||
for raw_entry in line.rstrip().split(" "):
|
||||
records[current_index].append(tuple(raw_entry.split(":")))
|
||||
|
||||
line = passports.readline()
|
||||
|
||||
for record in records:
|
||||
if len(record) == 8:
|
||||
if validate_passport(record):
|
||||
valid_passports += 1
|
||||
if len(record) == 7:
|
||||
is_north_pole_credential = True
|
||||
for field in record:
|
||||
if field[0] == "cid":
|
||||
is_north_pole_credential = False
|
||||
break
|
||||
if is_north_pole_credential and validate_passport(record):
|
||||
valid_passports += 1
|
||||
|
||||
print(f"There are {valid_passports} valid passports out of {len(records)}")
|
1029
2020/Day 4/input.txt
Normal file
1029
2020/Day 4/input.txt
Normal file
File diff suppressed because it is too large
Load diff
8
README.md
Normal file
8
README.md
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Advent of Code - trotFunky's pot O' code
|
||||
|
||||
This is the repository where I'll be storing my solutions for the [Advent of Code](https://adventofcode.com) event run by [Eric Wastl](http://was.tl/).
|
||||
The directory structure is simple : `year/day`, each day containing the input file and at least one file containing my solution(s).
|
||||
|
||||
## Depencies
|
||||
|
||||
Only Python3 for now !
|
Loading…
Add table
Reference in a new issue