mirror of
https://github.com/Noettore/AdventOfCode.git
synced 2025-10-15 11:46:39 +02:00
AoC 2020: day4
Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
200
2020-python/inputs/day_1
Normal file
200
2020-python/inputs/day_1
Normal file
@@ -0,0 +1,200 @@
|
||||
1837
|
||||
1585
|
||||
1894
|
||||
1715
|
||||
1947
|
||||
1603
|
||||
1746
|
||||
1911
|
||||
1939
|
||||
1791
|
||||
1800
|
||||
1479
|
||||
1138
|
||||
1810
|
||||
1931
|
||||
1833
|
||||
1470
|
||||
1882
|
||||
1725
|
||||
1496
|
||||
1890
|
||||
1862
|
||||
1990
|
||||
1958
|
||||
1997
|
||||
1844
|
||||
1524
|
||||
541
|
||||
2001
|
||||
1591
|
||||
1687
|
||||
1941
|
||||
1940
|
||||
1561
|
||||
1813
|
||||
1654
|
||||
1500
|
||||
1575
|
||||
1826
|
||||
2006
|
||||
679
|
||||
1660
|
||||
1679
|
||||
1631
|
||||
2008
|
||||
575
|
||||
1583
|
||||
1883
|
||||
1904
|
||||
1436
|
||||
1650
|
||||
1532
|
||||
1907
|
||||
1803
|
||||
1693
|
||||
1700
|
||||
359
|
||||
1516
|
||||
1625
|
||||
1908
|
||||
1994
|
||||
1910
|
||||
1644
|
||||
1706
|
||||
1781
|
||||
1639
|
||||
1662
|
||||
1712
|
||||
1796
|
||||
1915
|
||||
1550
|
||||
1721
|
||||
1697
|
||||
1917
|
||||
1665
|
||||
1646
|
||||
1968
|
||||
1881
|
||||
1893
|
||||
1468
|
||||
1678
|
||||
1774
|
||||
285
|
||||
1754
|
||||
1856
|
||||
1677
|
||||
1823
|
||||
1802
|
||||
1681
|
||||
1587
|
||||
1767
|
||||
1711
|
||||
1900
|
||||
1983
|
||||
1787
|
||||
1996
|
||||
1726
|
||||
1982
|
||||
1971
|
||||
1553
|
||||
1542
|
||||
1863
|
||||
2002
|
||||
1831
|
||||
1891
|
||||
1555
|
||||
2000
|
||||
1847
|
||||
1783
|
||||
1873
|
||||
1761
|
||||
1742
|
||||
1534
|
||||
1993
|
||||
1898
|
||||
1973
|
||||
1974
|
||||
1597
|
||||
1540
|
||||
1581
|
||||
1864
|
||||
1452
|
||||
1637
|
||||
1649
|
||||
1886
|
||||
1965
|
||||
1460
|
||||
1664
|
||||
1701
|
||||
1647
|
||||
1812
|
||||
1937
|
||||
1902
|
||||
2004
|
||||
1991
|
||||
1718
|
||||
1887
|
||||
1606
|
||||
1748
|
||||
1737
|
||||
1608
|
||||
1641
|
||||
1710
|
||||
1724
|
||||
705
|
||||
1985
|
||||
1571
|
||||
1805
|
||||
131
|
||||
1788
|
||||
1707
|
||||
1513
|
||||
1615
|
||||
1897
|
||||
1476
|
||||
1927
|
||||
1745
|
||||
1926
|
||||
1839
|
||||
1807
|
||||
1955
|
||||
1692
|
||||
1645
|
||||
1699
|
||||
1471
|
||||
1604
|
||||
1830
|
||||
1622
|
||||
1972
|
||||
1866
|
||||
1814
|
||||
1816
|
||||
1855
|
||||
1820
|
||||
1034
|
||||
1673
|
||||
1704
|
||||
1969
|
||||
1580
|
||||
1980
|
||||
1739
|
||||
1896
|
||||
434
|
||||
497
|
||||
1851
|
||||
1933
|
||||
458
|
||||
1521
|
||||
1551
|
||||
1762
|
||||
2010
|
||||
1614
|
||||
1840
|
||||
1747
|
||||
1875
|
||||
1836
|
||||
1895
|
||||
1518
|
||||
1825
|
||||
1987
|
1000
2020-python/inputs/day_2
Normal file
1000
2020-python/inputs/day_2
Normal file
File diff suppressed because it is too large
Load Diff
323
2020-python/inputs/day_3
Normal file
323
2020-python/inputs/day_3
Normal file
@@ -0,0 +1,323 @@
|
||||
...........#..#.#.###....#.....
|
||||
...#..#...........#.#...#......
|
||||
#.....#..#........#...#..##....
|
||||
..#...##.#.....#.............#.
|
||||
#.#..#......#.....#....#.......
|
||||
.....#......#..#....#.....#....
|
||||
.......#.#..............#......
|
||||
.....#...#..........##...#.....
|
||||
...#....#.#...#.#........#...#.
|
||||
..#.........###.......##...##..
|
||||
.#....#...........#........#..#
|
||||
..#.............##.............
|
||||
..#.##.#....#................#.
|
||||
.....##.#.......#....#...#.....
|
||||
......#.#....##................
|
||||
..#..........###..#..#.#..#....
|
||||
....#..............#....##..#.#
|
||||
.#.........#.#....#.#.#....#...
|
||||
..#.....#......##.#....#.......
|
||||
..#.#....#..#.#...##....###....
|
||||
...#......##...#........#.#..#.
|
||||
.##.#.......##....#............
|
||||
...##..#.#............#...#.#..
|
||||
.##...##.#..#..................
|
||||
..#......##......#......##.....
|
||||
.....##...#..#...#.........#...
|
||||
.##.#.....#..#..#.##....##....#
|
||||
..#.#......#.......##..........
|
||||
......................#......##
|
||||
##.#...#.................#.#.#.
|
||||
......#.#..........#.....##.#..
|
||||
#.#......#.....#...........#...
|
||||
.....#...#.......#..#..#.#...#.
|
||||
...........#......#.#...#......
|
||||
....##...##...........#......#.
|
||||
.........#.##..................
|
||||
......#...#....#......##.##...#
|
||||
......#...#.#########......#...
|
||||
.......#.#...#.......#..#......
|
||||
............#...#...#.###...##.
|
||||
...........#..........#...#....
|
||||
...#..#.#................#.#..#
|
||||
..#....#.....#.............#.#.
|
||||
....##......#........#....#....
|
||||
........##...............#....#
|
||||
........#..#...#..............#
|
||||
...#....#.#...#..#...#....#.#.#
|
||||
.........#.......#....##.......
|
||||
#.##..............#.#........##
|
||||
......................###......
|
||||
.........#....##..##....#.#.#..
|
||||
.#...##.#.#...#....##..#.....#.
|
||||
....................#.#......#.
|
||||
.#..#.......................#..
|
||||
..#..#.............#..#....#...
|
||||
...#...#..#...#...#.#..#.##....
|
||||
........#....#...#....#........
|
||||
.#.....#........#.........#...#
|
||||
...#......#..#..#..####..#.....
|
||||
#....#..............#.##.......
|
||||
.#....#.............##...#.....
|
||||
....#...#.##........##......#..
|
||||
##....#...#.......#..#........#
|
||||
....##........................#
|
||||
..................#..#.........
|
||||
..#....#........#..#.......#...
|
||||
#...#..#....#...##...........#.
|
||||
.........#..#..#.#.##..........
|
||||
....#.#..#.....#..#.#.#.#..#.##
|
||||
##................#.##.....#...
|
||||
.#.....###..#..#..#.....#....##
|
||||
...#...........#..........####.
|
||||
.#.....#....#......#.##..#.#...
|
||||
..#...##....#................#.
|
||||
........#.......#......#.#.....
|
||||
....#.#.#.#....#...#......#..#.
|
||||
...........#......#..#.........
|
||||
###...##......##.#..#....##....
|
||||
##....##.........#..#....###...
|
||||
#.#.....#....#......#...#..##..
|
||||
#....##.#..............#.##....
|
||||
.#........#.#.........#...#....
|
||||
......................#......#.
|
||||
........#..#..##.....#..#.#....
|
||||
..#...###.................#..#.
|
||||
...#...#............#..........
|
||||
.##.......#..#.........#....#..
|
||||
.#..............#....#....##...
|
||||
...............##..#.#.......##
|
||||
.#.....#....#...#..#.......#..#
|
||||
#..#.............#....#......#.
|
||||
.....#.#......#.........###..#.
|
||||
.#...#.#...............#....#..
|
||||
#......#.............#.........
|
||||
.#.##.#.####...#..#.##..#.....#
|
||||
.....#......#..#...#.......#...
|
||||
#........###...#.....#..#.....#
|
||||
....#.#.....#...#..........#...
|
||||
...#.#.......#.#......#..#.##..
|
||||
..#..........#.#..#.......#.#..
|
||||
#...#.#..............#...###.#.
|
||||
...#..#...#............###.....
|
||||
..#..#...#.#............#..#...
|
||||
.###.#.....................#..#
|
||||
....#....#..#.....##.##........
|
||||
#....#....#.#..#.........#.....
|
||||
.#.....##....#............##..#
|
||||
#....#.#...#...#..#.#......#...
|
||||
#.....##.....##.....##.#...##..
|
||||
...##...#..#..####.#........#..
|
||||
.........#...#......##..##..#..
|
||||
..#.....###.#..#..####.#.......
|
||||
.......#......#...#..##....#...
|
||||
.#.....#.#.#..#....#...#..##...
|
||||
..........#.#...#...#.#..#.....
|
||||
....#.....#........#.....##..#.
|
||||
..#.#.##.........#...##.....##.
|
||||
.........#.##....#............#
|
||||
............##.....#.......#.#.
|
||||
......#.....#...#..#..###......
|
||||
##.....#.......#...##.#....#...
|
||||
...........##......#..##...#.#.
|
||||
..#.#.#.#...#.......#....#...#.
|
||||
#.............#.....#.#...###..
|
||||
##....#.......#.....#..##.#....
|
||||
...#.......#....#.........##...
|
||||
......#.......#......##.##.....
|
||||
..#......#...#.#........#......
|
||||
....#.#....#.#.##......#.....#.
|
||||
#......#.........#..#....#.....
|
||||
........#..#....##.....#.......
|
||||
#......##....#.##....#......#..
|
||||
..#.......#............##.....#
|
||||
...........#...#...........#...
|
||||
#.......#...#....#....#...#.#.#
|
||||
..###..#........#........#.....
|
||||
..#..###...........#.#......#..
|
||||
.#...........#......#..........
|
||||
.#.......#.....#.......#.......
|
||||
..#......##.#............#....#
|
||||
#..........#.....#.#..#........
|
||||
.....#...##.##.......#......#..
|
||||
..........#.....#........#.#.#.
|
||||
....#......#.....#......#.#....
|
||||
.........#.#.#..#...##....#...#
|
||||
.........#.......#...##...#.#..
|
||||
.##........#...............#...
|
||||
.......#....#...........##.....
|
||||
.........###......#.........#.#
|
||||
......#.......#...#..........#.
|
||||
...#.#..........##......#...#..
|
||||
#.......#.#..........#.........
|
||||
................#..#......#..##
|
||||
.....#...#....#.#.....#........
|
||||
#.....#....#...........#....#..
|
||||
#....#.#..#...##....#...##.#...
|
||||
...#.....#......#.#....#..#..#.
|
||||
..#................#...#.#..##.
|
||||
..........#..............#..#.#
|
||||
.....##.....#..#.###...........
|
||||
....#.#......#.#...........#...
|
||||
.#....#.#.........##.#....#....
|
||||
.#.#........#........###....#..
|
||||
##.#................#...#..#...
|
||||
.......#......##..#.....#..#.#.
|
||||
...#............#......###...##
|
||||
#.#...........#.........#......
|
||||
.....#.#.#.................#...
|
||||
....#..............#...#.#.....
|
||||
...#.#.....##..#...............
|
||||
.#..##...#....##.....###..#....
|
||||
...............#...#...#.#.###.
|
||||
.###....#.....#...#.#......#...
|
||||
...#..#.....#.......#..##.#....
|
||||
...........#..#....##..#...#...
|
||||
...#...#..........#.......##.#.
|
||||
............#.#.......#........
|
||||
....#.........#.....#..........
|
||||
...#.###.##..#...##..####..#..#
|
||||
.#.#...#..#...................#
|
||||
.....#..#.....##..#............
|
||||
....#......#...##..#.##........
|
||||
...#...............#..#.....##.
|
||||
...#......#.........#.#..##....
|
||||
.#....#.##.......#......#......
|
||||
....#.......#....#..........#..
|
||||
#.#.#....###.#.#.............#.
|
||||
..##..###........#.#..#.#..#...
|
||||
......#.#............##.#...###
|
||||
.........#.#....#####.....##...
|
||||
............##......#.#..#.....
|
||||
...#.....#.....###....#........
|
||||
##..........####.##.#.#........
|
||||
....................##.....##.#
|
||||
#.#............#........#......
|
||||
....#...##.....#......#....#...
|
||||
...###.#..##..................#
|
||||
..###......#..............#.#.#
|
||||
.#...#...........#....#........
|
||||
....#............#..#..#.#.....
|
||||
...#.........#.#.........#.####
|
||||
..#...#...#...#...........#....
|
||||
...............#.#......##..#..
|
||||
#....#.#.......#.#..#......#..#
|
||||
........#.#....#..#...#..#...#.
|
||||
...#..#.......#...........#....
|
||||
...........#.......#...........
|
||||
.#......#................#.....
|
||||
....#.#.#...#......#..#...#....
|
||||
................#.#.#....#.....
|
||||
.........................##..#.
|
||||
.#...........#............##...
|
||||
#...............#.....##.#.#..#
|
||||
.........#.......###....#.....#
|
||||
....##...#...#.....#..#........
|
||||
........#.....#..#.#.#...#..#..
|
||||
......#.......#.#.........#.#..
|
||||
#......#............#...#....#.
|
||||
#..##...#..#................#..
|
||||
.##...#...#.....#.##.......#..#
|
||||
.......#.##........##..##......
|
||||
##.#..##...............#.....#.
|
||||
......#....#..##...#......###.#
|
||||
#........##..#....#.#......#...
|
||||
.#......##.#...#.#...#.........
|
||||
.#.#...#..#.............#......
|
||||
.##..........#..........#......
|
||||
.#.....#.....#..............#.#
|
||||
..#.........#..#.#.....#.#....#
|
||||
..#.##..............##...#..###
|
||||
....................#..........
|
||||
......###..#..#...........#....
|
||||
..#..........#.......#...#.....
|
||||
...#......#......#.............
|
||||
....##..............#.#.....#..
|
||||
........#.#......#..#........##
|
||||
.............#...#.#.........##
|
||||
...###...#..........##.......#.
|
||||
.#..........#...##..#.#.....#..
|
||||
##...#.........#...............
|
||||
......#....#....#.....#.....#..
|
||||
..........#....#...#...#..#...#
|
||||
...##....#.#.#..#...##.........
|
||||
#......#.#...##.###...#....#...
|
||||
##.......##.#......##..#...#...
|
||||
......#.............#.##.....##
|
||||
#.......###....####.#...##....#
|
||||
..#...#..#.......#..........#..
|
||||
#.....#..#..#..#.##...###...#..
|
||||
.....##.#..#..#..#.#....#...#..
|
||||
..#...#..................##....
|
||||
....#.#........##..............
|
||||
#...#.......##...#...#.#.......
|
||||
..#...#........##....#.#.......
|
||||
..........###...###...#......#.
|
||||
#.....#..###...##...##..#..#..#
|
||||
..#.....##.....#.......##..#.#.
|
||||
........#........#.........#...
|
||||
.................#....#.......#
|
||||
.......#...#.....#...#.#.......
|
||||
....##...............#...##...#
|
||||
.##...#................#...#...
|
||||
.............#.................
|
||||
.#..#....#....#.#....#.........
|
||||
.#.#..#..........#.......#.....
|
||||
.....##.....##...#..#..........
|
||||
#...#.#.........#......#..#....
|
||||
........#....#...#....#.#.##...
|
||||
....#..#........#...#...#......
|
||||
.#..#.....#.#...#.........#....
|
||||
.#..#..#...........#..#....#...
|
||||
....###.............#..#.......
|
||||
#......#..#..##..........#.#...
|
||||
#..#..#.##..#...#.#.#..........
|
||||
....###......#.##.....#....#...
|
||||
.##..#...#......##.#...........
|
||||
..#..#.......#.....#.##....#.#.
|
||||
.......#.#.#........#....##....
|
||||
..##...#....#...............###
|
||||
#..##..#...........#.#....##...
|
||||
...##..#.....................#.
|
||||
###......#....#....###..#...##.
|
||||
.........##............#..#...#
|
||||
..#..........#...#.#.#......#.#
|
||||
.......#.....##..##......#.##..
|
||||
#..........#.....##.#..........
|
||||
#.......#.#...#...#....#.......
|
||||
#...#.....##.......#.#..#.#.#..
|
||||
.........#.#.#..#..#...#.###...
|
||||
.................##...#....#...
|
||||
###.......#..........##...#....
|
||||
#.#..#.........#....##.#.......
|
||||
......#.#.....#........#.......
|
||||
.......#.#........#......#.#..#
|
||||
..............#..#...##....#..#
|
||||
#...........#...##.....#..#.#..
|
||||
..#....#..#.#.#...#..#....#.#..
|
||||
...##.#.....#..#...##..#.....#.
|
||||
..#.#................#........#
|
||||
......#...#.............#......
|
||||
.##............#....#...#..#...
|
||||
....#...#...........#.......#..
|
||||
.###..#.......#.............#.#
|
||||
.#.#....#.#...........#.#......
|
||||
...#.........#.........#..#....
|
||||
...#..........#.#.....#.#......
|
||||
.....#........#....##......#...
|
||||
..#.#.#......#..#.#......#....#
|
||||
.#.#..#................#.#.....
|
||||
.#.#.........##...#.......#.#.#
|
||||
#..#.....#...#..#...........#..
|
||||
..##......####......#..#....###
|
||||
#.....###....#.#........#..#..#
|
||||
..##.#...#.#..##..........#..#.
|
||||
#.........#.#.............#...#
|
||||
...#.#...#...#.#.#....##.......
|
||||
##.##...#.....#...#...........#
|
||||
....#........#.#.....#.........
|
||||
.................##..#..##...##
|
||||
.....##....#...#...#.....#..#..
|
||||
....#...#........#............#
|
||||
..#...........##....#...#...##.
|
||||
.....#......#.........#..##.#..
|
1102
2020-python/inputs/day_4
Normal file
1102
2020-python/inputs/day_4
Normal file
File diff suppressed because it is too large
Load Diff
43
2020-python/solutions/day_1.py
Normal file
43
2020-python/solutions/day_1.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""AOC Day 1"""
|
||||
|
||||
import pathlib
|
||||
import time
|
||||
|
||||
def read_input(input_path: str) -> list:
|
||||
"""take input file path and return appropriate data structure"""
|
||||
with open(input_path, 'r') as input_file:
|
||||
entries = list()
|
||||
for entry in input_file.readlines():
|
||||
entries.append(int(entry))
|
||||
return entries
|
||||
|
||||
def part1(entries: list) -> int:
|
||||
"""part1 solver take a list of int and return an int"""
|
||||
for x in entries:
|
||||
complement = 2020 - x
|
||||
if complement in entries:
|
||||
return x * complement
|
||||
return None
|
||||
|
||||
def part2(entries: list) -> int:
|
||||
"""part2 solver take a list of int and return an int"""
|
||||
for x, i in enumerate(entries):
|
||||
for y in entries[i:]:
|
||||
complement = 2020 - x - y
|
||||
if complement in entries:
|
||||
return x * y * complement
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
"""main function"""
|
||||
input_path = str(pathlib.Path.cwd()) + "/inputs/" + str(pathlib.Path(__file__).stem)
|
||||
entries = read_input(input_path)
|
||||
start_time = time.time()
|
||||
print("Part 1: %d" % part1(entries))
|
||||
print("Part 2: %d" % part2(entries))
|
||||
end_time = time.time()
|
||||
print("Execution time: %f" % (end_time-start_time))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
49
2020-python/solutions/day_2.py
Normal file
49
2020-python/solutions/day_2.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""AOC Day 2"""
|
||||
|
||||
import pathlib
|
||||
import time
|
||||
import re
|
||||
|
||||
def read_input(input_path: str) -> list:
|
||||
"""take input file path and return appropriate data structure"""
|
||||
with open(input_path, 'r') as input_file:
|
||||
rules = list()
|
||||
for entry in input_file.readlines():
|
||||
splitted = re.split('-| |: ', entry)
|
||||
rule = [int(splitted[0]), int(splitted[1]), splitted[2], splitted[3]]
|
||||
rules.append(rule)
|
||||
return rules
|
||||
|
||||
|
||||
def part1(entries: list) -> int:
|
||||
"""part1 solver take a list of tuples and return an int"""
|
||||
correct_passwords = 0
|
||||
for entry in entries:
|
||||
occurrences = entry[3].count(entry[2])
|
||||
if occurrences in range(entry[0], entry[1]+1):
|
||||
correct_passwords += 1
|
||||
return correct_passwords
|
||||
|
||||
|
||||
def part2(entries: list) -> int:
|
||||
"""part2 solver take a list of tuples and return an int"""
|
||||
correct_passwords = 0
|
||||
for entry in entries:
|
||||
pos_1, pos_2, letter, password = entry[:]
|
||||
if (password[pos_1-1] == letter) != (password[pos_2-1] == letter):
|
||||
correct_passwords += 1
|
||||
return correct_passwords
|
||||
|
||||
def main():
|
||||
"""main function"""
|
||||
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
|
||||
entries = read_input(input_path)
|
||||
start_time = time.time()
|
||||
print("Part 1: %d" % part1(entries))
|
||||
print("Part 2: %d" % part2(entries))
|
||||
end_time = time.time()
|
||||
print("Execution time: %f" % (end_time-start_time))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
59
2020-python/solutions/day_3.py
Normal file
59
2020-python/solutions/day_3.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""AOC Day 3"""
|
||||
|
||||
import pathlib
|
||||
import time
|
||||
|
||||
def read_input(input_path: str) -> list:
|
||||
"""take input file path and return appropriate data structure"""
|
||||
with open(input_path, 'r') as input_file:
|
||||
lines = list()
|
||||
for line in input_file.readlines():
|
||||
lines.append(line.strip())
|
||||
return lines
|
||||
|
||||
|
||||
def slope_tree_check(lines: list, dx: int, dy: int) -> int:
|
||||
"""check how many trees would be encountered with a specific slope"""
|
||||
x_pos = 0
|
||||
y_pos = 0
|
||||
trees_encountered = 0
|
||||
line_length = len(lines[0])
|
||||
while y_pos < len(lines):
|
||||
if lines[y_pos][x_pos] == '#':
|
||||
trees_encountered += 1
|
||||
x_pos = (x_pos + dx) % line_length
|
||||
y_pos += dy
|
||||
return trees_encountered
|
||||
|
||||
def part1(entries: list) -> int:
|
||||
"""part1 solver take a list of strings and return an int"""
|
||||
return slope_tree_check(entries, 3, 1)
|
||||
|
||||
|
||||
def part2(entries: list) -> int:
|
||||
"""part2 solver take a list of tuples and return an int"""
|
||||
slopes = [
|
||||
(1, 1),
|
||||
(3, 1),
|
||||
(5, 1),
|
||||
(7, 1),
|
||||
(1, 2),
|
||||
]
|
||||
prod = 1
|
||||
for slope in slopes:
|
||||
prod *= slope_tree_check(entries, slope[0], slope[1])
|
||||
return prod
|
||||
|
||||
def main():
|
||||
"""main function"""
|
||||
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
|
||||
entries = read_input(input_path)
|
||||
start_time = time.time()
|
||||
print("Part 1: %d" % part1(entries))
|
||||
print("Part 2: %d" % part2(entries))
|
||||
end_time = time.time()
|
||||
print("Execution time: %f" % (end_time-start_time))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
72
2020-python/solutions/day_4.py
Normal file
72
2020-python/solutions/day_4.py
Normal file
@@ -0,0 +1,72 @@
|
||||
"""AOC Day 4"""
|
||||
|
||||
import pathlib
|
||||
import time
|
||||
import re
|
||||
|
||||
def read_input(input_path: str) -> list:
|
||||
"""take input file path and return appropriate data structure"""
|
||||
with open(input_path, 'r') as input_file:
|
||||
data = input_file.read()
|
||||
entries = re.split(r"(?:\r?\n){2,}", data.strip())
|
||||
passports = list()
|
||||
for entry in entries:
|
||||
passport = dict()
|
||||
entry = entry.replace('\n', ' ')
|
||||
data = entry.split(' ')
|
||||
for field in data:
|
||||
key, value = field.split(':')[:2]
|
||||
passport[key] = value
|
||||
passports.append(passport)
|
||||
return passports
|
||||
|
||||
def check_fields(passport: dict) -> bool:
|
||||
"""check if a passport contains all the required fields"""
|
||||
required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
|
||||
for required_field in required_fields:
|
||||
if required_field not in passport.keys():
|
||||
return False
|
||||
elif passport[required_field] == None or passport[required_field] == '':
|
||||
return False
|
||||
return True
|
||||
|
||||
def check_data(passport: dict) -> bool:
|
||||
"""check if all passport fields contains correct data"""
|
||||
return (1920 <= int(passport["byr"]) <= 2002 and
|
||||
2010 <= int(passport["iyr"]) <= 2020 and
|
||||
2020 <= int(passport["eyr"]) <= 2030 and
|
||||
((passport["hgt"].endswith("cm") and 150 <= int(passport["hgt"][:-2]) <= 193) or (passport["hgt"].endswith("in") and 59 <= int(passport["hgt"][:-2]) <= 76)) and
|
||||
re.match(r"^#[0-9a-f]{6}$", passport["hcl"]) and
|
||||
passport["ecl"] in ("amb", "blu", "brn", "gry", "grn", "hzl", "oth") and
|
||||
re.match(r"^[0-9]{9}$", passport["pid"]))
|
||||
|
||||
def part1(entries: list) -> int:
|
||||
"""part1 solver take a list of strings and return an int"""
|
||||
valid_passports = 0
|
||||
for passport in entries:
|
||||
if check_fields(passport):
|
||||
valid_passports += 1
|
||||
return valid_passports
|
||||
|
||||
|
||||
def part2(entries: list) -> int:
|
||||
"""part2 solver take a list of tuples and return an int"""
|
||||
valid_passports = 0
|
||||
for passport in entries:
|
||||
if check_fields(passport) and check_data(passport):
|
||||
valid_passports += 1
|
||||
return valid_passports
|
||||
|
||||
def main():
|
||||
"""main function"""
|
||||
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
|
||||
start_time = time.time()
|
||||
entries = read_input(input_path)
|
||||
print("Part 1: %d" % part1(entries))
|
||||
print("Part 2: %d" % part2(entries))
|
||||
end_time = time.time()
|
||||
print("Execution time: %f" % (end_time-start_time))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user