porting over AOC from previous years to a monorepo.
This commit is contained in:
commit
84c4cf9991
194 changed files with 30104 additions and 0 deletions
54
2020/days/01/day1.py
Executable file
54
2020/days/01/day1.py
Executable file
|
|
@ -0,0 +1,54 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
import pathlib
|
||||
import sys
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
|
||||
def parse(puzzle_input):
|
||||
"""Parse input"""
|
||||
return [int(string) for string in puzzle_input.splitlines()]
|
||||
|
||||
def part1(data:list[int]):
|
||||
"""Solve part 1"""
|
||||
data.sort()
|
||||
while True:
|
||||
if len(data)<2:
|
||||
raise ValueError('no match found')
|
||||
s = data[0]+data[-1]
|
||||
if s>2020:
|
||||
data.pop(-1)
|
||||
elif s<2020:
|
||||
data.pop(0)
|
||||
else:
|
||||
return data[0]*data[-1]
|
||||
|
||||
def part2(data):
|
||||
"""Solve part 2"""
|
||||
data.sort()
|
||||
while True:
|
||||
if len(data)<3:
|
||||
raise ValueError('no match found')
|
||||
if data[0]+data[1]+data[-1]>2020:
|
||||
data.pop(-1)
|
||||
elif data[0]+data[-1]+data[-2]<2020:
|
||||
data.pop(0)
|
||||
elif data[0]+data[1]+data[-1]==2020:
|
||||
return data[0]*data[1]*data[-1]
|
||||
else:
|
||||
return data[0]*data[-1]*data[-2]
|
||||
|
||||
def solve(puzzle_input):
|
||||
"""Solve the puzzle for the given input"""
|
||||
data = parse(puzzle_input)
|
||||
solution1 = part1(data.copy())
|
||||
solution2 = part2(data.copy())
|
||||
|
||||
return solution1, solution2
|
||||
|
||||
if __name__ == "__main__":
|
||||
for path in sys.argv[1:]:
|
||||
print(f"{path}:")
|
||||
puzzle_input = pathlib.Path(path).read_text().strip()
|
||||
solutions = solve(puzzle_input)
|
||||
print("\n".join(str(solution) for solution in solutions))
|
||||
6
2020/days/01/example1
Normal file
6
2020/days/01/example1
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
1721
|
||||
979
|
||||
366
|
||||
299
|
||||
675
|
||||
1456
|
||||
200
2020/days/01/input
Normal file
200
2020/days/01/input
Normal file
|
|
@ -0,0 +1,200 @@
|
|||
1772
|
||||
1065
|
||||
1827
|
||||
1671
|
||||
1181
|
||||
1915
|
||||
1657
|
||||
1632
|
||||
1053
|
||||
1546
|
||||
1039
|
||||
1388
|
||||
1698
|
||||
1174
|
||||
1275
|
||||
1250
|
||||
1988
|
||||
1078
|
||||
1075
|
||||
1958
|
||||
1617
|
||||
1387
|
||||
1543
|
||||
1965
|
||||
1867
|
||||
1771
|
||||
1755
|
||||
1331
|
||||
1677
|
||||
1935
|
||||
1488
|
||||
911
|
||||
1001
|
||||
1516
|
||||
1949
|
||||
1626
|
||||
1083
|
||||
1402
|
||||
1223
|
||||
1179
|
||||
2001
|
||||
1790
|
||||
1551
|
||||
1117
|
||||
1990
|
||||
1968
|
||||
1532
|
||||
1999
|
||||
1175
|
||||
1126
|
||||
1869
|
||||
1666
|
||||
1753
|
||||
513
|
||||
1349
|
||||
1139
|
||||
1941
|
||||
1823
|
||||
1647
|
||||
1835
|
||||
1943
|
||||
1459
|
||||
1833
|
||||
1398
|
||||
1877
|
||||
1625
|
||||
1749
|
||||
1631
|
||||
1864
|
||||
1826
|
||||
1499
|
||||
1336
|
||||
1264
|
||||
1091
|
||||
1558
|
||||
1321
|
||||
1754
|
||||
1729
|
||||
1585
|
||||
1740
|
||||
1767
|
||||
1774
|
||||
1164
|
||||
1318
|
||||
1930
|
||||
1236
|
||||
1995
|
||||
1611
|
||||
1319
|
||||
1361
|
||||
1119
|
||||
1563
|
||||
1578
|
||||
1047
|
||||
1797
|
||||
1787
|
||||
1038
|
||||
1921
|
||||
1656
|
||||
1898
|
||||
1828
|
||||
1727
|
||||
1825
|
||||
2010
|
||||
536
|
||||
1395
|
||||
1865
|
||||
1882
|
||||
1638
|
||||
1954
|
||||
1565
|
||||
1296
|
||||
1723
|
||||
1187
|
||||
60
|
||||
1130
|
||||
1102
|
||||
1963
|
||||
1048
|
||||
1493
|
||||
1795
|
||||
472
|
||||
1496
|
||||
1278
|
||||
1444
|
||||
1889
|
||||
860
|
||||
1975
|
||||
1961
|
||||
1070
|
||||
1570
|
||||
1495
|
||||
1644
|
||||
1881
|
||||
1293
|
||||
1090
|
||||
1906
|
||||
1385
|
||||
1549
|
||||
1143
|
||||
1195
|
||||
2004
|
||||
1397
|
||||
1032
|
||||
1681
|
||||
2000
|
||||
1574
|
||||
1400
|
||||
1911
|
||||
1868
|
||||
1917
|
||||
1872
|
||||
1696
|
||||
1086
|
||||
1291
|
||||
1761
|
||||
1703
|
||||
1202
|
||||
1486
|
||||
1705
|
||||
1924
|
||||
1186
|
||||
1676
|
||||
1615
|
||||
1951
|
||||
1556
|
||||
1604
|
||||
1534
|
||||
2002
|
||||
1334
|
||||
1109
|
||||
1108
|
||||
1713
|
||||
1422
|
||||
1909
|
||||
1418
|
||||
1592
|
||||
1887
|
||||
1037
|
||||
1568
|
||||
1914
|
||||
1780
|
||||
1929
|
||||
1973
|
||||
1684
|
||||
1581
|
||||
1148
|
||||
1931
|
||||
1619
|
||||
1082
|
||||
1166
|
||||
1913
|
||||
1312
|
||||
1330
|
||||
1540
|
||||
1841
|
||||
1977
|
||||
1769
|
||||
1691
|
||||
1821
|
||||
33
2020/days/01/test_day1.py
Normal file
33
2020/days/01/test_day1.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
import pathlib
|
||||
import pytest
|
||||
import day1 as aoc
|
||||
|
||||
PUZZLE_DIR = pathlib.Path(__file__).parent
|
||||
|
||||
#these test fixtures setup the test, mainly by reading the filename into a string in this simple case.
|
||||
@pytest.fixture
|
||||
def example1():
|
||||
puzzle_input = (PUZZLE_DIR / "example1").read_text().strip()
|
||||
return aoc.parse(puzzle_input)
|
||||
|
||||
@pytest.fixture
|
||||
def example2():
|
||||
puzzle_input = (PUZZLE_DIR / "example2").read_text().strip()
|
||||
return aoc.parse(puzzle_input)
|
||||
|
||||
# @pytest.mark.skip(reason="Not implemented")
|
||||
def test_parse_example1(example1):
|
||||
"""Test that input is parsed properly"""
|
||||
assert example1 == [1721, 979, 366, 299, 675, 1456]
|
||||
|
||||
# @pytest.mark.skip(reason="Not implemented")
|
||||
def test_part1_example1(example1):
|
||||
"""Test part 1 on example input"""
|
||||
assert aoc.part1(example1) == 514579
|
||||
|
||||
# @pytest.mark.skip(reason="Not implemented")
|
||||
def test_part2_example2(example1):
|
||||
"""Test part 2 on example input"""
|
||||
assert aoc.part2(example1) == 241861950
|
||||
Loading…
Add table
Add a link
Reference in a new issue