├── bash ├── 2017 │ ├── run_tests.bash │ └── dec05.bash ├── 2018 │ └── run_tests.bash ├── 2020 │ └── run_tests.bash └── 2021 │ ├── dec02.bash │ ├── run_tests.bash │ └── dec01.bash ├── python ├── src │ ├── __init__.py │ ├── y2015 │ │ ├── __init__.py │ │ ├── dec10.py │ │ ├── dec25.py │ │ ├── dec04.py │ │ ├── dec02.py │ │ ├── dec08.py │ │ ├── dec01.py │ │ ├── dec20.py │ │ └── dec12.py │ ├── y2016 │ │ ├── __init__.py │ │ ├── dec06.py │ │ ├── dec16.py │ │ └── dec18.py │ ├── y2017 │ │ ├── __init__.py │ │ ├── common.py │ │ ├── dec01.py │ │ ├── dec04.py │ │ ├── dec02.py │ │ ├── dec05.py │ │ ├── dec12.py │ │ └── dec17.py │ ├── y2018 │ │ ├── __init__.py │ │ ├── dec01.py │ │ └── dec02.py │ ├── y2019 │ │ ├── __init__.py │ │ ├── dec01_golf.py │ │ ├── dec05.py │ │ ├── dec09.py │ │ ├── dec01.py │ │ └── dec02.py │ ├── helpers │ │ ├── __init__.py │ │ └── SimpleGrid.py │ ├── y2021 │ │ ├── dec01_golf.py │ │ ├── run_all.py │ │ ├── dec01.py │ │ └── __init__.py │ ├── y2020 │ │ ├── run_all.py │ │ ├── dec05.py │ │ ├── dec01.py │ │ ├── dec25.py │ │ ├── dec06.py │ │ ├── dec15.py │ │ ├── __init__.py │ │ └── dec02.py │ ├── y2022 │ │ ├── run_all.py │ │ ├── dec01.py │ │ ├── dec06.py │ │ ├── dec04.py │ │ ├── __init__.py │ │ └── dec25.py │ ├── y2023 │ │ ├── run_all.py │ │ ├── __init__.py │ │ └── dec09.py │ ├── y2024 │ │ ├── run_all.py │ │ ├── dec03.py │ │ ├── dec01.py │ │ └── __init__.py │ ├── y2025 │ │ ├── run_all.py │ │ └── __init__.py │ └── gol.py ├── test │ ├── __init__.py │ ├── y2015 │ │ ├── __init__.py │ │ ├── test_dec17.py │ │ ├── test_dec21.py │ │ ├── test_dec04.py │ │ ├── test_dec23.py │ │ ├── test_dec24.py │ │ ├── test_dec02.py │ │ ├── test_dec09.py │ │ ├── test_dec10.py │ │ ├── test_dec22.py │ │ ├── test_dec14.py │ │ ├── test_dec03.py │ │ ├── test_dec11.py │ │ ├── test_dec15.py │ │ ├── test_dec01.py │ │ └── test_dec08.py │ ├── y2016 │ │ ├── __init__.py │ │ ├── test_dec19.py │ │ ├── test_dec14.py │ │ ├── test_dec25.py │ │ ├── test_dec05.py │ │ ├── test_dec01.py │ │ ├── test_dec12.py │ │ ├── test_dec16.py │ │ ├── test_dec23.py │ │ ├── test_dec02.py │ │ ├── test_dec24.py │ │ ├── test_dec17.py │ │ ├── test_dec20.py │ │ ├── test_dec15.py │ │ ├── test_dec10.py │ │ ├── test_dec18.py │ │ ├── test_dec03.py │ │ └── test_dec07.py │ ├── y2017 │ │ ├── __init__.py │ │ ├── test_dec17.py │ │ ├── test_dec16.py │ │ ├── test_dec05.py │ │ ├── test_dec21.py │ │ ├── test_dec13.py │ │ ├── test_dec06.py │ │ ├── test_dec19.py │ │ ├── test_dec02.py │ │ ├── test_dec24.py │ │ ├── test_dec14.py │ │ ├── test_dec08.py │ │ ├── test_dec23.py │ │ ├── test_dec22.py │ │ ├── test_dec12.py │ │ ├── test_dec11.py │ │ ├── test_dec20.py │ │ ├── test_dec10.py │ │ ├── test_dec15.py │ │ ├── test_dec03.py │ │ ├── test_dec07.py │ │ ├── test_dec18.py │ │ ├── test_dec04.py │ │ ├── test_dec09.py │ │ └── test_dec01.py │ ├── y2018 │ │ ├── __init__.py │ │ ├── test_dec20.py │ │ ├── test_dec08.py │ │ ├── test_dec18.py │ │ ├── test_dec17.py │ │ ├── test_dec19.py │ │ ├── test_dec22.py │ │ ├── test_dec06.py │ │ ├── test_dec09.py │ │ ├── test_dec14.py │ │ ├── test_dec12.py │ │ ├── test_dec01.py │ │ ├── test_dec02.py │ │ ├── test_dec13.py │ │ ├── test_dec16.py │ │ ├── test_dec11.py │ │ └── test_dec24.py │ ├── y2019 │ │ ├── __init__.py │ │ ├── test_dec08.py │ │ ├── test_dec01.py │ │ ├── test_dec04.py │ │ ├── test_dec02.py │ │ ├── test_dec09.py │ │ ├── test_dec24.py │ │ ├── test_dec06.py │ │ └── test_dec12.py │ ├── y2020 │ │ ├── __init__.py │ │ ├── test_dec25.py │ │ ├── test_dec02.py │ │ ├── test_dec12.py │ │ ├── test_dec01.py │ │ ├── test_dec17.py │ │ ├── test_dec23.py │ │ ├── test_dec05.py │ │ ├── test_dec08.py │ │ ├── test_dec21.py │ │ ├── test_dec16.py │ │ ├── test_dec11.py │ │ ├── test_dec06.py │ │ ├── test_dec03.py │ │ ├── test_dec14.py │ │ ├── test_dec09.py │ │ ├── test_dec13.py │ │ ├── test_dec15.py │ │ ├── test_dec22.py │ │ └── test_dec10.py │ ├── y2021 │ │ ├── __init__.py │ │ ├── test_dec06.py │ │ ├── test_dec07.py │ │ ├── test_dec21.py │ │ ├── test_dec09.py │ │ ├── test_dec02.py │ │ ├── test_dec25.py │ │ ├── test_dec17.py │ │ ├── test_dec01.py │ │ ├── test_dec15.py │ │ ├── test_dec05.py │ │ ├── test_dec03.py │ │ ├── test_dec11.py │ │ ├── test_dec13.py │ │ ├── test_dec14.py │ │ ├── test_dec23.py │ │ ├── test_dec04.py │ │ └── test_dec20.py │ ├── y2022 │ │ ├── __init__.py │ │ ├── test_dec20.py │ │ ├── test_dec14.py │ │ ├── test_dec17.py │ │ ├── test_dec24.py │ │ ├── test_dec23.py │ │ ├── test_dec12.py │ │ ├── test_dec04.py │ │ ├── test_dec08.py │ │ ├── test_dec18.py │ │ ├── test_dec22.py │ │ ├── test_dec09.py │ │ ├── test_dec03.py │ │ ├── test_dec21.py │ │ ├── test_dec05.py │ │ ├── test_dec02.py │ │ ├── test_dec13.py │ │ ├── test_dec01.py │ │ ├── test_dec19.py │ │ ├── test_dec06.py │ │ ├── test_dec07.py │ │ ├── test_dec16.py │ │ ├── test_dec11.py │ │ └── test_dec25.py │ ├── y2023 │ │ ├── __init__.py │ │ ├── test_dec22.py │ │ ├── test_dec06.py │ │ ├── test_dec15.py │ │ ├── test_dec12.py │ │ ├── test_dec03.py │ │ ├── test_dec16.py │ │ ├── test_dec25.py │ │ ├── test_dec24.py │ │ ├── test_dec20.py │ │ ├── test_dec13.py │ │ ├── test_dec04.py │ │ ├── test_dec17.py │ │ ├── test_dec01.py │ │ ├── test_dec11.py │ │ ├── test_dec21.py │ │ ├── test_dec18.py │ │ ├── test_dec02.py │ │ ├── test_dec19.py │ │ └── test_dec09.py │ ├── y2024 │ │ ├── __init__.py │ │ ├── test_dec09.py │ │ ├── test_dec21.py │ │ ├── test_dec02.py │ │ ├── test_dec14.py │ │ ├── test_dec19.py │ │ ├── test_dec01.py │ │ ├── test_dec06.py │ │ ├── test_dec07.py │ │ ├── test_dec04.py │ │ ├── test_dec11.py │ │ ├── test_dec25.py │ │ ├── test_dec16.py │ │ ├── test_dec13.py │ │ ├── test_dec18.py │ │ ├── test_dec23.py │ │ ├── test_dec03.py │ │ ├── test_dec22.py │ │ ├── test_dec05.py │ │ ├── test_dec10.py │ │ ├── test_dec17.py │ │ └── test_dec12.py │ └── y2025 │ │ ├── __init__.py │ │ ├── test_dec09.py │ │ ├── test_dec05.py │ │ ├── test_dec03.py │ │ ├── test_dec06.py │ │ ├── test_dec04.py │ │ ├── test_dec10.py │ │ ├── test_dec12.py │ │ ├── test_dec07.py │ │ ├── test_dec11.py │ │ ├── test_dec02.py │ │ ├── test_dec01.py │ │ └── test_dec08.py └── __init__.py ├── requirements.txt ├── .gitignore ├── .flake8 └── README.md /bash/2021/dec02.bash: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/src/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/src/y2015/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/src/y2016/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/src/y2017/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/src/y2018/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/src/y2019/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/src/helpers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2015/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2016/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2017/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2018/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2019/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2020/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2021/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2022/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2023/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2024/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /python/test/y2025/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pytest 2 | z3-solver -------------------------------------------------------------------------------- /python/__init__.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anna' 2 | -------------------------------------------------------------------------------- /python/src/y2017/common.py: -------------------------------------------------------------------------------- 1 | DATA_DIR = '../../../data/2017' 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | */__pycache__ 3 | *.pyc 4 | .cache/ 5 | data/ -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length = 99 3 | exclude = .git, __pycache__, build, dist, .eggs *_golf.py 4 | -------------------------------------------------------------------------------- /python/src/y2021/dec01_golf.py: -------------------------------------------------------------------------------- 1 | import sys 2 | n=[*map(int,sys.stdin.readlines())] 3 | for i in 1,3: 4 | print(sum(a 498,6 -> 496,6 8 | 503,4 -> 502,4 -> 502,9 -> 494,9""".split('\n') 9 | 10 | def test_part_1(self): 11 | self.assertEqual(24, Dec14(instructions=self.data).part_1()) 12 | 13 | def test_part_2(self): 14 | self.assertEqual(93, Dec14(instructions=self.data).part_2()) 15 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec17.py: -------------------------------------------------------------------------------- 1 | from python.src.y2022.dec17 import Dec17 2 | 3 | import unittest 4 | 5 | 6 | class TestDec17(unittest.TestCase): 7 | data = [">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>"] 8 | 9 | def test_part_1(self): 10 | self.assertEqual(3068, Dec17(instructions=self.data).part_1()) 11 | 12 | def test_part_2(self): 13 | self.assertEqual(1514285714288, 14 | Dec17(instructions=self.data).part_2()) 15 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec24.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec24 import Dec24 4 | 5 | 6 | class TestDec24(unittest.TestCase): 7 | data = """#.###### 8 | #>>.<^<# 9 | #.<..<<# 10 | #>v.><># 11 | #<^v^^># 12 | ######.#""".split('\n') 13 | 14 | def test_part_1(self): 15 | self.assertEqual(18, Dec24(instructions=self.data).part_1()) 16 | 17 | def test_part_2(self): 18 | self.assertEqual(54, Dec24(instructions=self.data).part_2()) 19 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec09 import Dec09 4 | 5 | 6 | class TestDec09(unittest.TestCase): 7 | data = ["2333133121414131402"] 8 | 9 | def test_part_1(self): 10 | self.assertEqual(1928, Dec09(instructions=self.data).part_1()) 11 | 12 | def test_part_2(self): 13 | self.assertEqual(2858, Dec09(instructions=self.data).part_2()) 14 | 15 | 16 | if __name__ == '__main__': 17 | unittest.main() 18 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec21.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec21 4 | 5 | 6 | class TestFractalArt(unittest.TestCase): 7 | 8 | def test_list_pixels(self): 9 | puzzle_input = [ 10 | "../.# => ##./#../...", 11 | ".#./..#/### => #..#/..../..../#..#" 12 | ] 13 | 14 | FA = dec21.FractalArt(puzzle_input) 15 | for _ in range(2): 16 | FA.step() 17 | self.assertEqual(12, FA.lit_pixels()) 18 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec21.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec21 import Dec21 4 | 5 | 6 | class TestDec21(unittest.TestCase): 7 | data = ["Player 1 starting position: 4", 8 | "Player 2 starting position: 8"] 9 | 10 | def test_part_1(self): 11 | 12 | self.assertEqual(739785, Dec21(instructions=self.data).part_1()) 13 | 14 | def test_part_2(self): 15 | self.assertEqual(444356092776315, Dec21(instructions=self.data).part_2()) 16 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec23.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec23 import Dec23 4 | 5 | 6 | class TestDec23(unittest.TestCase): 7 | larger_data = """....#.. 8 | ..###.# 9 | #...#.# 10 | .#...## 11 | #.###.. 12 | ##.#.## 13 | .#..#..""".split('\n') 14 | 15 | def atest_part_1(self): 16 | self.assertEqual(110, Dec23(instructions=self.larger_data).part_1()) 17 | 18 | def test_part_2(self): 19 | self.assertEqual(20, Dec23(instructions=self.larger_data).part_2()) 20 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec12.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec12 import Dec12 4 | 5 | 6 | class TestDec12(unittest.TestCase): 7 | data = [ 8 | "Sabqponm", 9 | "abcryxxl", 10 | "accszExk", 11 | "acctuvwj", 12 | "abdefghi" 13 | ] 14 | 15 | def test_part_1(self): 16 | self.assertEqual(31, Dec12(instructions=self.data).part_1()) 17 | 18 | def test_part_2(self): 19 | self.assertEqual(29, Dec12(instructions=self.data).part_2()) 20 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec21.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec21 import Dec21 4 | from src.y2024.dec21 import KeyPad, ReindeerStarship 5 | 6 | 7 | class TestDec21(unittest.TestCase): 8 | data = [ 9 | "029A", 10 | "980A", 11 | "179A", 12 | "456A", 13 | "379A" 14 | ] 15 | 16 | def test_part_1(self): 17 | self.assertEqual(126384, Dec21(instructions=self.data).part_1()) 18 | 19 | 20 | if __name__ == '__main__': 21 | unittest.main() 22 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec02.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec02 import Dec02 4 | 5 | 6 | class TestDec02(unittest.TestCase): 7 | data = [ 8 | "1-3 a: abcde", 9 | "1-3 b: cdefg", 10 | "2-9 c: ccccccccc" 11 | ] 12 | 13 | def test_old_policy(self): 14 | d = Dec02(instructions=self.data) 15 | self.assertEqual(2, d.part_1()) 16 | 17 | def test_new_policy(self): 18 | d = Dec02(instructions=self.data) 19 | self.assertEqual(1, d.part_2()) 20 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec12.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec12 import Dec12 4 | 5 | 6 | class TestDec12(unittest.TestCase): 7 | instructions = [ 8 | "F10", 9 | "N3", 10 | "F7", 11 | "R90", 12 | "F11" 13 | ] 14 | 15 | def test_manhattan_distance(self): 16 | self.assertEqual(25, Dec12(instructions=self.instructions).part_1()) 17 | 18 | def test_with_waypoint(self): 19 | self.assertEqual(286, Dec12(instructions=self.instructions).part_2()) 20 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec09 import Dec09 4 | 5 | 6 | class TestDec09(unittest.TestCase): 7 | data = [ 8 | "2199943210", 9 | "3987894921", 10 | "9856789892", 11 | "8767896789", 12 | "9899965678" 13 | ] 14 | 15 | def test_risk_level(self): 16 | self.assertEqual(15, Dec09(instructions=self.data).part_1()) 17 | 18 | def test_basins(self): 19 | self.assertEqual(1134, Dec09(instructions=self.data).part_2()) 20 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec04.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec04 import Dec04 4 | 5 | 6 | class TestDec04(unittest.TestCase): 7 | data = [ 8 | "2-4,6-8", 9 | "2-3,4-5", 10 | "5-7,7-9", 11 | "2-8,3-7", 12 | "6-6,4-6", 13 | "2-6,4-8" 14 | ] 15 | 16 | def test_part_1(self): 17 | self.assertEqual(2, Dec04(instructions=self.data).part_1()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(4, Dec04(instructions=self.data).part_2()) 21 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec08.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec08 import Dec08 4 | 5 | 6 | class TestDec08(unittest.TestCase): 7 | data = [ 8 | "30373", 9 | "25512", 10 | "65332", 11 | "33549", 12 | "35390" 13 | ] 14 | 15 | def test_part_1_count_visible_trees(self): 16 | self.assertEqual(21, Dec08(instructions=self.data).part_1()) 17 | 18 | def test_part_2_scenic_score(self): 19 | self.assertEqual(8, Dec08(instructions=self.data).part_2()) 20 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec20.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec20 import Dec20 4 | 5 | 6 | class TestDec20(unittest.TestCase): 7 | 8 | def test_part1(self): 9 | cases = [ 10 | # (3, ['^WNE$']), 11 | # (10, ['^ENWWW(NEEE|SSE(EE|N))$']), 12 | (18, ['^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$']) 13 | ] 14 | 15 | for expected, instruction in cases: 16 | d = Dec20(instructions=instruction) 17 | self.assertEqual(expected, d.part_1()) 18 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec01.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec01 import Dec01 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | data = [ 8 | "1721", 9 | "979", 10 | "366", 11 | "299", 12 | "675", 13 | "1456" 14 | ] 15 | 16 | def test_two_entries(self): 17 | self.assertEqual(514579, Dec01(instructions=self.data).part_1()) 18 | 19 | def test_three_entries(self): 20 | self.assertEqual(241861950, Dec01(instructions=self.data).part_2()) 21 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec18.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec18 import Dec18 4 | 5 | 6 | class TestDec18(unittest.TestCase): 7 | data = """2,2,2 8 | 1,2,2 9 | 3,2,2 10 | 2,1,2 11 | 2,3,2 12 | 2,2,1 13 | 2,2,3 14 | 2,2,4 15 | 2,2,6 16 | 1,2,5 17 | 3,2,5 18 | 2,1,5 19 | 2,3,5""".split('\n') 20 | 21 | def test_part_1(self): 22 | self.assertEqual(64, Dec18(instructions=self.data).part_1()) 23 | 24 | def test_part_2(self): 25 | self.assertEqual(58, Dec18(instructions=self.data).part_2()) 26 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec09 import Dec09 4 | 5 | 6 | class TestDec09(unittest.TestCase): 7 | data = """7,1 8 | 11,1 9 | 11,7 10 | 9,7 11 | 9,5 12 | 2,5 13 | 2,3 14 | 7,3""".splitlines() 15 | 16 | def test_part_1(self): 17 | self.assertEqual(50, Dec09(instructions=self.data).part_1()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(24, Dec09(instructions=self.data).part_2()) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/src/y2016/dec06.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def repetition_code(data): 5 | return ''.join(Counter(x).most_common(1)[0][0] for x in zip(*data)) 6 | 7 | 8 | def modified_repetition_code(data): 9 | return ''.join(Counter(x).most_common()[::-1][0][0] for x in zip(*data)) 10 | 11 | 12 | if __name__ == '__main__': 13 | with open('../../../data/2016/input.6.txt', 'r') as fh: 14 | lines = fh.readlines() 15 | print(repetition_code(lines)) 16 | print(modified_repetition_code(lines)) 17 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec22.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec22 import Dec22 4 | 5 | 6 | class TestDec22(unittest.TestCase): 7 | data = """ ...# 8 | .#.. 9 | #... 10 | .... 11 | ...#.......# 12 | ........#... 13 | ..#....#.... 14 | ..........#. 15 | ...#.... 16 | .....#.. 17 | .#...... 18 | ......#. 19 | 20 | 10R5L5R10L4R5L5""".split('\n') 21 | 22 | def test_part_1(self): 23 | self.assertEqual(6032, Dec22(instructions=self.data).part_1()) 24 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec04.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import unittest 3 | 4 | from python.src.y2015 import dec04 5 | 6 | __author__ = 'anna' 7 | 8 | 9 | class Dec04Tests(unittest.TestCase): 10 | def setUp(self): 11 | pass 12 | 13 | def testMiningHashesExample1(self): 14 | self.assertEqual(609043, dec04.mining_hashes('abcdef')) 15 | 16 | def testMiningHashesExample2(self): 17 | self.assertEqual(1048970, dec04.mining_hashes('pqrstuv')) 18 | 19 | 20 | if __name__ == '__main__': 21 | unittest.main() 22 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec23.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Anna' 2 | 3 | import unittest 4 | from python.src.y2015 import dec23 5 | 6 | 7 | class Dec23Tests(unittest.TestCase): 8 | def test_example(self): 9 | lines = ['inc a', 10 | 'jio a, +2', 11 | 'tpl a', 12 | 'inc a'] 13 | 14 | program = dec23.Program(lines) 15 | registers = program.run({'a': 0, 'b': 0}) 16 | 17 | self.assertEqual(2, registers['a']) 18 | 19 | 20 | if __name__ == '__main__': 21 | unittest.main() 22 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec13.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec13 3 | 4 | 5 | class TestFireWall(unittest.TestCase): 6 | 7 | @classmethod 8 | def setUpClass(cls): 9 | cls.puzzle_input = ["0: 3", "1: 2", "4: 4", "6: 4"] 10 | 11 | def test_severity(self): 12 | fw = dec13.FireWall(self.puzzle_input) 13 | self.assertEqual(24, fw.severity()) 14 | 15 | def test_shortest_delay(self): 16 | fw = dec13.FireWall(self.puzzle_input) 17 | self.assertEqual(10, fw.shortest_delay()) 18 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec17.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec17 import Dec17 4 | 5 | 6 | class TestDec17(unittest.TestCase): 7 | 8 | def test_active_cubes_3d(self): 9 | state = [".#.", 10 | "..#", 11 | "###"] 12 | self.assertEqual(112, Dec17(instructions=state).part_1()) 13 | 14 | def test_active_cubes_4d(self): 15 | state = [".#.", 16 | "..#", 17 | "###"] 18 | self.assertEqual(848, Dec17(instructions=state).part_2()) 19 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec02.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec02 import Dec02 4 | 5 | 6 | class TestDec02(unittest.TestCase): 7 | data = [ 8 | "forward 5", 9 | "down 5 ", 10 | "forward 8", 11 | "up 3 ", 12 | "down 8 ", 13 | "forward 2" 14 | ] 15 | 16 | def test_part_1(self): 17 | self.assertEqual(150, Dec02(instructions=self.data).part_1()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(900, Dec02(instructions=self.data).part_2()) 21 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec02.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec02 import Dec02 4 | 5 | 6 | class TestDec02(unittest.TestCase): 7 | data = """7 6 4 2 1 8 | 1 2 7 8 9 9 | 9 7 6 2 1 10 | 1 3 2 4 5 11 | 8 6 4 4 1 12 | 1 3 6 7 9""".split('\n') 13 | 14 | def test_part_1(self): 15 | self.assertEqual(2, Dec02(instructions=self.data).part_1()) 16 | 17 | def test_part_2(self): 18 | self.assertEqual(4, Dec02(instructions=self.data).part_2()) 19 | 20 | 21 | if __name__ == '__main__': 22 | unittest.main() 23 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec19.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec19 5 | 6 | 7 | class Dec19Tests(unittest.TestCase): 8 | 9 | def test_remaining_elf(self): 10 | self.assertEqual(3, dec19.remaining_elf_dumb(5)) 11 | self.assertEqual(7, dec19.remaining_elf_dumb(11)) 12 | 13 | def test_steal_across(self): 14 | self.assertEqual(2, dec19.steal_across(5)) 15 | self.assertEqual(2, dec19.steal_across(11)) 16 | 17 | 18 | if __name__ == '__main__': 19 | unittest.main() 20 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec06.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec06 3 | 4 | 5 | class TestDec06(unittest.TestCase): 6 | 7 | def setUp(self): 8 | self.puzzle_input = "0 2 7 0" 9 | 10 | def test_memory_reallocation(self): 11 | memory = dec06.Memory(self.puzzle_input) 12 | self.assertEqual(5, memory.steps_until_loop()) 13 | 14 | def test_loop_size(self): 15 | memory = dec06.Memory(self.puzzle_input) 16 | memory.steps_until_loop() 17 | self.assertEqual(4, memory.loop_size()) 18 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec05.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec05 import Dec05 4 | 5 | 6 | class TestDec05(unittest.TestCase): 7 | data = """3-5 8 | 10-14 9 | 16-20 10 | 12-18 11 | 12 | 1 13 | 5 14 | 8 15 | 11 16 | 17 17 | 32""".splitlines() 18 | 19 | def test_part_1(self): 20 | self.assertEqual(3, Dec05(instructions=self.data).part_1()) 21 | 22 | def test_part_2(self): 23 | self.assertEqual(14, Dec05(instructions=self.data).part_2()) 24 | 25 | 26 | if __name__ == '__main__': 27 | unittest.main() 28 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec25.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec25 import Dec25 4 | 5 | 6 | class TestDec125(unittest.TestCase): 7 | 8 | def test_small_map(self): 9 | data = [ 10 | "v...>>.vv>", 11 | ".vv>>.vv..", 12 | ">>.>v>...v", 13 | ">>v>>.>.v.", 14 | "v>v.vv.v..", 15 | ">.>>..v...", 16 | ".vv..>.>v.", 17 | "v.v..>>v.v", 18 | "....v..v.>"] 19 | 20 | self.assertEqual(58, Dec25(instructions=data).part_1()) 21 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec03.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec03 import Dec03 4 | 5 | 6 | class TestDec03(unittest.TestCase): 7 | data = """987654321111111 8 | 811111111111119 9 | 234234234234278 10 | 818181911112111""".split('\n') 11 | 12 | def test_part_1(self): 13 | self.assertEqual(357, Dec03(instructions=self.data).part_1()) 14 | 15 | def test_part_2(self): 16 | self.assertEqual(3121910778619, Dec03(instructions=self.data).part_2()) 17 | 18 | 19 | if __name__ == '__main__': 20 | unittest.main() 21 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec06.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec06 import Dec06 4 | 5 | 6 | class TestDec06(unittest.TestCase): 7 | data = """123 328 51 64 8 | 45 64 387 23 9 | 6 98 215 314 10 | * + * + """.splitlines() 11 | 12 | def test_part_1(self): 13 | self.assertEqual(4277556, Dec06(instructions=self.data).part_1()) 14 | 15 | def test_part_2(self): 16 | self.assertEqual(3263827, Dec06(instructions=self.data).part_2()) 17 | 18 | 19 | if __name__ == '__main__': 20 | unittest.main() 21 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec23.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec23 import Dec23 4 | 5 | 6 | class TestDec23(unittest.TestCase): 7 | 8 | def test_part_1_10_moves(self): 9 | self.assertEqual('92658374', Dec23(instructions=['389125467']).part_1(moves=10)) 10 | 11 | def test_part_1(self): 12 | self.assertEqual('67384529', Dec23(instructions=['389125467']).part_1()) 13 | 14 | def test_part_2(self): 15 | self.assertEqual(149245887792, 16 | Dec23(instructions=['389125467']).part_2()) 17 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec19.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec19 3 | 4 | 5 | class TestRouting(unittest.TestCase): 6 | def test_routing(self): 7 | puzzle_input = [ 8 | " | ", 9 | " | +--+ ", 10 | " A | C ", 11 | " F---|----E|--+ ", 12 | " | | | D ", 13 | " +B-+ +--+ " 14 | ] 15 | r = dec19.Routing(puzzle_input) 16 | self.assertEqual('ABCDEF', r.part_1()) 17 | self.assertEqual(38, r.part_2()) 18 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec05.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec05 import Dec05 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | data = [ 8 | ("FBFBBFFRLR", 357), 9 | ("BFFFBBFRRR", 567), 10 | ("FFFBBBFRRR", 119), 11 | ("BBFFBBFRLL", 820) 12 | ] 13 | 14 | def test_seat_id(self): 15 | for case in self.data: 16 | self.assertEqual(case[1], Dec05.seat_id(case[0])) 17 | 18 | def test_part_1(self): 19 | self.assertEqual(820, Dec05(instructions=[d[0] for d in self.data]).part_1()) 20 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec02.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec02 3 | 4 | 5 | class TestDec02(unittest.TestCase): 6 | def test_minmax_checksum(self): 7 | input = [[5, 1, 9, 5], 8 | [7, 5, 3], 9 | [2, 4, 6, 8]] 10 | self.assertEqual(18, dec02.minmax_checksum(input)) 11 | 12 | def test_evenly_divisible_checksum(self): 13 | input = [[5, 9, 2, 8], 14 | [9, 4, 7, 3], 15 | [3, 8, 6, 5]] 16 | self.assertEqual(9, dec02.evenly_divisible_checksum(input)) 17 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec24.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec24 4 | 5 | 6 | class TestBridge(unittest.TestCase): 7 | def test_strongest_bridge(self): 8 | puzzle_input = ["0/2", 9 | "2/2", 10 | "2/3", 11 | "3/4", 12 | "3/5", 13 | "0/1", 14 | "10/1", 15 | "9/10"] 16 | b = dec24.Bridges(puzzle_input) 17 | b.build() 18 | self.assertEqual(31, b.part1()) 19 | -------------------------------------------------------------------------------- /python/src/y2025/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 401 2 | from python.src.y2025.dec01 import Dec01 3 | from python.src.y2025.dec02 import Dec02 4 | from python.src.y2025.dec03 import Dec03 5 | from python.src.y2025.dec04 import Dec04 6 | from python.src.y2025.dec05 import Dec05 7 | from python.src.y2025.dec06 import Dec06 8 | from python.src.y2025.dec07 import Dec07 9 | from python.src.y2025.dec08 import Dec08 10 | from python.src.y2025.dec09 import Dec09 11 | from python.src.y2025.dec10 import Dec10 12 | from python.src.y2025.dec11 import Dec11 13 | from python.src.y2025.dec12 import Dec12 -------------------------------------------------------------------------------- /python/test/y2018/test_dec08.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec08 import Dec08 4 | 5 | 6 | class TestDec08(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | instructions = [ 10 | "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2" 11 | ] 12 | d = Dec08(instructions) 13 | self.assertEqual(138, d.part_1()) 14 | 15 | def test_part_2(self): 16 | instructions = [ 17 | "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2" 18 | ] 19 | d = Dec08(instructions) 20 | self.assertEqual(66, d.part_2()) 21 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec17.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec17 import Dec17 4 | 5 | 6 | class TestDec17(unittest.TestCase): 7 | 8 | def test_highest_y(self): 9 | self.assertEqual(45, Dec17(instructions=['target area: x=20..30, y=-10..-5']).part_1()) 10 | 11 | def test_number_of_hits(self): 12 | self.assertEqual(112, Dec17(instructions=['target area: x=20..30, y=-10..-5']).part_2()) 13 | 14 | def test_breaks_simple_solution(self): 15 | self.assertEqual(66, Dec17(instructions=['target area: x=352..377, y=-49..-30']).part_1()) 16 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec14.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec14 import Dec14 4 | 5 | 6 | class TestDec14(unittest.TestCase): 7 | data = """p=0,4 v=3,-3 8 | p=6,3 v=-1,-3 9 | p=10,3 v=-1,2 10 | p=2,0 v=2,-1 11 | p=0,0 v=1,3 12 | p=3,0 v=-2,-2 13 | p=7,6 v=-1,-3 14 | p=3,0 v=-1,-2 15 | p=9,3 v=2,3 16 | p=7,3 v=-1,2 17 | p=2,4 v=2,-3 18 | p=9,5 v=-3,-3""".split('\n') 19 | 20 | def test_part_1(self): 21 | self.assertEqual(12, Dec14(instructions=self.data).part_1(max_y=7, max_x=11)) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec19.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec19 import Dec19 4 | 5 | 6 | class TestDec19(unittest.TestCase): 7 | data = """r, wr, b, g, bwu, rb, gb, br 8 | 9 | brwrr 10 | bggr 11 | gbbr 12 | rrbgbr 13 | ubwu 14 | bwurrg 15 | brgr 16 | bbrgwb""".split('\n') 17 | 18 | def test_part_1(self): 19 | self.assertEqual(6, Dec19(instructions=self.data).part_1()) 20 | 21 | def test_part_2(self): 22 | self.assertEqual(16, Dec19(instructions=self.data).part_2()) 23 | 24 | 25 | if __name__ == '__main__': 26 | unittest.main() 27 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec09 import Dec09 4 | 5 | 6 | class TestDec09(unittest.TestCase): 7 | data = [ 8 | "R 4", 9 | "U 4", 10 | "L 3", 11 | "D 1", 12 | "R 4", 13 | "D 1", 14 | "L 5", 15 | "R 2", 16 | ] 17 | 18 | def test_part_1_count_tail_positions(self): 19 | self.assertEqual(13, Dec09(instructions=self.data).part_1()) 20 | 21 | def test_part_2_count_tail_positions(self): 22 | self.assertEqual(1, Dec09(instructions=self.data).part_2()) 23 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec01.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec01 import Dec01 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | data = [ 8 | "3 4", 9 | "4 3", 10 | "2 5", 11 | "1 3", 12 | "3 9", 13 | "3 3" 14 | ] 15 | 16 | def test_part_1(self): 17 | self.assertEqual(11, Dec01(instructions=self.data).part_1()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(31, Dec01(instructions=self.data).part_2()) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec14.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec14 5 | 6 | 7 | class Dec14Tests(unittest.TestCase): 8 | def test_otp_keys(self): 9 | keys = dec14.get_hash_keys('abc') 10 | self.assertEqual(39, keys[0]) 11 | self.assertEqual(92, keys[1]) 12 | self.assertEqual(22728, keys[63]) 13 | 14 | def test_stretch(self): 15 | self.assertEqual("a107ff634856bb300138cac6568c0f24", 16 | dec14.super_hash("abc0", 2016)) 17 | 18 | 19 | if __name__ == '__main__': 20 | unittest.main() 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advent of Code 2 | 3 | ## 2015 4 | 5 | - Python (🎄 50⭐) 6 | 7 | ## 2016 8 | 9 | - Python (🎄 50⭐) 10 | 11 | ## 2017 12 | 13 | - Python (🎄 50⭐) 14 | - Bash (10⭐) 15 | 16 | ## 2018 17 | 18 | - Python (🎄 50⭐) 19 | - Bash (2⭐) 20 | 21 | ## 2019 22 | 23 | - Python (🎄 50⭐) 24 | 25 | ## 2020 26 | 27 | - Python (🎄 50⭐) 28 | - Bash (2⭐) 29 | 30 | ## 2021 31 | 32 | - Python (🎄 50⭐) 33 | - Bash (2⭐) 34 | 35 | ## 2022 36 | 37 | - Python (🎄 50⭐) 38 | 39 | ## 2023 40 | 41 | - Python (🎄 50⭐) 42 | 43 | ## 2024 44 | 45 | - Python (🎄 50⭐) 46 | 47 | ## 2025 48 | 49 | - Python (🎄 24⭐) 50 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec25.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec25 5 | 6 | 7 | class Dec25Tests(unittest.TestCase): 8 | def test_out(self): 9 | self.computer = dec25.Computer(['out a', 10 | 'inc a', 11 | 'jnz 1 -2'], 12 | 10) 13 | self.computer.run() 14 | self.assertListEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 15 | self.computer.output) 16 | 17 | 18 | if __name__ == '__main__': 19 | unittest.main() 20 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec18.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec18 import Dec18 4 | 5 | 6 | class TestDec18(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | acres = [ 10 | ".#.#...|#.", 11 | ".....#|##|", 12 | ".|..|...#.", 13 | "..|#.....#", 14 | "#.#|||#|#|", 15 | "...#.||...", 16 | ".|....|...", 17 | "||...#|.#|", 18 | "|.||||..|.", 19 | "...#.|..|." 20 | ] 21 | 22 | d = Dec18(instructions=acres) 23 | self.assertEqual(1147, d.part_1()) 24 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec01.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec01 import Dec01 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | data = [ 8 | "199", 9 | "200", 10 | "208", 11 | "210", 12 | "200", 13 | "207", 14 | "240", 15 | "269", 16 | "260", 17 | "263" 18 | ] 19 | 20 | def test_number_of_decreases(self): 21 | self.assertEqual(7, Dec01(instructions=self.data).part_1()) 22 | 23 | def test_three_measurement_window(self): 24 | self.assertEqual(5, Dec01(instructions=self.data).part_2()) 25 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec06.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec06 import Dec06 4 | 5 | 6 | class TestDec06(unittest.TestCase): 7 | data = """....#..... 8 | .........# 9 | .......... 10 | ..#....... 11 | .......#.. 12 | .......... 13 | .#..^..... 14 | ........#. 15 | #......... 16 | ......#...""".split('\n') 17 | 18 | def test_part_1(self): 19 | self.assertEqual(41, Dec06(instructions=self.data).part_1()) 20 | 21 | def test_part_2(self): 22 | self.assertEqual(6, Dec06(instructions=self.data).part_2()) 23 | 24 | 25 | if __name__ == '__main__': 26 | unittest.main() 27 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec04.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec04 import Dec04 4 | 5 | 6 | class TestDec04(unittest.TestCase): 7 | data = """..@@.@@@@. 8 | @@@.@.@.@@ 9 | @@@@@.@.@@ 10 | @.@@@@..@. 11 | @@.@@@@.@@ 12 | .@@@@@@@.@ 13 | .@.@.@.@@@ 14 | @.@@@.@@@@ 15 | .@@@@@@@@. 16 | @.@.@@@.@.""".splitlines() 17 | 18 | def test_part_1(self): 19 | self.assertEqual(13, Dec04(instructions=self.data).part_1()) 20 | 21 | def test_part_2(self): 22 | self.assertEqual(43, Dec04(instructions=self.data).part_2()) 23 | 24 | 25 | if __name__ == '__main__': 26 | unittest.main() 27 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec24.py: -------------------------------------------------------------------------------- 1 | from python.src.y2015 import dec24 2 | import unittest 3 | 4 | 5 | class Dec23Tests(unittest.TestCase): 6 | 7 | def test_quantum_entanglement(self): 8 | self.assertEqual(99, dec24.quantum_entanglement([11, 9])) 9 | 10 | def test_smallest_group(self): 11 | self.assertSetEqual({9, 11}, 12 | set(dec24.smallest_group([1, 2, 3, 4, 5, 7, 8, 9, 10, 11], 3))) 13 | 14 | def test_smallest_group_part_2(self): 15 | self.assertSetEqual({11, 4}, 16 | set(dec24.smallest_group([1, 2, 3, 4, 5, 7, 8, 9, 10, 11], 4))) 17 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec14.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec14 4 | 5 | 6 | class TestDefrag(unittest.TestCase): 7 | 8 | @classmethod 9 | def setUpClass(cls): 10 | cls.defragger = dec14.Defragger("flqrgnkx") 11 | 12 | def test_hash_to_bits(self): 13 | self.assertEqual('1010000011000010000000010111', self.defragger.hash_to_bits('a0c2017')) 14 | 15 | def test_find_squares_used(self): 16 | self.assertEqual(8108, self.defragger.find_squares_used()) 17 | 18 | def test_find_regions(self): 19 | self.assertEqual(1242, self.defragger.find_regions()) 20 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec17.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec17 import Dec17 4 | 5 | 6 | class TestDec17(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | scan = [ 10 | "x=495, y=2..7", 11 | "y=7, x=495..501", 12 | "x=501, y=3..7", 13 | "x=498, y=2..4", 14 | "x=506, y=1..2", 15 | "x=498, y=10..13", 16 | "x=504, y=10..13", 17 | "y=13, x=498..504" 18 | ] 19 | 20 | d = Dec17(instructions=scan) 21 | self.assertEqual(57, d.part_1()) 22 | self.assertEqual(29, d.part_2()) 23 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec08.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec08 import Dec08 4 | 5 | 6 | class TestDec08(unittest.TestCase): 7 | instructions = [ 8 | "nop +0", 9 | "acc +1", 10 | "jmp +4", 11 | "acc +3", 12 | "jmp -3", 13 | "acc -99", 14 | "acc +1", 15 | "jmp -4", 16 | "acc +6" 17 | ] 18 | 19 | def test_debug_part_1(self): 20 | self.assertEqual(5, Dec08(instructions=self.instructions).part_1()) 21 | 22 | def test_change_par_2(self): 23 | self.assertEqual(8, Dec08(instructions=self.instructions).part_2()) 24 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec07.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec07 import Dec07 4 | 5 | 6 | class TestDec07(unittest.TestCase): 7 | data = """190: 10 19 8 | 3267: 81 40 27 9 | 83: 17 5 10 | 156: 15 6 11 | 7290: 6 8 6 15 12 | 161011: 16 10 13 13 | 192: 17 8 14 14 | 21037: 9 7 18 13 15 | 292: 11 6 16 20""".split('\n') 16 | 17 | def test_part_1(self): 18 | self.assertEqual(3749, Dec07(instructions=self.data).part_1()) 19 | 20 | def test_part_2(self): 21 | self.assertEqual(11387, Dec07(instructions=self.data).part_2()) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec03.py: -------------------------------------------------------------------------------- 1 | from python.src.y2022.dec03 import Dec03 2 | 3 | import unittest 4 | 5 | 6 | class TestDec03(unittest.TestCase): 7 | data = [ 8 | "vJrwpWtwJgWrhcsFMMfFFhFp", 9 | "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL", 10 | "PmmdzqPrVvPwwTWBwg", 11 | "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn", 12 | "ttgJtRGJQctTZtZT", 13 | "CrZsJsPPZsGzwwsLwLmpwMDw" 14 | ] 15 | 16 | def test_part_1(self): 17 | self.assertEqual(157, Dec03(instructions=self.data).part_1()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(70, Dec03(instructions=self.data).part_2()) 21 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec08.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec08 4 | 5 | 6 | class TestDecc08(unittest.TestCase): 7 | 8 | def setUp(self): 9 | puzzle_input = ["b inc 5 if a > 1", 10 | "a inc 1 if b < 5", 11 | "c dec -10 if a >= 1", 12 | "c inc -20 if c == 10"] 13 | self.context = dec08.Context(puzzle_input) 14 | 15 | def test_max_value(self): 16 | self.context.pp() 17 | self.context.run() 18 | self.assertEqual(1, self.context.max()) 19 | 20 | self.assertEqual(10, self.context.all_time_max) 21 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec23.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec23 import Dec23 4 | 5 | 6 | class TestDec23(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | nanobots = [ 10 | "pos=<0,0,0>, r=4", 11 | "pos=<1,0,0>, r=1", 12 | "pos=<4,0,0>, r=3", 13 | "pos=<0,2,0>, r=1", 14 | "pos=<0,5,0>, r=3", 15 | "pos=<0,0,3>, r=1", 16 | "pos=<1,1,1>, r=1", 17 | "pos=<1,1,2>, r=1", 18 | "pos=<1,3,1>, r=1" 19 | ] 20 | 21 | d = Dec23(instructions=nanobots) 22 | self.assertEqual(7, d.part_1()) 23 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec19.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec19 import Dec19 4 | 5 | 6 | class TestDec19(unittest.TestCase): 7 | 8 | def test_device(self): 9 | program = [ 10 | "#ip 0", 11 | "seti 5 0 1", 12 | "seti 6 0 2", 13 | "addi 0 1 0", 14 | "addr 1 2 3", 15 | "setr 1 0 0", 16 | "seti 8 0 4", 17 | "seti 9 0 5" 18 | ] 19 | 20 | d = Dec19(instructions=program) 21 | d.device.run(d.program) 22 | self.assertListEqual([7, 5, 6, 0, 0, 9], 23 | d.device.reg) 24 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec22.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec22 import Dec22 4 | 5 | 6 | class TestDec22(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | instructions = [ 10 | "depth: 510\n", 11 | "target: 10,10\n" 12 | ] 13 | 14 | d = Dec22(instructions=instructions) 15 | self.assertEqual(114, d.part_1()) 16 | 17 | def test_part_2(self): 18 | instructions = [ 19 | "depth: 510\n", 20 | "target: 10,10\n" 21 | ] 22 | 23 | d = Dec22(instructions=instructions) 24 | self.assertEqual(45, d.part_2()) 25 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec22.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec22 import Dec22 4 | 5 | 6 | class TestDec22(unittest.TestCase): 7 | data = [ 8 | "1,0,1~1,2,1", 9 | "0,0,2~2,0,2", 10 | "0,2,3~2,2,3", 11 | "0,0,4~0,2,4", 12 | "2,0,5~2,2,5", 13 | "0,1,6~2,1,6", 14 | "1,1,8~1,1,9" 15 | ] 16 | 17 | def test_part_1(self): 18 | self.assertEqual(5, Dec22(instructions=self.data).part_1()) 19 | 20 | def test_part_2(self): 21 | self.assertEqual(7, Dec22(instructions=self.data).part_2()) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec10.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec10 import Dec10 4 | 5 | 6 | class TestDec10(unittest.TestCase): 7 | data = """[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7} 8 | [...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2} 9 | [.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}""".splitlines() 10 | 11 | def test_part_1(self): 12 | self.assertEqual(7, Dec10(instructions=self.data).part_1()) 13 | 14 | def test_part_2(self): 15 | self.assertEqual(33, Dec10(instructions=self.data).part_2()) 16 | 17 | 18 | if __name__ == '__main__': 19 | unittest.main() 20 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec05.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import unittest 3 | 4 | from python.src.y2016 import dec05 5 | 6 | __author__ = 'anna' 7 | 8 | 9 | class Dec05Tests(unittest.TestCase): 10 | def setUp(self): 11 | pass 12 | 13 | def testMiningHashesExample1(self): 14 | self.assertEqual('1', dec05.hash_password('abc', 1)) 15 | 16 | def testMiningHashesExample2(self): 17 | self.assertEqual('18', dec05.hash_password('abc', 2)) 18 | 19 | def testMoviePassword(self): 20 | self.assertEqual('05ace8e3', dec05.movie_password('abc')) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec21.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec21 import Dec21 4 | 5 | 6 | class TestDec21(unittest.TestCase): 7 | foods = [ 8 | "mxmxvkd kfcds sqjhc nhms (contains dairy, fish)", 9 | "trh fvjkl sbzzf mxmxvkd (contains dairy)", 10 | "sqjhc fvjkl (contains soy)", 11 | "sqjhc mxmxvkd sbzzf (contains fish)" 12 | ] 13 | 14 | def test_part_1(self): 15 | self.assertEqual(5, Dec21(instructions=self.foods).part_1()) 16 | 17 | def test_part_2(self): 18 | self.assertEqual('mxmxvkd,sqjhc,fvjkl', 19 | Dec21(instructions=self.foods).part_2()) 20 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec06.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec06 import Dec06 4 | 5 | 6 | class TestDec06(unittest.TestCase): 7 | data = [ 8 | "Time: 7 15 30", 9 | "Distance: 9 40 200" 10 | ] 11 | 12 | def test_number_of_ways(self): 13 | self.assertEqual(4, Dec06.brute_wins(time=7, distance=9)) 14 | 15 | def test_part_1(self): 16 | self.assertEqual(288, Dec06(instructions=self.data).part_1()) 17 | 18 | def test_part_2(self): 19 | self.assertEqual(71503, Dec06(instructions=self.data).part_2()) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec15.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec15 import Dec15 4 | 5 | 6 | class TestDec15(unittest.TestCase): 7 | data = ['rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7'] 8 | 9 | def test_part_1(self): 10 | cases = ( 11 | (['HASH'], 52), 12 | (self.data, 1320) 13 | ) 14 | for test, expected in cases: 15 | self.assertEqual(expected, Dec15(instructions=test).part_1()) 16 | 17 | def test_part_2(self): 18 | self.assertEqual(145, Dec15(instructions=self.data).part_2()) 19 | 20 | 21 | if __name__ == '__main__': 22 | unittest.main() 23 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec21.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec21 import Dec21 4 | 5 | 6 | class TestDec21(unittest.TestCase): 7 | data = """root: pppw + sjmn 8 | dbpl: 5 9 | cczh: sllz + lgvd 10 | zczc: 2 11 | ptdq: humn - dvpt 12 | dvpt: 3 13 | lfqf: 4 14 | humn: 5 15 | ljgn: 2 16 | sjmn: drzm * dbpl 17 | sllz: 4 18 | pppw: cczh / lfqf 19 | lgvd: ljgn * ptdq 20 | drzm: hmdt - zczc 21 | hmdt: 32""".split('\n') 22 | 23 | def test_part_1(self): 24 | self.assertEqual(152, Dec21(instructions=self.data).part_1()) 25 | 26 | def test_part_2(self): 27 | self.assertEqual(301, Dec21(instructions=self.data).part_2()) 28 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec01.py: -------------------------------------------------------------------------------- 1 | from python.src.y2016 import dec01 2 | import unittest 3 | 4 | __author__ = 'Anna Holmgren' 5 | 6 | 7 | class Dec01Tests(unittest.TestCase): 8 | def testDistanceExample1(self): 9 | self.assertEqual(5, dec01.distance(["R2", "L3"])) 10 | 11 | def testDistanceExample2(self): 12 | self.assertEqual(2, dec01.distance(["R2", "R2", "R2"])) 13 | 14 | def testDistanceExample3(self): 15 | self.assertEqual(12, dec01.distance(["R5", "L5", "R5", "R3"])) 16 | 17 | def testVisitedTwice(self): 18 | position = dec01.visited_twice(["R8", "R4", "R4", "R8"]) 19 | self.assertEqual(4, position) 20 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec15.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec15 import Dec15 4 | 5 | 6 | class TestDec15(unittest.TestCase): 7 | data = [ 8 | "1163751742", 9 | "1381373672", 10 | "2136511328", 11 | "3694931569", 12 | "7463417111", 13 | "1319128137", 14 | "1359912421", 15 | "3125421639", 16 | "1293138521", 17 | "2311944581" 18 | ] 19 | 20 | def test_risk_level(self): 21 | self.assertEqual(40, Dec15(instructions=self.data).part_1()) 22 | 23 | def test_full_map(self): 24 | self.assertEqual(315, Dec15(instructions=self.data).part_2()) 25 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec05.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec05 import Dec05 4 | 5 | 6 | class TestDec05(unittest.TestCase): 7 | data = [ 8 | " [D] ", 9 | "[N] [C] ", 10 | "[Z] [M] [P]", 11 | " 1 2 3 ", 12 | "", 13 | "move 1 from 2 to 1", 14 | "move 3 from 1 to 3", 15 | "move 2 from 2 to 1", 16 | "move 1 from 1 to 2", 17 | ] 18 | 19 | def test_part_1(self): 20 | self.assertEqual('CMZ', Dec05(instructions=self.data).part_1()) 21 | 22 | def test_part_2(self): 23 | self.assertEqual('MCD', Dec05(instructions=self.data).part_2()) 24 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec12.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec12 import Dec12 4 | 5 | 6 | class TestDec12(unittest.TestCase): 7 | data = """0: 8 | ### 9 | ##. 10 | ##. 11 | 12 | 1: 13 | ### 14 | ##. 15 | .## 16 | 17 | 2: 18 | .## 19 | ### 20 | ##. 21 | 22 | 3: 23 | ##. 24 | ### 25 | ##. 26 | 27 | 4: 28 | ### 29 | #.. 30 | ### 31 | 32 | 5: 33 | ### 34 | .#. 35 | ### 36 | 37 | 4x4: 0 0 0 0 2 0 38 | 12x5: 1 0 1 0 2 2 39 | 12x5: 1 0 1 0 3 2""".splitlines() 40 | 41 | def test_part_1(self): 42 | self.assertEqual(2, Dec12(instructions=self.data).part_1()) 43 | 44 | 45 | 46 | if __name__ == '__main__': 47 | unittest.main() 48 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec22.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec22 4 | 5 | 6 | class TestVirus(unittest.TestCase): 7 | def test_virus(self): 8 | puzzle_input = ['..#', '#..', '...'] 9 | v = dec22.Virus(puzzle_input) 10 | v.step(7) 11 | self.assertEqual(5, v.infect) 12 | v.step(63) 13 | self.assertEqual(41, v.infect) 14 | v.step(9930) 15 | self.assertEqual(5587, v.infect) 16 | 17 | def test_evolved_virus(self): 18 | puzzle_input = ['..#', '#..', '...'] 19 | v = dec22.EvolvedVirus(puzzle_input) 20 | v.step(100) 21 | self.assertEqual(26, v.infect) 22 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec16.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec16 import Dec16 4 | 5 | 6 | class TestDec16(unittest.TestCase): 7 | 8 | def test_error_rate(self): 9 | notes = ["class: 1-3 or 5-7", 10 | "row: 6-11 or 33-44", 11 | "seat: 13-40 or 45-50", 12 | "", 13 | "your ticket:", 14 | "7,1,14", 15 | "", 16 | "nearby tickets:", 17 | "7,3,47", 18 | "40,4,50", 19 | "55,2,20", 20 | "38,6,12"] 21 | self.assertEqual(71, Dec16(instructions=notes).part_1()) 22 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec05.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec05 import Dec05 4 | 5 | 6 | class TestDec05(unittest.TestCase): 7 | data = [ 8 | "0,9 -> 5,9", 9 | "8,0 -> 0,8", 10 | "9,4 -> 3,4", 11 | "2,2 -> 2,1", 12 | "7,0 -> 7,4", 13 | "6,4 -> 2,0", 14 | "0,9 -> 2,9", 15 | "3,4 -> 1,4", 16 | "0,0 -> 8,8", 17 | "5,5 -> 8,2" 18 | ] 19 | 20 | def test_overlap(self): 21 | self.assertEqual(5, Dec05(instructions=self.data).part_1()) 22 | 23 | def test_overlap_with_diagonals(self): 24 | self.assertEqual(12, Dec05(instructions=self.data).part_2()) 25 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec11.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec11 import Dec11 4 | 5 | 6 | class TestDec11(unittest.TestCase): 7 | seat_layout = [ 8 | "L.LL.LL.LL", 9 | "LLLLLLL.LL", 10 | "L.L.L..L..", 11 | "LLLL.LL.LL", 12 | "L.LL.LL.LL", 13 | "L.LLLLL.LL", 14 | "..L.L.....", 15 | "LLLLLLLLLL", 16 | "L.LLLLLL.L", 17 | "L.LLLLL.LL" 18 | ] 19 | 20 | def test_part_1_a(self): 21 | self.assertEqual(37, Dec11(instructions=self.seat_layout).part_1()) 22 | 23 | def test_part_2(self): 24 | self.assertEqual(26, Dec11(instructions=self.seat_layout).part_2()) 25 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec03.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec03 import Dec03 4 | 5 | 6 | class TestDec03(unittest.TestCase): 7 | data = [ 8 | "00100", 9 | "11110", 10 | "10110", 11 | "10111", 12 | "10101", 13 | "01111", 14 | "00111", 15 | "11100", 16 | "10000", 17 | "11001", 18 | "00010", 19 | "01010" 20 | ] 21 | 22 | def test_power_consumption(self): 23 | self.assertEqual(198, Dec03(instructions=self.data).part_1()) 24 | 25 | def test_life_support_rating(self): 26 | self.assertEqual(230, Dec03(instructions=self.data).part_2()) 27 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec12.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec12 import Dec12 4 | 5 | 6 | class TestDec12(unittest.TestCase): 7 | data = [ 8 | "???.### 1,1,3", 9 | ".??..??...?##. 1,1,3", 10 | "?#?#?#?#?#?#?#? 1,3,1,6", 11 | "????.#...#... 4,1,1", 12 | "????.######..#####. 1,6,5", 13 | "?###???????? 3,2,1" 14 | ] 15 | 16 | def test_part_1(self): 17 | self.assertEqual(21, Dec12(instructions=self.data).part_1()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(525152, Dec12(instructions=self.data).part_2()) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec06.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec06 import Dec06 4 | 5 | 6 | class TestDec06(unittest.TestCase): 7 | data = [ 8 | "abc", 9 | "", 10 | "a", 11 | "b", 12 | "c", 13 | "", 14 | "ab", 15 | "ac", 16 | "", 17 | "a", 18 | "a", 19 | "a", 20 | "a", 21 | "", 22 | "b" 23 | ] 24 | 25 | def test_count_any_yes_groups(self): 26 | self.assertEqual(11, Dec06(instructions=self.data).part_1()) 27 | 28 | def test_count_all_yes_groups(self): 29 | self.assertEqual(6, Dec06(instructions=self.data).part_2()) 30 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec11.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec11 import Dec11 4 | 5 | 6 | class TestDec11(unittest.TestCase): 7 | 8 | data = [ 9 | "5483143223", 10 | "2745854711", 11 | "5264556173", 12 | "6141336146", 13 | "6357385478", 14 | "4167524645", 15 | "2176841721", 16 | "6882881134", 17 | "4846848554", 18 | "5283751526" 19 | ] 20 | 21 | def test_number_of_flashes(self): 22 | self.assertEqual(1656, Dec11(instructions=self.data).part_1()) 23 | 24 | def test_syncronize_at(self): 25 | self.assertEqual(195, Dec11(instructions=self.data).part_2()) 26 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec02.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec02 import Dec02, Dec02_2 4 | 5 | 6 | class TestDec02(unittest.TestCase): 7 | data = [ 8 | "A Y", 9 | "B X", 10 | "C Z" 11 | ] 12 | 13 | def test_part1_1(self): 14 | self.assertEqual(15, Dec02(instructions=self.data).part_1()) 15 | 16 | def test_part2_1(self): 17 | self.assertEqual(12, Dec02(instructions=self.data).part_2()) 18 | 19 | def test_part1_2(self): 20 | self.assertEqual(15, Dec02_2(instructions=self.data).part_1()) 21 | 22 | def test_part2_2(self): 23 | self.assertEqual(12, Dec02_2(instructions=self.data).part_2()) 24 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec13.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec13 import Dec13 4 | 5 | 6 | class TestDec13(unittest.TestCase): 7 | data = """[1,1,3,1,1] 8 | [1,1,5,1,1] 9 | 10 | [[1],[2,3,4]] 11 | [[1],4] 12 | 13 | [9] 14 | [[8,7,6]] 15 | 16 | [[4,4],4,4] 17 | [[4,4],4,4,4] 18 | 19 | [7,7,7,7] 20 | [7,7,7] 21 | 22 | [] 23 | [3] 24 | 25 | [[[]]] 26 | [[]] 27 | 28 | [1,[2,[3,[4,[5,6,7]]]],8,9] 29 | [1,[2,[3,[4,[5,6,0]]]],8,9]""".split('\n') 30 | 31 | def test_part_1(self): 32 | self.assertEqual(13, Dec13(instructions=self.data).part_1()) 33 | 34 | def test_part_2(self): 35 | self.assertEqual(140, Dec13(instructions=self.data).part_2()) 36 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec02.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import unittest 3 | 4 | from python.src.y2015 import dec02 5 | 6 | __author__ = 'anna' 7 | 8 | 9 | class Dec02Tests(unittest.TestCase): 10 | def setUp(self): 11 | pass 12 | 13 | def testWrapExample1(self): 14 | self.assertEqual(58, dec02.wrap(2, 3, 4)) 15 | 16 | def testWrapExample2(self): 17 | self.assertEqual(43, dec02.wrap(1, 1, 10)) 18 | 19 | def testRibbonExample1(self): 20 | self.assertEqual(34, dec02.ribbon(2, 3, 4)) 21 | 22 | def testRibbonExample2(self): 23 | self.assertEqual(14, dec02.ribbon(1, 1, 10)) 24 | 25 | 26 | if __name__ == '__main__': 27 | unittest.main() 28 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec01.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec01 import Dec01 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | data = [ 8 | "1000", 9 | "2000", 10 | "3000", 11 | "", 12 | "4000", 13 | "", 14 | "5000", 15 | "6000", 16 | "", 17 | "7000", 18 | "8000", 19 | "9000", 20 | "", 21 | "10000" 22 | ] 23 | 24 | def test_carrying_most_calories(self): 25 | self.assertEqual(24000, Dec01(instructions=self.data).part_1()) 26 | 27 | def test_sum_top_three_calories(self): 28 | self.assertEqual(45000, Dec01(instructions=self.data).part_2()) 29 | -------------------------------------------------------------------------------- /python/src/y2015/dec10.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Anna' 2 | 3 | 4 | def look_and_say(sequence): 5 | output = [] 6 | current = sequence[0] 7 | count = 1 8 | for char in sequence[1:]: 9 | if char == current: 10 | count += 1 11 | else: 12 | output.extend([str(count), current]) 13 | count = 1 14 | current = char 15 | output.extend([str(count), current]) 16 | return ''.join(output) 17 | 18 | 19 | if __name__ == '__main__': 20 | seq = '1113222113' 21 | for x in range(0, 40): 22 | seq = look_and_say(seq) 23 | print(len(seq)) 24 | for x in range(0, 10): 25 | seq = look_and_say(seq) 26 | print(len(seq)) 27 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec12.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec12 5 | 6 | 7 | class Dec12Tests(unittest.TestCase): 8 | def setUp(self): 9 | self.instructions = ["cpy 41 a", 10 | "inc a", 11 | "inc a", 12 | "dec a", 13 | "jnz a 2", 14 | "dec a"] 15 | self.computer = dec12.Computer(self.instructions) 16 | 17 | def test_part_1(self): 18 | self.computer.run() 19 | self.assertEqual(42, self.computer.register('a')) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec12.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec12 4 | 5 | 6 | class TestDigitalPlumber(unittest.TestCase): 7 | 8 | def test_find_groups(self): 9 | puzzle_input = ["0 <-> 2", 10 | "1 <-> 1", 11 | "2 <-> 0, 3, 4", 12 | "3 <-> 2, 4", 13 | "4 <-> 2, 3, 6", 14 | "5 <-> 6", 15 | "6 <-> 4, 5"] 16 | 17 | groups = dec12.find_groups(puzzle_input) 18 | self.assertEqual(6, len(groups.get(0))) 19 | self.assertEqual(2, dec12.count_groups(groups)) 20 | self.assertSetEqual({0, 2, 3, 4, 5, 6}, groups.get(0)) 21 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec04.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec04 import Dec04 4 | 5 | 6 | class TestDec04(unittest.TestCase): 7 | data = [ 8 | "MMMSXXMASM", 9 | "MSAMXMSMSA", 10 | "AMXSXMAAMM", 11 | "MSAMASMSMX", 12 | "XMASAMXAMM", 13 | "XXAMMXXAMA", 14 | "SMSMSASXSS", 15 | "SAXAMASAAA", 16 | "MAMMMXMMMM", 17 | "MXMXAXMASX" 18 | ] 19 | 20 | def test_part_1(self): 21 | self.assertEqual(18, Dec04(instructions=self.data).part_1()) 22 | 23 | def test_part_2(self): 24 | self.assertEqual(9, Dec04(instructions=self.data).part_2()) 25 | 26 | 27 | if __name__ == '__main__': 28 | unittest.main() 29 | -------------------------------------------------------------------------------- /python/test/y2019/test_dec08.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2019 import dec08 4 | 5 | 6 | class TestDec08(unittest.TestCase): 7 | def test_create_image(self): 8 | pixels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2] 9 | 10 | image = dec08.Image(width=3, height=2, pixels=pixels) 11 | self.assertEqual(2, image.no_layers) 12 | self.assertEqual([1, 2, 3, 4, 5, 6], image.layers[0]) 13 | 14 | def test_render_image(self): 15 | pixels = [0, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 2, 0, 0, 0, 0] 16 | image = dec08.Image(width=2, height=2, pixels=pixels) 17 | self.assertEqual([0, 1, 1, 0], image.render()) 18 | 19 | 20 | if __name__ == '__main__': 21 | unittest.main() 22 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec19.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec19 import Dec19 4 | 5 | 6 | class TestDec19(unittest.TestCase): 7 | data = [ 8 | """Blueprint 1: 9 | Each ore robot costs 4 ore. 10 | Each clay robot costs 2 ore. 11 | Each obsidian robot costs 3 ore and 14 clay. 12 | Each geode robot costs 2 ore and 7 obsidian.""".replace('\n', ''), 13 | """Blueprint 2: 14 | Each ore robot costs 2 ore. 15 | Each clay robot costs 3 ore. 16 | Each obsidian robot costs 3 ore and 8 clay. 17 | Each geode robot costs 3 ore and 12 obsidian.""".replace('\n', '') 18 | ] 19 | 20 | def test_part_1(self): 21 | self.assertEqual(33, Dec19(instructions=self.data).part_1()) 22 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec11.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec11 import Dec11, StoneCorridor 4 | 5 | 6 | class TestDec11(unittest.TestCase): 7 | 8 | def test_blink(self): 9 | result = StoneCorridor([0, 1, 10, 99, 999]).blink(1) 10 | self.assertEqual(7, result) 11 | 12 | def test_blink_6(self): 13 | result = StoneCorridor([125, 17]).blink(6) 14 | self.assertEqual(22, result) 15 | 16 | def test_part_1(self): 17 | self.assertEqual(55312, Dec11(instructions=['125 17']).part_1()) 18 | 19 | def atest_part_2(self): 20 | self.assertEqual(0, Dec11(instructions=self.data).part_2()) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/src/y2015/dec25.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anna' 2 | 3 | """ To continue, please consult the code grid in the manual. 4 | Enter the code at row 2947, column 3029. """ 5 | 6 | 7 | def next_cell(): 8 | row = 0 9 | while True: 10 | row += 1 11 | for col in range(1, row + 1): 12 | yield row - col + 1, col 13 | 14 | 15 | def code(row_index, col_index): 16 | the_code = 20151125 17 | for row, col in next_cell(): 18 | if row == row_index and col == col_index: 19 | break 20 | the_code = divmod(the_code * 252533, 33554393)[1] 21 | return the_code 22 | 23 | 24 | def main(): 25 | print(code(2947, 3029)) 26 | 27 | 28 | if __name__ == '__main__': 29 | main() 30 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec06.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018 import dec06 4 | 5 | 6 | class TestDec06(unittest.TestCase): 7 | def test_part_1(self): 8 | coordinates = [ 9 | "1, 1", 10 | "1, 6", 11 | "8, 3", 12 | "3, 4", 13 | "5, 5", 14 | "8, 9" 15 | ] 16 | 17 | self.assertEqual(17, dec06.Dec06(coordinates).part_1()) 18 | 19 | def test_part_2(self): 20 | coordinates = [ 21 | "1, 1", 22 | "1, 6", 23 | "8, 3", 24 | "3, 4", 25 | "5, 5", 26 | "8, 9" 27 | ] 28 | self.assertEqual(16, dec06.Dec06(coordinates).part_2(32)) 29 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec13.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec13 import Dec13 4 | 5 | 6 | class TestDec13(unittest.TestCase): 7 | data = [ 8 | "6,10", 9 | "0,14", 10 | "9,10", 11 | "0,3", 12 | "10,4", 13 | "4,11", 14 | "6,0", 15 | "6,12", 16 | "4,1", 17 | "0,13", 18 | "10,12", 19 | "3,4", 20 | "3,0", 21 | "8,4", 22 | "1,10", 23 | "2,14", 24 | "8,10", 25 | "9,0", 26 | "", 27 | "fold along y=7", 28 | "fold along x=5" 29 | ] 30 | 31 | def test_visible_dots(self): 32 | self.assertEqual(17, Dec13(instructions=self.data).part_1()) 33 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec03.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec03 import Dec03 4 | 5 | 6 | class TestDec03(unittest.TestCase): 7 | data = [ 8 | "467..114..", 9 | "...*......", 10 | "..35..633.", 11 | "......#...", 12 | "617*......", 13 | ".....+.58.", 14 | "..592.....", 15 | "......755.", 16 | "...$.*....", 17 | ".664.598.." 18 | ] 19 | 20 | def test_part_1(self): 21 | self.assertEqual(4361, Dec03(instructions=self.data).part_1()) 22 | 23 | def test_part_2(self): 24 | self.assertEqual(467835, Dec03(instructions=self.data).part_2()) 25 | 26 | 27 | if __name__ == '__main__': 28 | unittest.main() 29 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec16.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec16 import Dec16 4 | 5 | 6 | class TestDec16(unittest.TestCase): 7 | data = [ 8 | ".|...\\....", 9 | "|.-.\\.....", 10 | ".....|-...", 11 | "........|.", 12 | "..........", 13 | ".........\\", 14 | "..../.\\\\..", 15 | ".-.-/..|..", 16 | ".|....-|.\\", 17 | "..//.|...." 18 | 19 | ] 20 | 21 | def test_part_1(self): 22 | self.assertEqual(46, Dec16(instructions=self.data).part_1()) 23 | 24 | def test_part_2(self): 25 | self.assertEqual(51, Dec16(instructions=self.data).part_2()) 26 | 27 | 28 | if __name__ == '__main__': 29 | unittest.main() 30 | -------------------------------------------------------------------------------- /python/src/y2017/dec01.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from python.src.y2017.common import DATA_DIR 4 | 5 | 6 | def captcha_sum(number, halfway_sum=False): 7 | number = str(number) 8 | sum_doubles = 0 9 | nlen = len(number) 10 | step = nlen // 2 if halfway_sum else 1 11 | for n in range(nlen): 12 | next_position = (n + step) % nlen 13 | if number[n] == number[next_position]: 14 | sum_doubles += int(number[n]) 15 | return sum_doubles 16 | 17 | 18 | if __name__ == '__main__': 19 | with open(os.path.join(DATA_DIR, 'input.1.txt')) as fh: 20 | number = fh.read() 21 | print("Captcha sum: ", captcha_sum(number)) 22 | print("Halfway sum: ", captcha_sum(number, halfway_sum=True)) 23 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec25.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec25 import Dec25 4 | 5 | 6 | class TestDec25(unittest.TestCase): 7 | data = [ 8 | "jqt: rhn xhk nvd", 9 | "rsh: frs pzl lsr", 10 | "xhk: hfx", 11 | "cmg: qnr nvd lhk bvb", 12 | "rhn: xhk bvb hfx", 13 | "bvb: xhk hfx", 14 | "pzl: lsr hfx nvd", 15 | "qnr: nvd", 16 | "ntq: jqt hfx bvb xhk", 17 | "nvd: lhk", 18 | "lsr: lhk", 19 | "rzs: qnr cmg lsr rsh", 20 | "frs: qnr lhk lsr" 21 | ] 22 | 23 | def test_part_1(self): 24 | self.assertEqual(54, Dec25(instructions=self.data).part_1()) 25 | 26 | 27 | if __name__ == '__main__': 28 | unittest.main() 29 | -------------------------------------------------------------------------------- /python/test/y2019/test_dec01.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2019 import dec01 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | 8 | def test_module_mass(self): 9 | cases = [ 10 | (['12'], 2), 11 | (['14'], 2), 12 | (['1969'], 654), 13 | (['100756'], 33583) 14 | ] 15 | 16 | for case in cases: 17 | self.assertEqual(case[1], dec01.Dec01(case[0]).part_1()) 18 | 19 | def test_added_fuel_mass(self): 20 | cases = [ 21 | (['14'], 2), 22 | (['1969'], 966), 23 | (['100756'], 50346) 24 | ] 25 | 26 | for case in cases: 27 | self.assertEqual(case[1], dec01.Dec01(case[0]).part_2()) 28 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec03.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec03 import Dec03 4 | 5 | 6 | class TestDec03(unittest.TestCase): 7 | area_map = [ 8 | "..##.......", 9 | "#...#...#..", 10 | ".#....#..#.", 11 | "..#.#...#.#", 12 | ".#...##..#.", 13 | "..#.##.....", 14 | ".#.#.#....#", 15 | ".#........#", 16 | "#.##...#...", 17 | "#...##....#", 18 | ".#..#...#.#" 19 | 20 | ] 21 | 22 | def test_part_1(self): 23 | d = Dec03(instructions=self.area_map) 24 | self.assertEqual(7, d.part_1()) 25 | 26 | def test_part_2(self): 27 | d = Dec03(instructions=self.area_map) 28 | self.assertEqual(336, d.part_2()) 29 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec25.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec25 import Dec25 4 | 5 | 6 | class TestDec25(unittest.TestCase): 7 | data = """##### 8 | .#### 9 | .#### 10 | .#### 11 | .#.#. 12 | .#... 13 | ..... 14 | 15 | ##### 16 | ##.## 17 | .#.## 18 | ...## 19 | ...#. 20 | ...#. 21 | ..... 22 | 23 | ..... 24 | #.... 25 | #.... 26 | #...# 27 | #.#.# 28 | #.### 29 | ##### 30 | 31 | ..... 32 | ..... 33 | #.#.. 34 | ###.. 35 | ###.# 36 | ###.# 37 | ##### 38 | 39 | ..... 40 | ..... 41 | ..... 42 | #.... 43 | #.#.. 44 | #.#.# 45 | #####""".splitlines() 46 | 47 | def test_part_1(self): 48 | self.assertEqual(3, Dec25(instructions=self.data).part_1()) 49 | 50 | 51 | if __name__ == '__main__': 52 | unittest.main() 53 | -------------------------------------------------------------------------------- /python/test/y2019/test_dec04.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2019 import dec04 4 | 5 | 6 | class TestDec03(unittest.TestCase): 7 | def test_valid_passwords(self): 8 | cases = [ 9 | ('111111', True), 10 | ('223450', False), 11 | ('123789', False) 12 | ] 13 | 14 | for case in cases: 15 | self.assertEqual(case[1], dec04.is_valid_password(case[0])) 16 | 17 | def test_valid_passwords_part_2(self): 18 | cases = [ 19 | ('112233', True), 20 | ('123444', False), 21 | ('111122', True) 22 | ] 23 | 24 | for case in cases: 25 | self.assertEqual(case[1], dec04.is_valid_password(case[0], max_repeated=2)) 26 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec09.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anna' 2 | 3 | import unittest 4 | 5 | from python.src.y2015 import dec09 6 | 7 | 8 | class Dec09Tests(unittest.TestCase): 9 | def test_shortest_path(self): 10 | distances = ["London to Dublin = 464", 11 | "London to Belfast = 518", 12 | "Dublin to Belfast = 141"] 13 | self.assertEqual(605, dec09.shortest_path(distances)) 14 | 15 | def test_longest_path(self): 16 | distances = ["London to Dublin = 464", 17 | "London to Belfast = 518", 18 | "Dublin to Belfast = 141"] 19 | self.assertEqual(982, dec09.longest_path(distances)) 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec16.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec16 import Dec16 4 | 5 | 6 | class TestDec16(unittest.TestCase): 7 | data = """############### 8 | #.......#....E# 9 | #.#.###.#.###.# 10 | #.....#.#...#.# 11 | #.###.#####.#.# 12 | #.#.#.......#.# 13 | #.#.#####.###.# 14 | #...........#.# 15 | ###.#.#####.#.# 16 | #...#.....#.#.# 17 | #.#.#.###.#.#.# 18 | #.....#...#.#.# 19 | #.###.#.#.#.#.# 20 | #S..#.....#...# 21 | ###############""".split('\n') 22 | 23 | def test_part_1(self): 24 | self.assertEqual(7036, Dec16(instructions=self.data).part_1()) 25 | 26 | def test_part_2(self): 27 | self.assertEqual(45, Dec16(instructions=self.data).part_2()) 28 | 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec16.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec16 5 | 6 | 7 | class Dec16Tests(unittest.TestCase): 8 | def test_dragon_curve(self): 9 | self.assertEqual("100", dec16.dragon_curve("1")) 10 | self.assertEqual("001", dec16.dragon_curve("0")) 11 | self.assertEqual("11111000000", dec16.dragon_curve("11111")) 12 | self.assertEqual("1111000010100101011110000", dec16.dragon_curve("111100001010")) 13 | 14 | def test_checksum(self): 15 | self.assertEqual("100", dec16.checksum("110010110100")) 16 | 17 | def test_checksum_of(self): 18 | self.assertEqual("01100", dec16.checksum_of("10000", 20)) 19 | 20 | 21 | if __name__ == '__main__': 22 | unittest.main() 23 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec24.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec24 import Dec24, Hailstorm 4 | 5 | 6 | class TestDec24(unittest.TestCase): 7 | data = [ 8 | "19, 13, 30 @ -2, 1, -2", 9 | "18, 19, 22 @ -1, -1, -2", 10 | "20, 25, 34 @ -2, -2, -4", 11 | "12, 31, 28 @ -1, -2, -1", 12 | "20, 19, 15 @ 1, -5, -3" 13 | ] 14 | 15 | def test_part_1(self): 16 | day = Dec24(instructions=self.data) 17 | self.assertEqual(2, Hailstorm(day.instructions).count_intersections( 18 | min_pos=7, max_pos=27 19 | )) 20 | 21 | def test_part_2(self): 22 | self.assertEqual(47, Dec24(instructions=self.data).part_2()) 23 | 24 | 25 | if __name__ == '__main__': 26 | unittest.main() 27 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec23.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec23 5 | 6 | 7 | class Dec23Tests(unittest.TestCase): 8 | def setUp(self): 9 | self.instructions = ["cpy 2 a", 10 | "tgl a", 11 | "tgl a", 12 | "tgl a", 13 | "cpy 1 a", 14 | "dec a", 15 | "dec a"] 16 | self.computer = dec23.Computer(self.instructions) 17 | 18 | def test_assembunny(self): 19 | self.computer.run() 20 | self.assertEqual(3, 21 | self.computer.register('a')) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec13.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec13 import Dec13 4 | 5 | 6 | class TestDec13(unittest.TestCase): 7 | data = """Button A: X+94, Y+34 8 | Button B: X+22, Y+67 9 | Prize: X=8400, Y=5400 10 | 11 | Button A: X+26, Y+66 12 | Button B: X+67, Y+21 13 | Prize: X=12748, Y=12176 14 | 15 | Button A: X+17, Y+86 16 | Button B: X+84, Y+37 17 | Prize: X=7870, Y=6450 18 | 19 | Button A: X+69, Y+23 20 | Button B: X+27, Y+71 21 | Prize: X=18641, Y=10279""".split('\n') 22 | 23 | def test_part_1(self): 24 | self.assertEqual(480, Dec13(instructions=self.data).part_1()) 25 | 26 | def test_part_2(self): 27 | self.assertEqual(0, Dec13(instructions=self.data).part_2()) 28 | 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec07.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec07 import Dec07 4 | 5 | 6 | class TestDec07(unittest.TestCase): 7 | data = """.......S....... 8 | ............... 9 | .......^....... 10 | ............... 11 | ......^.^...... 12 | ............... 13 | .....^.^.^..... 14 | ............... 15 | ....^.^...^.... 16 | ............... 17 | ...^.^...^.^... 18 | ............... 19 | ..^...^.....^.. 20 | ............... 21 | .^.^.^.^.^...^. 22 | ...............""".splitlines() 23 | 24 | def test_part_1(self): 25 | self.assertEqual(21, Dec07(instructions=self.data).part_1()) 26 | 27 | def test_part_2(self): 28 | self.assertEqual(40, Dec07(instructions=self.data).part_2()) 29 | 30 | 31 | if __name__ == '__main__': 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /python/src/y2017/dec04.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from python.src.y2017.common import DATA_DIR 4 | 5 | 6 | def valid_passphrases(puzzle_input, anagram=False): 7 | return len(list(filter(lambda x: is_valid(x, anagram), puzzle_input))) 8 | 9 | 10 | def is_valid(passphrase, anagram=False): 11 | words = passphrase.split() 12 | if anagram: 13 | words = [''.join(sorted(word)) for word in words] 14 | return len(set(words)) == len(words) 15 | 16 | 17 | if __name__ == '__main__': 18 | with open(os.path.join(DATA_DIR, 'input.4.txt')) as fh: 19 | puzzle_input = fh.readlines() 20 | print("# of valid passphrases: {}".format(valid_passphrases(puzzle_input, False))) 21 | print("# of valid anagram passhprases: {}".format(valid_passphrases(puzzle_input, True))) 22 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec09 import Dec09 4 | 5 | 6 | class TestDec09(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | examples = [ 10 | ("9 players; last marble is worth 25 points", 32), 11 | ("10 players; last marble is worth 1618 points", 8317), 12 | ("13 players; last marble is worth 7999 points", 146373), 13 | ("17 players; last marble is worth 1104 points", 2764), 14 | ("21 players; last marble is worth 6111 points", 54718), 15 | ("30 players; last marble is worth 5807 points", 37305) 16 | ] 17 | 18 | for example, result in examples: 19 | d = Dec09(instructions=[example]) 20 | self.assertEqual(result, d.part_1()) 21 | -------------------------------------------------------------------------------- /python/src/y2022/dec01.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec01(Day, year=2022, day=1): 5 | 6 | @staticmethod 7 | def parse_instructions(instructions): 8 | return [ 9 | [int(r) for r in g] 10 | for g in Day.parse_groups(instructions=instructions) 11 | ] 12 | 13 | @timer(part=1, title='Most calories carried by one elf') 14 | def part_1(self): 15 | return max(sum(g) for g in self.instructions) 16 | 17 | @timer(part=2, title='Total calories carried by top three elves') 18 | def part_2(self): 19 | elves = [sum(g) for g in self.instructions] 20 | return sum(sorted(elves, reverse=True)[:3]) 21 | 22 | 23 | if __name__ == '__main__': 24 | with Timer('Total'): 25 | Dec01().run_day() 26 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec14.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec14 import Dec14 4 | 5 | 6 | class TestDec14(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | 10 | cases = [ 11 | (9, "5158916779"), 12 | (5, "0124515891"), 13 | (18, "9251071085"), 14 | (2018, "5941429882") 15 | ] 16 | 17 | for num_recipes, result in cases: 18 | self.assertEqual(result, Dec14(num_recipes).part_1()) 19 | 20 | def test_part_2(self): 21 | cases = [ 22 | ("51589", 9), 23 | ("01245", 5), 24 | ("92510", 18), 25 | ("59414", 2018) 26 | ] 27 | 28 | for num_recipes, result in cases: 29 | self.assertEqual(result, Dec14(num_recipes).part_2()) 30 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec06.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec06 import Dec06 4 | 5 | 6 | class TestDec06(unittest.TestCase): 7 | data = [ 8 | ('mjqjpqmgbljsphdztnvjfqwrcgsmlb', 7, 19), 9 | ('bvwbjplbgvbhsrlpgdmjqwftvncz', 5, 23), 10 | ('nppdvjthqldpwncqszvftbrmjlhg', 6, 23), 11 | ('nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg', 10, 29), 12 | ('zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw', 11, 26) 13 | ] 14 | 15 | def test_find_start_of_packet(self): 16 | for message, part1, _ in self.data: 17 | self.assertEqual(part1, Dec06(instructions=[message]).part_1()) 18 | 19 | def test_find_start_of_message(self): 20 | for message, _, part2 in self.data: 21 | self.assertEqual(part2, Dec06(instructions=[message]).part_2()) 22 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec18.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec18 import Dec18, Computer 4 | 5 | 6 | class TestDec18(unittest.TestCase): 7 | data = [(5, 4), (4, 2), (4, 5), (3, 0), (2, 1), (6, 3), 8 | (2, 4), (1, 5), (0, 6), (3, 3), (2, 6), (5, 1), 9 | (1, 2), (5, 5), (2, 5), (6, 5), (1, 4), (0, 4), 10 | (6, 4), (1, 1), (6, 1), (1, 0), (0, 5), (1, 6), (2, 0)] 11 | 12 | def test_part_1(self): 13 | c = Computer(max_y=7, max_x=7, falling=self.data) 14 | c.corrupt(12) 15 | self.assertEqual(22, c.safe_path()) 16 | 17 | def test_part_2(self): 18 | c = Computer(max_y=7, max_x=7, falling=self.data) 19 | self.assertEqual((6,1), c.bisect(0)) 20 | 21 | if __name__ == '__main__': 22 | unittest.main() 23 | -------------------------------------------------------------------------------- /python/src/y2015/dec04.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Anna' 2 | from hashlib import md5 3 | 4 | SECRET = 'ckczppom' 5 | 6 | 7 | def mining_hashes(secret, zeroes=5): 8 | """ 9 | Yay bruteforce. 10 | :param secret: 11 | :param zeroes: 12 | :return: Lowest decimal number yielding a md5 hash starting with the specified number of zeroes 13 | """ 14 | number = 0 15 | hashcode = '' 16 | z = '0' * zeroes 17 | while not hashcode.startswith(z): 18 | number += 1 19 | value = ("%s%d" % (secret, number)).encode('utf-8') 20 | hashcode = md5(value).hexdigest() 21 | 22 | return number 23 | 24 | 25 | if __name__ == '__main__': 26 | print("Number with five leading zeroes:", mining_hashes(SECRET)) 27 | print("Number with six leading zeroes:", mining_hashes(SECRET, 6)) 28 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec10.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Anna' 2 | 3 | import unittest 4 | 5 | from python.src.y2015 import dec10 6 | 7 | 8 | class Dec10Tests(unittest.TestCase): 9 | 10 | def test_look_and_say_example1(self): 11 | self.assertEqual('11', dec10.look_and_say('1')) 12 | 13 | def test_look_and_say_example2(self): 14 | self.assertEqual('21', dec10.look_and_say('11')) 15 | 16 | def test_look_and_say_example3(self): 17 | self.assertEqual('1211', dec10.look_and_say('21')) 18 | 19 | def test_look_and_say_example4(self): 20 | self.assertEqual('111221', dec10.look_and_say('1211')) 21 | 22 | def test_look_and_say_example5(self): 23 | self.assertEqual('312211', dec10.look_and_say('111221')) 24 | 25 | 26 | if __name__ == '__main__': 27 | unittest.main() 28 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec14.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec14 import Dec14 4 | 5 | 6 | class TestDec14(unittest.TestCase): 7 | 8 | def test_initialization(self): 9 | instructions = [ 10 | "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X", 11 | "mem[8] = 11", 12 | "mem[7] = 101", 13 | "mem[8] = 0" 14 | ] 15 | self.assertEqual(165, Dec14(instructions=instructions).part_1()) 16 | 17 | def test_floating(self): 18 | instructions = [ 19 | "mask = 000000000000000000000000000000X1001X", 20 | "mem[42] = 100", 21 | "mask = 00000000000000000000000000000000X0XX", 22 | "mem[26] = 1" 23 | ] 24 | self.assertEqual(208, Dec14(instructions=instructions).part_2()) 25 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec23.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec23 import Dec23 4 | 5 | 6 | class TestDec23(unittest.TestCase): 7 | data = """kh-tc 8 | qp-kh 9 | de-cg 10 | ka-co 11 | yn-aq 12 | qp-ub 13 | cg-tb 14 | vc-aq 15 | tb-ka 16 | wh-tc 17 | yn-cg 18 | kh-ub 19 | ta-co 20 | de-co 21 | tc-td 22 | tb-wq 23 | wh-td 24 | ta-ka 25 | td-qp 26 | aq-cg 27 | wq-ub 28 | ub-vc 29 | de-ta 30 | wq-aq 31 | wq-vc 32 | wh-yn 33 | ka-de 34 | kh-ta 35 | co-tc 36 | wh-qp 37 | tb-vc 38 | td-yn""".splitlines() 39 | 40 | def test_part_1(self): 41 | self.assertEqual(7, Dec23(instructions=self.data).part_1()) 42 | 43 | def test_part_2(self): 44 | self.assertEqual('co,de,ka,ta', Dec23(instructions=self.data).part_2()) 45 | 46 | 47 | if __name__ == '__main__': 48 | unittest.main() 49 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec11.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec11 4 | 5 | 6 | class TestHex(unittest.TestCase): 7 | 8 | @classmethod 9 | def setUpClass(cls): 10 | cls.puzzle_inputs = [ 11 | ("ne,ne,ne", 3), 12 | ("ne,ne,sw,sw", 0), 13 | ("se,sw,se,sw,sw", 3) 14 | ] 15 | 16 | def test_naive_hex(self): 17 | for puzzle, expected in self.puzzle_inputs: 18 | h = dec11.NaiveHex() 19 | steps, max_steps = h.solve(puzzle) 20 | self.assertEqual(expected, steps) 21 | 22 | def test_quicker_hex(self): 23 | for puzzle, expected in self.puzzle_inputs: 24 | h = dec11.QuickerHex() 25 | steps, max_steps = h.solve(puzzle) 26 | self.assertEqual(expected, steps) 27 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec20.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec20 import Dec20 4 | 5 | 6 | class TestDec20(unittest.TestCase): 7 | example_1 = [ 8 | "broadcaster -> a, b, c", 9 | "%a -> b", 10 | "%b -> c", 11 | "%c -> inv", 12 | "&inv -> a" 13 | ] 14 | 15 | example_2 = [ 16 | "broadcaster -> a", 17 | "%a -> inv, con", 18 | "&inv -> b", 19 | "%b -> con", 20 | "&con -> output" 21 | ] 22 | 23 | def test_part_1(self): 24 | self.assertEqual(32000000, 25 | Dec20(instructions=self.example_1).part_1()) 26 | self.assertEqual(11687500, 27 | Dec20(instructions=self.example_2).part_1()) 28 | 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /python/test/y2019/test_dec02.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2019.intcode import IntCode 4 | 5 | 6 | class TestDec02(unittest.TestCase): 7 | def test_intcode(self): 8 | cases = [ 9 | ([1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50], 10 | [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]), 11 | ([1, 0, 0, 0, 99], 12 | [2, 0, 0, 0, 99]), 13 | ([2, 3, 0, 3, 99], 14 | [2, 3, 0, 6, 99]), 15 | ([2, 4, 4, 5, 99, 0], 16 | [2, 4, 4, 5, 99, 9801]), 17 | ([1, 1, 1, 4, 99, 5, 6, 0, 99], 18 | [30, 1, 1, 4, 2, 5, 6, 0, 99]) 19 | ] 20 | 21 | for case in cases: 22 | ic = IntCode(case[0]) 23 | ic.run() 24 | self.assertEqual(case[1], list(ic.data.values())) 25 | -------------------------------------------------------------------------------- /python/src/y2018/dec01.py: -------------------------------------------------------------------------------- 1 | from itertools import cycle 2 | from python.src.common import Day 3 | 4 | 5 | class Dec01(Day): 6 | def __init__(self, instructions=None): 7 | super().__init__(2018, 1, instructions) 8 | 9 | @staticmethod 10 | def parse_instructions(instructions): 11 | return list(map(int, instructions)) 12 | 13 | def part_1(self): 14 | return sum(self.instructions) 15 | 16 | def part_2(self): 17 | found = {0} 18 | current = 0 19 | for n in cycle(self.instructions): 20 | current += n 21 | if current in found: 22 | return current 23 | found.add(current) 24 | 25 | 26 | if __name__ == '__main__': 27 | d = Dec01() 28 | print("Frequency sum", d.part_1()) 29 | print("Captcha sum: ", d.part_2()) 30 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec02.py: -------------------------------------------------------------------------------- 1 | from python.src.y2016 import dec02 2 | import unittest 3 | 4 | __author__ = 'Anna Holmgren' 5 | 6 | 7 | class Dec02Tests(unittest.TestCase): 8 | def test_bathroom_code(self): 9 | self.assertEqual("1985", dec02.bathroom_code(["ULL", 10 | "RRDDD", 11 | "LURDL", 12 | "UUUUD"], dec02.Standard(5))) 13 | 14 | def test_bathroom_code_designed(self): 15 | self.assertEqual("5DB3", dec02.bathroom_code(["ULL", 16 | "RRDDD", 17 | "LURDL", 18 | "UUUUD"], dec02.Designed(5))) 19 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec20.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec20 4 | 5 | 6 | class TestSwarm(unittest.TestCase): 7 | 8 | def test_closest_particle(self): 9 | puzzle_input = ["p=<3,0,0>, v=<2,0,0>, a=<-1,0,0>", 10 | "p=<4,0,0>, v=<0,0,0>, a=<-2,0,0>"] 11 | s = dec20.Swarm(puzzle_input) 12 | self.assertEqual(0, s.get_closest()) 13 | 14 | def test_collide(self): 15 | puzzle_input = ["p=<-6,0,0>, v=< 3,0,0>, a=< 0,0,0>", 16 | "p=<-4,0,0>, v=< 2,0,0>, a=< 0,0,0>", 17 | "p=<-2,0,0>, v=< 1,0,0>, a=< 0,0,0>", 18 | "p=< 3,0,0>, v=<-1,0,0>, a=< 0,0,0>" 19 | ] 20 | s = dec20.Swarm(puzzle_input) 21 | self.assertEqual(1, s.get_particle_count()) 22 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec13.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec13 import Dec13 4 | 5 | 6 | class TestDec13(unittest.TestCase): 7 | data = [ 8 | "#.##..##.", 9 | "..#.##.#.", 10 | "##......#", 11 | "##......#", 12 | "..#.##.#.", 13 | "..##..##.", 14 | "#.#.##.#.", 15 | "", 16 | "#...##..#", 17 | "#....#..#", 18 | "..##..###", 19 | "#####.##.", 20 | "#####.##.", 21 | "..##..###", 22 | "#....#..#" 23 | ] 24 | 25 | def test_part_1(self): 26 | self.assertEqual(405, Dec13(instructions=self.data).part_1()) 27 | 28 | def test_part_2(self): 29 | self.assertEqual(400, Dec13(instructions=self.data).part_2()) 30 | 31 | 32 | if __name__ == '__main__': 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /python/src/y2019/dec05.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer 2 | from python.src.y2019.intcode import IntCode 3 | 4 | 5 | class Dec05(Day): 6 | 7 | def __init__(self, instructions=None, filename=None): 8 | super().__init__(2019, 5, instructions, filename) 9 | 10 | @staticmethod 11 | def parse_instructions(instructions): 12 | return Day.parse_int_line(instructions) 13 | 14 | @timer(part=1) 15 | def part_1(self): 16 | ic = IntCode(self.instructions) 17 | ic.add_input(1) 18 | ic.run() 19 | return ic.get_output() 20 | 21 | @timer(part=2) 22 | def part_2(self): 23 | ic = IntCode(self.instructions) 24 | ic.add_input(5) 25 | ic.run() 26 | return ic.get_output() 27 | 28 | 29 | if __name__ == '__main__': 30 | Dec05().run_day() 31 | -------------------------------------------------------------------------------- /python/src/y2020/dec05.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec05(Day, year=2020, day=5): 5 | @staticmethod 6 | def parse_instructions(instructions): 7 | return sorted([Dec05.seat_id(i) for i in instructions]) 8 | 9 | @staticmethod 10 | def seat_id(boarding_pass): 11 | row = boarding_pass.translate(str.maketrans('FBLR', '0101')) 12 | return int(row, 2) 13 | 14 | @timer(part=1) 15 | def part_1(self): 16 | return self.instructions[-1] 17 | 18 | @timer(part=2) 19 | def part_2(self): 20 | seats = self.instructions 21 | all_seats = set(range(seats[0], seats[-1] + 1)) 22 | return all_seats.difference(seats).pop() 23 | 24 | 25 | if __name__ == '__main__': 26 | with Timer('Binary Boarding'): 27 | Dec05().run_day() 28 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec04.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec04 import Dec04 4 | 5 | 6 | class TestDec04(unittest.TestCase): 7 | data = [ 8 | "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53", 9 | "Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19", 10 | "Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1", 11 | "Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83", 12 | "Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36", 13 | "Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11" 14 | ] 15 | 16 | def test_part_1(self): 17 | self.assertEqual(13, Dec04(instructions=self.data).part_1()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(30, Dec04(instructions=self.data).part_2()) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec17.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec17 import Dec17 4 | 5 | 6 | class TestDec17(unittest.TestCase): 7 | data = [ 8 | "2413432311323", 9 | "3215453535623", 10 | "3255245654254", 11 | "3446585845452", 12 | "4546657867536", 13 | "1438598798454", 14 | "4457876987766", 15 | "3637877979653", 16 | "4654967986887", 17 | "4564679986453", 18 | "1224686865563", 19 | "2546548887735", 20 | "4322674655533" 21 | ] 22 | 23 | def test_part_1(self): 24 | self.assertEqual(102, Dec17(instructions=self.data).part_1()) 25 | 26 | def test_part_2(self): 27 | self.assertEqual(94, Dec17(instructions=self.data).part_2()) 28 | 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /python/src/y2020/dec01.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | from python.src.common import Day, timer, Timer 4 | 5 | 6 | class Dec01(Day, year=2020, day=1): 7 | 8 | @staticmethod 9 | def parse_instructions(instructions): 10 | return sorted( 11 | int(row) for row in instructions 12 | ) 13 | 14 | @timer(part=1) 15 | def part_1(self): 16 | for pair in itertools.permutations(self.instructions, 2): 17 | if sum(pair) == 2020: 18 | return pair[0] * pair[1] 19 | 20 | @timer(part=2) 21 | def part_2(self): 22 | for pair in itertools.permutations(self.instructions, 3): 23 | if sum(pair) == 2020: 24 | return pair[0] * pair[1] * pair[2] 25 | 26 | 27 | if __name__ == '__main__': 28 | with Timer('Report Repair'): 29 | Dec01().run_day() 30 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec22.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Anna' 2 | 3 | import unittest 4 | from python.src.y2015 import dec22 5 | 6 | 7 | class Dec21Tests(unittest.TestCase): 8 | def test_part1_example1(self): 9 | player = dec22.Character("Player", hp=10, mana=250) 10 | boss = dec22.Character("Boss", hp=13, damage=8) 11 | game = dec22.play(player, boss) 12 | self.assertTrue(game.boss.dead()) 13 | self.assertEqual(226, game.player.spent) 14 | 15 | def test_part1_example2(self): 16 | player = dec22.Character("Player", hp=10, mana=250) 17 | boss = dec22.Character("Boss", hp=14, damage=8) 18 | game = dec22.play(player, boss) 19 | self.assertTrue(game.boss.dead()) 20 | self.assertEqual(641, game.player.spent) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec12.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec12 import Dec12 4 | 5 | 6 | class TestDec12(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | instructions = [ 10 | "initial state: #..#.#..##......###...###\n", 11 | "\n", 12 | "...## => #\n", 13 | "..#.. => #\n", 14 | ".#... => #\n", 15 | ".#.#. => #\n", 16 | ".#.## => #\n", 17 | ".##.. => #\n", 18 | ".#### => #\n", 19 | "#.#.# => #\n", 20 | "#.### => #\n", 21 | "##.#. => #\n", 22 | "##.## => #\n", 23 | "###.. => #\n", 24 | "###.# => #\n", 25 | "####. => #\n" 26 | ] 27 | 28 | d = Dec12(instructions) 29 | self.assertEqual(325, d.part_1()) 30 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec11.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec11 import Dec11 4 | 5 | 6 | class TestDec11(unittest.TestCase): 7 | data = """aaa: you hhh 8 | you: bbb ccc 9 | bbb: ddd eee 10 | ccc: ddd eee fff 11 | ddd: ggg 12 | eee: out 13 | fff: out 14 | ggg: out 15 | hhh: ccc fff iii 16 | iii: out""".splitlines() 17 | 18 | def test_part_1(self): 19 | self.assertEqual(5, Dec11(instructions=self.data).part_1()) 20 | 21 | def test_part_2(self): 22 | new_data = """svr: aaa bbb 23 | aaa: fft 24 | fft: ccc 25 | bbb: tty 26 | tty: ccc 27 | ccc: ddd eee 28 | ddd: hub 29 | hub: fff 30 | eee: dac 31 | dac: fff 32 | fff: ggg hhh 33 | ggg: out 34 | hhh: out""".splitlines() 35 | self.assertEqual(2, Dec11(instructions=new_data).part_2()) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec14.py: -------------------------------------------------------------------------------- 1 | from python.src.y2015 import dec14 2 | 3 | __author__ = 'anna' 4 | 5 | import unittest 6 | 7 | 8 | class Dec14Tests(unittest.TestCase): 9 | reindeer_rules = ['Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.', 10 | 'Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.'] 11 | 12 | def test_winner(self): 13 | winner = dec14.winner(self.reindeer_rules, 1000) 14 | self.assertEqual('Comet', winner.name) 15 | self.assertEqual(1120, winner.location) 16 | 17 | def test_winning_ticks(self): 18 | winner = dec14.winner_ticks(self.reindeer_rules, 1000) 19 | self.assertEqual('Dancer', winner.name) 20 | self.assertEqual(689, winner.score) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec10.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec10 3 | 4 | 5 | class TestKnotHash(unittest.TestCase): 6 | def test_check_first_two(self): 7 | kh = dec10.KnotHash.from_string_input("3,4,1,5", 5) 8 | kh.round() 9 | self.assertEqual(12, kh.check_first_two()) 10 | 11 | def test_checksum(self): 12 | test_cases = [ 13 | ("", "a2582a3a0e66e6e86e3812dcb672a272"), 14 | ("AoC 2017", "33efeb34ea91902bb2f59c9920caa6cd"), 15 | ("1,2,3", "3efbe78a8d82f29979031a4aa0b16a9d"), 16 | ("1,2,4", "63960835bcdc130f0b66d7ff4f6a5a8e") 17 | ] 18 | for lengths, expected_result in test_cases: 19 | kh = dec10.KnotHash.from_bytes_input(lengths) 20 | kh.run() 21 | self.assertEqual(expected_result, kh.checksum()) 22 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec15.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec15 4 | 5 | 6 | class TestDuelGenerators(unittest.TestCase): 7 | def test_find_first_match(self): 8 | gen_a = dec15.Generator(factor=16807, start=65) 9 | gen_b = dec15.Generator(factor=48271, start=8921) 10 | j = dec15.Judge(gen_a, gen_b) 11 | j.run(2) 12 | self.assertEqual(0, j.count) 13 | j.run(1) 14 | self.assertEqual(1, j.count) 15 | 16 | def test_find_first_count_modulo(self): 17 | gen_a = dec15.Generator(factor=16807, start=65, multiplier=4) 18 | gen_b = dec15.Generator(factor=48271, start=8921, multiplier=8) 19 | j = dec15.Judge(gen_a, gen_b) 20 | j.run(1055) 21 | self.assertEqual(0, j.count) 22 | j.run(1) 23 | self.assertEqual(1, j.count) 24 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec02.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec02 import Dec02, GiftShopDatabase 4 | 5 | 6 | class TestDec02(unittest.TestCase): 7 | data = ["11-22,95-115,998-1012,1188511880-1188511890," 8 | "222220-222224,1698522-1698528,446443-446449," 9 | "38593856-38593862,565653-565659,824824821-824824827," 10 | "2121212118-2121212124"] 11 | 12 | def test_part_1(self): 13 | self.assertEqual(1227775554, Dec02(instructions=self.data).part_1()) 14 | 15 | def test_part_1_mini(self): 16 | gs = GiftShopDatabase([(2, 18)]) 17 | self.assertEqual(11, gs.identify_repeated_twice()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(4174379265, Dec02(instructions=self.data).part_2()) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec01.py: -------------------------------------------------------------------------------- 1 | from python.src.y2018 import dec01 2 | 3 | import unittest 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | 8 | def test_frequency_change(self): 9 | cases = [ 10 | (['+1', '-2', '+3', '+1'], 3), 11 | (['+1', '+1', '+1'], 3), 12 | (['-1', '-2', '-3'], -6) 13 | ] 14 | 15 | for case in cases: 16 | self.assertEqual(case[1], dec01.Dec01(case[0]).part_1()) 17 | 18 | def test_first_repeating_frequency(self): 19 | cases = [ 20 | (['-1', '+1'], 0), 21 | (['+3', '+3', '+4', '-2', '-4'], 10), 22 | (['-6', '+3', '+8', '+5', '-6'], 5), 23 | (['+7', '+7', '-2', '-7', '-4'], 14) 24 | ] 25 | 26 | for case in cases: 27 | self.assertEqual(case[1], dec01.Dec01(case[0]).part_2()) 28 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec02.py: -------------------------------------------------------------------------------- 1 | from python.src.y2018 import dec02 2 | 3 | import unittest 4 | 5 | 6 | class TestDec02(unittest.TestCase): 7 | def test_box_checksum(self): 8 | boxes = [ 9 | "abcdef", 10 | "bababc", 11 | "abbcde", 12 | "abcccd", 13 | "aabcdd", 14 | "abcdee", 15 | "ababab" 16 | ] 17 | day2 = dec02.Dec02(instructions=boxes) 18 | self.assertEqual(12, day2.part_1()) 19 | 20 | def test_common_letters(self): 21 | boxes = [ 22 | "abcde", 23 | "fghij", 24 | "klmno", 25 | "pqrst", 26 | "fguij", 27 | "axcye", 28 | "wvxyz" 29 | ] 30 | day2 = dec02.Dec02(instructions=boxes) 31 | self.assertEqual('fgij', day2.part_2()) 32 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec01.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec01 import Dec01 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | 8 | def test_part_1_calibration_values(self): 9 | data = [ 10 | "1abc2", 11 | "pqr3stu8vwx", 12 | "a1b2c3d4e5f", 13 | "treb7uchet" 14 | ] 15 | self.assertEqual(142, Dec01(instructions=data).part_1()) 16 | 17 | def test_part_2(self): 18 | data = [ 19 | "two1nine", 20 | "eightwothree", 21 | "abcone2threexyz", 22 | "xtwone3four", 23 | "4nineeightseven2", 24 | "zoneight234", 25 | "7pqrstsixteen" 26 | ] 27 | self.assertEqual(281, Dec01(instructions=data).part_2()) 28 | 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec11.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec11 import Dec11, StarMap 4 | 5 | 6 | class TestDec11(unittest.TestCase): 7 | data = [ 8 | "...#......", 9 | ".......#..", 10 | "#.........", 11 | "..........", 12 | "......#...", 13 | ".#........", 14 | ".........#", 15 | "..........", 16 | ".......#..", 17 | "#...#....." 18 | ] 19 | 20 | def test_part_1(self): 21 | 22 | self.assertEqual(374, Dec11(instructions=self.data).part_1()) 23 | 24 | def test_expansion(self): 25 | for (factor, expected) in ((10, 1030), (100, 8410)): 26 | sm = StarMap(self.data, expansion=factor) 27 | self.assertEqual(expected, sm.sum_shortest_pairs()) 28 | 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec14.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec14 import Dec14 4 | 5 | 6 | class TestDec14(unittest.TestCase): 7 | data = ["NNCB", 8 | "", 9 | "CH -> B", 10 | "HH -> N", 11 | "CB -> H", 12 | "NH -> C", 13 | "HB -> C", 14 | "HC -> B", 15 | "HN -> C", 16 | "NN -> C", 17 | "BH -> H", 18 | "NC -> B", 19 | "NB -> B", 20 | "BN -> B", 21 | "BB -> N", 22 | "BC -> B", 23 | "CC -> N", 24 | "CN -> C"] 25 | 26 | def test_10_steps(self): 27 | self.assertEqual(1588, Dec14(instructions=self.data).part_1()) 28 | 29 | def test_40_steps(self): 30 | self.assertEqual(2188189693529, Dec14(instructions=self.data).part_2()) 31 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec21.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec21 import Farm 4 | 5 | 6 | class TestDec21(unittest.TestCase): 7 | data = [ 8 | "...........", 9 | ".....###.#.", 10 | ".###.##..#.", 11 | "..#.#...#..", 12 | "....#.#....", 13 | ".##..S####.", 14 | ".##..#...#.", 15 | ".......##..", 16 | ".##.#.####.", 17 | ".##..##.##.", 18 | "..........." 19 | ] 20 | 21 | def test_part_1(self): 22 | f = Farm(self.data) 23 | for (case, expected) in ( 24 | (3, 6), (6, 16), (10, 50), (50, 1594), 25 | (100, 6536), (500, 167004) # , (1000, 668697) , (5000, 16733044) 26 | ): 27 | self.assertEqual(expected, f.move(max_steps=case)) 28 | 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec18.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec18 import Dec18 4 | 5 | 6 | class TestDec18(unittest.TestCase): 7 | data = [ 8 | "R 6 (#70c710)", 9 | "D 5 (#0dc571)", 10 | "L 2 (#5713f0)", 11 | "D 2 (#d2c081)", 12 | "R 2 (#59c680)", 13 | "D 2 (#411b91)", 14 | "L 5 (#8ceee2)", 15 | "U 2 (#caa173)", 16 | "L 1 (#1b58a2)", 17 | "U 2 (#caa171)", 18 | "R 2 (#7807d2)", 19 | "U 3 (#a77fa3)", 20 | "L 2 (#015232)", 21 | "U 2 (#7a21e3)" 22 | ] 23 | 24 | def test_part_1(self): 25 | self.assertEqual(62, Dec18(instructions=self.data).part_1()) 26 | 27 | def test_part_2(self): 28 | self.assertEqual(952408144115, Dec18(instructions=self.data).part_2()) 29 | 30 | 31 | if __name__ == '__main__': 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /python/test/y2019/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2019.intcode import IntCode 4 | 5 | 6 | class TestDec09(unittest.TestCase): 7 | def test_quine(self): 8 | data = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99] 9 | ic = IntCode(instructions=data) 10 | ic.run() 11 | self.assertEqual(data, ic.output) 12 | 13 | def atest_16bit(self): 14 | data = [1102, 34915192, 34915192, 7, 4, 7, 99, 0] 15 | ic = IntCode(instructions=data) 16 | ic.run() 17 | self.assertGreaterEqual(ic.get_output(), 2 ** 16) 18 | 19 | def atest_large_number(self): 20 | data = [104, 1125899906842624, 99] 21 | ic = IntCode(instructions=data) 22 | ic.run() 23 | self.assertEqual(data[1], ic.get_output()) 24 | 25 | 26 | if __name__ == '__main__': 27 | unittest.main() 28 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec03.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec03 3 | 4 | 5 | class TestDec03(unittest.TestCase): 6 | def test_spiral_steps_without_filling(self): 7 | sm = dec03.SpiralMemoryAlgoritm() 8 | self.assertEqual(0, sm.spiral_steps(1)) 9 | self.assertEqual(3, sm.spiral_steps(12)) 10 | self.assertEqual(2, sm.spiral_steps(23)) 11 | self.assertEqual(31, sm.spiral_steps(1024)) 12 | 13 | def test_spiral_steps(self): 14 | sm = dec03.SpiralMemory() 15 | self.assertEqual(3, sm.spiral_steps(12)) 16 | self.assertEqual(2, sm.spiral_steps(23)) 17 | self.assertEqual(31, sm.spiral_steps(1024)) 18 | 19 | def test_stress(self): 20 | sm = dec03.SpiralMemory() 21 | sm.stress_test(23) 22 | self.assertEqual(4, sm.cell(0, 1)) 23 | self.assertEqual(147, sm.cell(-2, 2)) 24 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec03.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec03 import Dec03 4 | 5 | 6 | class TestDec03(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | data = ["xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)" 10 | "+mul(32,64]then(mul(11,8)mul(8,5))"] 11 | self.assertEqual(161, Dec03(instructions=data).part_1()) 12 | 13 | def test_part_2(self): 14 | data = ["xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"] 15 | self.assertEqual(48, Dec03(instructions=data).part_2()) 16 | 17 | def test_part_2_no_ending_do(self): 18 | data = ["xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" 19 | "don't()mul(10,10)"] 20 | self.assertEqual(48, Dec03(instructions=data).part_2()) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec02.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec02 import Dec02 4 | 5 | 6 | class TestDec02(unittest.TestCase): 7 | data = [ 8 | "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", 9 | "Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue", 10 | "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red", 11 | "Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red", 12 | "Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green" 13 | ] 14 | 15 | def test_part_1(self): 16 | self.assertEqual(8, 17 | Dec02(instructions=self.data).part_1()) 18 | 19 | def test_part_2(self): 20 | self.assertEqual(2286, 21 | Dec02(instructions=self.data).part_2()) 22 | 23 | 24 | if __name__ == '__main__': 25 | unittest.main() 26 | -------------------------------------------------------------------------------- /python/src/helpers/SimpleGrid.py: -------------------------------------------------------------------------------- 1 | class SimpleGrid(object): 2 | 3 | def __init__(self, grid_map, 4 | walls='#', start='S', end='E'): 5 | self.max_y = len(grid_map) 6 | self.max_x = len(grid_map[0]) 7 | 8 | self.walls = set() 9 | self.start = self.end = (0, 0) 10 | for y, row in enumerate(grid_map): 11 | for x, cell in enumerate(row): 12 | if cell == walls: 13 | self.walls.add((x, y)) 14 | elif cell == start: 15 | self.start = (x, y) 16 | elif cell == end: 17 | self.end = (x, y) 18 | self.delta = ((-1, 0), (1, 0), (0, -1), (0, 1)) 19 | 20 | def nbs(self, pos): 21 | for d in self.delta: 22 | if 0 <= (px := pos[0] + d[0]) < self.max_x and 0 <= (py := pos[1] + d[1]) < self.max_y: 23 | yield px, py 24 | -------------------------------------------------------------------------------- /python/src/y2024/dec03.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | from python.src.common import Day, timer, Timer 4 | 5 | 6 | class Dec03(Day, year=2024, day=3, title='Mull it over'): 7 | mul_pattern = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)") 8 | 9 | @staticmethod 10 | def parse_instructions(instructions): 11 | return ''.join(instructions) 12 | 13 | @timer(part=1) 14 | def part_1(self): 15 | return sum(int(m[0]) * int(m[1]) for m in self.mul_pattern.findall(self.instructions)) 16 | 17 | @timer(part=2) 18 | def part_2(self): 19 | removed_dont_do = re.sub(r"don't\(\).*?do\(\)", 20 | '', 21 | self.instructions + 'do()') 22 | return sum(int(m[0]) * int(m[1]) for m in self.mul_pattern.findall(removed_dont_do)) 23 | 24 | 25 | if __name__ == '__main__': 26 | with Timer('Total'): 27 | Dec03().run_day() 28 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec22.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec22 import Dec22 4 | from src.y2024.dec22 import Secret 5 | 6 | 7 | class TestDec22(unittest.TestCase): 8 | data = ["1", "10", "100", "2024"] 9 | 10 | def test_generate_secrets(self): 11 | seed = 123 12 | secrets = [15887950, 16495136, 527345, 704524, 1553684, 13 | 12683156, 11100544, 12249484, 7753432, 5908254] 14 | secret_generator = Secret(seed) 15 | for secret in secrets: 16 | self.assertEqual(secret, secret_generator.next_secret()) 17 | 18 | def test_part_1(self): 19 | self.assertEqual(37327623, Dec22(instructions=self.data).part_1()) 20 | 21 | def test_part_2(self): 22 | data = ["1", "2", "3", "2024"] 23 | self.assertEqual(23, Dec22(instructions=data).part_2()) 24 | 25 | 26 | if __name__ == '__main__': 27 | unittest.main() 28 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec03.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import unittest 3 | 4 | from python.src.y2015 import dec03 5 | 6 | __author__ = 'anna' 7 | 8 | 9 | class Dec03Tests(unittest.TestCase): 10 | def setUp(self): 11 | pass 12 | 13 | def testHousesExample1(self): 14 | self.assertEqual(2, dec03.houses('>')) 15 | 16 | def testHousesExample2(self): 17 | self.assertEqual(4, dec03.houses('^>v<')) 18 | 19 | def testHousesExample3(self): 20 | self.assertEqual(2, dec03.houses('^v^v^v^v^v')) 21 | 22 | def testRoboSantaExample1(self): 23 | self.assertEqual(3, dec03.houses('^v', 2)) 24 | 25 | def testRoboSantaExample2(self): 26 | self.assertEqual(3, dec03.houses('^>v<', 2)) 27 | 28 | def testRoboSantaExample3(self): 29 | self.assertEqual(11, dec03.houses('^v^v^v^v^v', 2)) 30 | 31 | 32 | if __name__ == '__main__': 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /python/src/y2019/dec09.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer 2 | from python.src.y2019.intcode import IntCode 3 | 4 | 5 | class Dec09(Day): 6 | 7 | def __init__(self, instructions=None, filename=None): 8 | super().__init__(2019, 9, instructions, filename) 9 | 10 | @staticmethod 11 | def parse_instructions(instructions): 12 | return Day.parse_int_line(instructions) 13 | 14 | @timer(part=1, title='BOOST keycode') 15 | def part_1(self): 16 | ic = IntCode(instructions=self.instructions) 17 | ic.add_input(1) 18 | ic.run() 19 | return ic.get_output() 20 | 21 | @timer(part=2, title='Coordinates of distress signal') 22 | def part_2(self): 23 | ic = IntCode(instructions=self.instructions) 24 | ic.add_input(2) 25 | ic.run() 26 | return ic.get_output() 27 | 28 | 29 | if __name__ == '__main__': 30 | Dec09().run_day() 31 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec09 import Dec09 4 | 5 | 6 | class TestDec09(unittest.TestCase): 7 | instructions = [ 8 | "35", 9 | "20", 10 | "15", 11 | "25", 12 | "47", 13 | "40", 14 | "62", 15 | "55", 16 | "65", 17 | "95", 18 | "102", 19 | "117", 20 | "150", 21 | "182", 22 | "127", 23 | "219", 24 | "299", 25 | "277", 26 | "309", 27 | "576" 28 | ] 29 | 30 | def test_find_invalid_number(self): 31 | self.assertEqual(127, 32 | Dec09(instructions=self.instructions).find_invalid_number(preamble=5)) 33 | 34 | def test_find_weakness(self): 35 | self.assertEqual((15, 47), 36 | Dec09(instructions=self.instructions).find_encryption_weakness(127)) 37 | -------------------------------------------------------------------------------- /python/src/y2022/dec06.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec06(Day, year=2022, day=6): 5 | 6 | @staticmethod 7 | def parse_instructions(instructions): 8 | return instructions[0] 9 | 10 | def run(self, window_size): 11 | window = self.instructions[:window_size] 12 | char_count = window_size 13 | for ch in self.instructions[window_size:]: 14 | char_count += 1 15 | window = window[1:] + ch 16 | if len(set(window)) == window_size: 17 | return char_count 18 | 19 | @timer(part=1, title='Start of packet') 20 | def part_1(self): 21 | return self.run(window_size=4) 22 | 23 | @timer(part=2, title='Start of message') 24 | def part_2(self): 25 | return self.run(window_size=14) 26 | 27 | 28 | if __name__ == '__main__': 29 | with Timer('Total'): 30 | Dec06().run_day() 31 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec05.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec05 import Dec05 4 | 5 | 6 | class TestDec05(unittest.TestCase): 7 | data = """47|53 8 | 97|13 9 | 97|61 10 | 97|47 11 | 75|29 12 | 61|13 13 | 75|53 14 | 29|13 15 | 97|29 16 | 53|29 17 | 61|53 18 | 97|53 19 | 61|29 20 | 47|13 21 | 75|47 22 | 97|75 23 | 47|61 24 | 75|61 25 | 47|29 26 | 75|13 27 | 53|13 28 | 29 | 75,47,61,53,29 30 | 97,61,53,29,13 31 | 75,29,13 32 | 75,97,47,61,53 33 | 61,13,29 34 | 97,13,75,29,47""".split("\n") 35 | 36 | def test_part_1(self): 37 | """ 38 | Valid: 39 | 75,47,61,53,29 40 | 97,61,53,29,13 41 | 75,29,13 42 | """ 43 | self.assertEqual(143, Dec05(instructions=self.data).part_1()) 44 | 45 | def test_part_2(self): 46 | self.assertEqual(123, Dec05(instructions=self.data).part_2()) 47 | 48 | 49 | if __name__ == '__main__': 50 | unittest.main() 51 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec10.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec10 import Dec10 4 | 5 | 6 | class TestDec10(unittest.TestCase): 7 | data = [ 8 | "89010123", 9 | "78121874", 10 | "87430965", 11 | "96549874", 12 | "45678903", 13 | "32019012", 14 | "01329801", 15 | "10456732" 16 | ] 17 | 18 | def test_part_1(self): 19 | cases = ( 20 | (["0123", "1234", "8765", "9876"], 1), 21 | (["9990999", "9991998", "9992997", "6543456", "7651987", "8761111", "29871111"], 3), 22 | (self.data, 36) 23 | ) 24 | for example, expected in cases: 25 | self.assertEqual(expected, Dec10(instructions=example).part_1()) 26 | 27 | def test_part_2(self): 28 | self.assertEqual(81, Dec10(instructions=self.data).part_2()) 29 | 30 | 31 | if __name__ == '__main__': 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec01.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec01 import Dec01 4 | from src.y2025.dec01 import Dial0x434C49434B 5 | 6 | 7 | class TestDec01(unittest.TestCase): 8 | data = """L68 9 | L30 10 | R48 11 | L5 12 | R60 13 | L55 14 | L1 15 | L99 16 | R14 17 | L82""".splitlines() 18 | 19 | def test_part_1(self): 20 | self.assertEqual(3, Dec01(instructions=self.data).part_1()) 21 | 22 | def test_part_2(self): 23 | self.assertEqual(6, Dec01(instructions=self.data).part_2()) 24 | 25 | def test_part_2_mini(self): 26 | d = Dial0x434C49434B() 27 | d.rotate(1050, 'L') 28 | self.assertEqual(11, d.password) 29 | 30 | def test_part_2_2(self): 31 | d = Dial0x434C49434B() 32 | d.rotate(50, 'L') 33 | d.rotate(101, 'R') 34 | self.assertEqual(2, d.password) 35 | 36 | 37 | if __name__ == '__main__': 38 | unittest.main() 39 | -------------------------------------------------------------------------------- /python/test/y2025/test_dec08.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2025.dec08 import Dec08 4 | from src.y2025.dec08 import Decoration 5 | 6 | 7 | class TestDec08(unittest.TestCase): 8 | data = """162,817,812 9 | 57,618,57 10 | 906,360,560 11 | 592,479,940 12 | 352,342,300 13 | 466,668,158 14 | 542,29,236 15 | 431,825,988 16 | 739,650,466 17 | 52,470,668 18 | 216,146,977 19 | 819,987,18 20 | 117,168,530 21 | 805,96,715 22 | 346,949,466 23 | 970,615,88 24 | 941,993,340 25 | 862,61,35 26 | 984,92,344 27 | 425,690,689""".splitlines() 28 | 29 | def test_part_1(self): 30 | boxes = Dec08.parse_instructions(instructions=self.data) 31 | d = Decoration(boxes) 32 | 33 | self.assertEqual(40, d.largest_circuits(connections=10)) 34 | 35 | def test_part_2(self): 36 | self.assertEqual(25272, Dec08(instructions=self.data).part_2()) 37 | 38 | 39 | if __name__ == '__main__': 40 | unittest.main() 41 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec11.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anna' 2 | 3 | import unittest 4 | 5 | from python.src.y2015 import dec11 6 | 7 | 8 | class Dec11Tests(unittest.TestCase): 9 | def test_password_validExample1(self): 10 | self.assertFalse(dec11.password_valid('hijklmmn')) 11 | 12 | def test_password_valid_example2(self): 13 | self.assertFalse(dec11.password_valid('abbceffg')) 14 | 15 | def test_password_valid_example3(self): 16 | self.assertFalse(dec11.password_valid('abbcegjk')) 17 | 18 | def test_next_password_example1(self): 19 | self.assertEqual('abcdffaa', dec11.next_password('abcdefgh')) 20 | 21 | def test_next_password_example2(self): 22 | self.assertEqual('ghjaabcc', dec11.next_password('ghijklmn')) 23 | 24 | def test_filter_bad(self): 25 | self.assertEqual('abcjaaa', dec11.filter_bad('abcijkl')) 26 | 27 | 28 | if __name__ == '__main__': 29 | unittest.main() 30 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec24.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec24 5 | 6 | 7 | class Dec24Tests(unittest.TestCase): 8 | def setUp(self): 9 | self.map = ["###########", 10 | "#0.1.....2#", 11 | "#.#######.#", 12 | "#4.......3#", 13 | "###########"] 14 | 15 | self.grid = dec24.Grid(self.map) 16 | 17 | def test_find_numbers(self): 18 | self.assertDictEqual({'0': (1, 1), 19 | '1': (1, 3), 20 | '2': (1, 9), 21 | '3': (3, 9), 22 | '4': (3, 1)}, 23 | self.grid.find_numbers()) 24 | 25 | def test_visit_numbers(self): 26 | self.assertEqual(14, self.grid.shortest_path()) 27 | 28 | 29 | if __name__ == '__main__': 30 | unittest.main() 31 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec13.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec13 import Dec13 4 | 5 | 6 | class TestDec13(unittest.TestCase): 7 | 8 | def a_test_part_1(self): 9 | instructions = ["/->-\\ ", 10 | "| | /----\\", 11 | "| /-+--+-\\ |", 12 | "| | | | v |", 13 | "\\-+-/ \\-+--/", 14 | "\\------/ "] 15 | 16 | d = Dec13(instructions) 17 | self.assertEqual((7, 3), d.part_1()) 18 | 19 | def test_part_2(self): 20 | instructions = ["/>-<\\", 21 | "| |", 22 | "| /<+-\\", 23 | "| | | v", 24 | "\\>+/"] 27 | d = Dec13(instructions) 28 | self.assertEqual((6, 4), d.part_2()) 29 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec23.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec23 import Dec23 4 | 5 | 6 | class TestDec23(unittest.TestCase): 7 | data = [ 8 | "#############", 9 | "#...........#", 10 | "###B#C#B#D###", 11 | " #A#D#C#A# ", 12 | " ######### " 13 | ] 14 | 15 | def test_part_1(self): 16 | self.assertEqual(12521, Dec23(instructions=self.data).part_1()) 17 | 18 | def test_part_2(self): 19 | self.assertEqual(44169, Dec23(instructions=self.data).part_2()) 20 | 21 | def test_4_deep(self): 22 | data = [ 23 | "#############", 24 | "#...........#", 25 | "###B#C#D#A###", 26 | " #A#B#C#D# ", 27 | " #A#B#C#D# ", 28 | " #A#B#C#D# ", 29 | " ######### " 30 | ] 31 | self.assertEqual(4450, 32 | Dec23(instructions=data).part_1()) 33 | -------------------------------------------------------------------------------- /python/src/y2015/dec02.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anna' 2 | 3 | 4 | def wrap(length, width, height): 5 | lw = length * width 6 | lh = length * height 7 | wh = width * height 8 | slack = min(lw, lh, wh) 9 | return 2 * lw + 2 * lh + 2 * wh + slack 10 | 11 | 12 | def ribbon(length, width, height): 13 | lw = 2 * length + 2 * width 14 | lh = 2 * length + 2 * height 15 | wh = 2 * width + 2 * height 16 | bow = length * width * height 17 | return bow + min(lw, lh, wh) 18 | 19 | 20 | if __name__ == '__main__': 21 | paper = 0 22 | ribbon_length = 0 23 | with open('../../../data/2015/input.2.txt', 'r') as fh: 24 | for line in fh.readlines(): 25 | l, w, h = map(int, line.split('x')) 26 | paper += wrap(l, w, h) 27 | ribbon_length += ribbon(l, w, h) 28 | 29 | print("Total amount of paper to order:", paper) 30 | print("Total length of ribbon to order: ", ribbon_length) 31 | -------------------------------------------------------------------------------- /python/src/y2021/dec01.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec01(Day, year=2021, day=1): 5 | 6 | @staticmethod 7 | def parse_instructions(instructions): 8 | return Day.parse_int_lines(instructions) 9 | 10 | def get_depth(self, window_size=1): 11 | decreases = 0 12 | prev_depth = None 13 | for i in range(0, len(self.instructions) - (window_size - 1)): 14 | depth = sum(self.instructions[i:i + window_size]) 15 | if prev_depth and depth > prev_depth: 16 | decreases += 1 17 | prev_depth = depth 18 | return decreases 19 | 20 | @timer(part=1) 21 | def part_1(self): 22 | return self.get_depth() 23 | 24 | @timer(part=2) 25 | def part_2(self): 26 | return self.get_depth(window_size=3) 27 | 28 | 29 | if __name__ == '__main__': 30 | with Timer('Sonar Sweep'): 31 | Dec01().run_day() 32 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec16.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec16 import Dec16, Device 4 | 5 | 6 | class TestDec16(unittest.TestCase): 7 | 8 | def test_parse(self): 9 | instructions = [ 10 | "Before: [3, 2, 1, 1]", 11 | "9 2 1 2", 12 | "After: [3, 2, 2, 1]", 13 | "1 1 1 1", 14 | ] 15 | 16 | d = Dec16(instructions=instructions) 17 | self.assertEqual([( 18 | [3, 2, 1, 1], 19 | [9, 2, 1, 2], 20 | [3, 2, 2, 1] 21 | )], d.samples) 22 | 23 | def test_sample(self): 24 | sample = ( 25 | [3, 2, 1, 1], 26 | [9, 2, 1, 2], 27 | [3, 2, 2, 1] 28 | ) 29 | d = Device() 30 | matches = d.sample(sample[0], sample[1], sample[2]) 31 | self.assertEqual(3, len(matches)) 32 | self.assertListEqual(['addi', 'mulr', 'seti'], sorted(matches)) 33 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec07.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec07 import Dec07 4 | 5 | 6 | class TestDec07(unittest.TestCase): 7 | data = [ 8 | "$ cd /", 9 | "$ ls", 10 | "dir a", 11 | "14848514 b.txt", 12 | "8504156 c.dat", 13 | "dir d", 14 | "$ cd a", 15 | "$ ls", 16 | "dir e", 17 | "29116 f", 18 | "2557 g", 19 | "62596 h.lst", 20 | "$ cd e", 21 | "$ ls", 22 | "584 i", 23 | "$ cd ..", 24 | "$ cd ..", 25 | "$ cd d", 26 | "$ ls", 27 | "4060174 j", 28 | "8033020 d.log", 29 | "5626152 d.ext", 30 | "7214296 k" 31 | ] 32 | 33 | def test_part_1(self): 34 | self.assertEqual(95437, Dec07(instructions=self.data).part_1()) 35 | 36 | def test_part_2(self): 37 | self.assertEqual(24933642, Dec07(instructions=self.data).part_2()) 38 | -------------------------------------------------------------------------------- /python/src/gol.py: -------------------------------------------------------------------------------- 1 | from python.src.grid import Grid 2 | 3 | 4 | class GameOfLife(Grid): 5 | 6 | def __init__(self, stay_alive, new_life, dimensions=2, stay_in_bounds=True, 7 | state=None): 8 | super().__init__(dimensions=dimensions, data_type=bool, 9 | stay_in_bounds=stay_in_bounds, state=state) 10 | self.stay_alive = set(stay_alive) 11 | self.new_life = set(new_life) 12 | self.delta.remove((0,) * self.dimensions) 13 | 14 | def count_neighbours(self, pos): 15 | return sum(self.grid[p] for p in self.neighbours(pos)) 16 | 17 | def update_pos(self, pos): 18 | alive = self.grid[pos] 19 | neighbours = self.count_neighbours(pos) 20 | if alive and neighbours not in self.stay_alive: 21 | return False 22 | elif not alive and neighbours in self.new_life: 23 | return True 24 | else: 25 | return alive 26 | -------------------------------------------------------------------------------- /python/src/y2015/dec08.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anna' 2 | 3 | 4 | def char_decode_diff(text): 5 | mem_length = len(text) 6 | char_length = len(eval(text)) 7 | return mem_length - char_length 8 | 9 | 10 | def char_encode_diff(text): 11 | added_backslash = text.count('\\') 12 | added_quote = text.count('"') 13 | added = added_backslash + added_quote + 2 14 | return added 15 | 16 | 17 | if __name__ == '__main__': 18 | with open('../../../data/2015/input.8.txt', 'r') as fh: 19 | total_decode_diff = 0 20 | total_encode_diff = 0 21 | for line in fh.readlines(): 22 | text = line.strip() 23 | 24 | total_decode_diff += char_decode_diff(text) 25 | total_encode_diff += char_encode_diff(text) 26 | 27 | print("Diff is {diff} (decode) & {diff2} (encode)".format(diff=total_decode_diff, 28 | diff2=total_encode_diff)) 29 | -------------------------------------------------------------------------------- /python/src/y2017/dec02.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | 4 | from python.src.y2017.common import DATA_DIR 5 | 6 | 7 | def minmax_checksum(lines): 8 | checksum = 0 9 | for line in lines: 10 | checksum += max(line) - min(line) 11 | return checksum 12 | 13 | 14 | def evenly_divisible_checksum(lines): 15 | checksum = 0 16 | for line in lines: 17 | for n, a in enumerate(line): 18 | for b in line[n + 1:]: 19 | ma = max(a, b) 20 | mb = min(a, b) 21 | checksum += ma // mb if ma % mb == 0 else 0 22 | return checksum 23 | 24 | 25 | def main(): 26 | with open(os.path.join(DATA_DIR, 'input.2.txt')) as fh: 27 | lines = [list(map(int, line.split())) for line in fh.readlines()] 28 | print("Part 1:", minmax_checksum(lines)) 29 | print("Part 2:", evenly_divisible_checksum(lines)) 30 | 31 | 32 | if __name__ == '__main__': 33 | main() 34 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec13.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec13 import Dec13 4 | 5 | 6 | class TestDec13(unittest.TestCase): 7 | instructions = [ 8 | "939", 9 | "7,13,x,x,59,x,31,19" 10 | ] 11 | 12 | def test_part_1(self): 13 | self.assertEqual(295, Dec13(instructions=self.instructions).part_1()) 14 | 15 | def a_test_part_2(self): 16 | self.assertEqual(1068781, Dec13(instructions=self.instructions).part_2()) 17 | 18 | def test_part_2_more_examples(self): 19 | cases = [ 20 | (3417, "17,x,13,19"), 21 | (754018, "67,7,59,61"), 22 | (779210, "67,x,7,59,61"), 23 | (1261476, "67,7,x,59,61"), 24 | (1202161486, "1789,37,47,1889") 25 | ] 26 | for expected_response, constraints in cases: 27 | self.assertEqual(expected_response, 28 | Dec13(instructions=["0", constraints]).part_2()) 29 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec17.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec17 import Dec17 4 | 5 | 6 | class TestDec17(unittest.TestCase): 7 | data = """Register A: 729 8 | Register B: 0 9 | Register C: 0 10 | 11 | Program: 0,1,5,4,3,0""".split('\n') 12 | 13 | def test_part_1(self): 14 | self.assertEqual('4,6,3,5,6,3,5,2,1,0', Dec17(instructions=self.data).part_1()) 15 | 16 | def test_part_2(self): 17 | data = """Register A: 2024 18 | Register B: 0 19 | Register C: 0 20 | 21 | Program: 0,3,5,4,3,0""".split('\n') 22 | self.assertEqual(117440, Dec17(instructions=data).part_2()) 23 | 24 | def test_part_2_challenge(self): 25 | data = """Register A: 12345678 26 | Register B: 0 27 | Register C: 0 28 | 29 | Program: 2,4,1,0,7,5,1,5,0,3,4,5,5,5,3,0 30 | """.split('\n') 31 | self.assertEqual(202797954918051, Dec17(instructions=data).part_2()) 32 | 33 | 34 | if __name__ == '__main__': 35 | unittest.main() 36 | -------------------------------------------------------------------------------- /python/src/y2020/dec25.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | def find_loop(public_key=None): 5 | value = 1 6 | loop = 0 7 | while value != public_key: 8 | loop += 1 9 | value = (value * 7) % 20201227 10 | return loop 11 | 12 | 13 | class Dec25(Day, year=2020, day=25): 14 | def __init__(self, instructions=None, filename=None): 15 | super().__init__(instructions=instructions, filename=filename) 16 | self.door, self.card = self.instructions 17 | 18 | @staticmethod 19 | def parse_instructions(instructions): 20 | return Day.parse_int_lines(instructions) 21 | 22 | @timer(part=1) 23 | def part_1(self): 24 | card_loop = find_loop(public_key=self.card) 25 | return pow(self.door, card_loop, 20201227) 26 | 27 | def part_2(self): 28 | return 0 29 | 30 | 31 | if __name__ == '__main__': 32 | with Timer('Combo breaker'): 33 | Dec25().run_day() 34 | -------------------------------------------------------------------------------- /python/src/y2024/dec01.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | from python.src.common import Day, timer, Timer 4 | 5 | 6 | class Dec01(Day, year=2024, day=1, title='Historian Hysteria'): 7 | 8 | @classmethod 9 | def parse_instructions(cls, instructions): 10 | lines = cls.parse_multiple_ints_per_line(instructions, separator=r'\s+') 11 | return [line[0] for line in lines], [line[1] for line in lines] 12 | 13 | @timer(part=1) 14 | def part_1(self): 15 | sorted_left = sorted(self.instructions[0]) 16 | sorted_right = sorted(self.instructions[1]) 17 | return sum(abs(x - y) for x, y in zip(sorted_left, sorted_right)) 18 | 19 | @timer(part=2) 20 | def part_2(self): 21 | left = self.instructions[0] 22 | right = Counter(self.instructions[1]) 23 | return sum(item * right[item] for item in left) 24 | 25 | 26 | if __name__ == '__main__': 27 | with Timer('Total'): 28 | Dec01().run_day() 29 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec17.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec17 5 | 6 | 7 | class Dec17Tests(unittest.TestCase): 8 | 9 | def test_shortest_path_example1(self): 10 | self.assertEqual('DDRRRD', dec17.shortest_path('ihgpwlah')) 11 | 12 | def test_shortest_path_example2(self): 13 | self.assertEqual('DDUDRLRRUDRD', dec17.shortest_path('kglvqrro')) 14 | 15 | def test_shortest_path_example3(self): 16 | self.assertEqual('DRURDRUDDLLDLUURRDULRLDUUDDDRR', dec17.shortest_path('ulqzkmiv')) 17 | 18 | def test_longest_path_example1(self): 19 | self.assertEqual(370, dec17.longest_path('ihgpwlah')) 20 | 21 | def test_longest_path_example2(self): 22 | self.assertEqual(492, dec17.longest_path('kglvqrro')) 23 | 24 | def test_longest_path_example3(self): 25 | self.assertEqual(830, dec17.longest_path('ulqzkmiv')) 26 | 27 | 28 | if __name__ == '__main__': 29 | unittest.main() 30 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec11.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec11 import Dec11 4 | 5 | 6 | class TestDec11(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | cases = [ 10 | ((33, 45), "18"), 11 | ((21, 61), "42") 12 | ] 13 | for expected, serial in cases: 14 | self.assertEqual(expected, Dec11([serial]).part_1()) 15 | 16 | def test_power_level(self): 17 | cases = [ 18 | (-5, (122, 79), "57"), 19 | (0, (217, 196), "39"), 20 | (4, (101, 153), "71") 21 | ] 22 | 23 | for expected, (x, y), serial in cases: 24 | self.assertEqual(expected, Dec11([serial]).power_level(x, y)) 25 | 26 | def test_part_2(self): 27 | cases = [ 28 | ((90, 269, 16), "18"), 29 | ((232, 251, 12), "42") 30 | ] 31 | for expected, serial in cases: 32 | self.assertEqual(expected, Dec11([serial]).part_2) 33 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec15.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec15 import Dec15 4 | 5 | 6 | class TestDec15(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | cases = [ 10 | (436, "0,3,6"), 11 | (1, "1,3,2"), 12 | (10, "2,1,3"), 13 | (27, "1,2,3"), 14 | (78, "2,3,1"), 15 | (438, "3,2,1"), 16 | (1836, "3,1,2") 17 | ] 18 | for expected, data in cases: 19 | self.assertEqual(expected, Dec15(instructions=[data]).part_1()) 20 | 21 | def test_part_2(self): 22 | cases = [ 23 | (175594, "0,3,6"), 24 | (2578, "1,3,2"), 25 | (3544142, "2,1,3"), 26 | (261214, "1,2,3"), 27 | (6895259, "2,3,1"), 28 | (18, "3,2,1"), 29 | (362, "3,1,2") 30 | ] 31 | for expected, data in cases: 32 | self.assertEqual(expected, Dec15(instructions=[data]).part_2()) 33 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec16.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec16 import Dec16 4 | 5 | 6 | class TestDec16(unittest.TestCase): 7 | data = """Valve AA has flow rate=0; tunnels lead to valves DD, II, BB 8 | Valve BB has flow rate=13; tunnels lead to valves CC, AA 9 | Valve CC has flow rate=2; tunnels lead to valves DD, BB 10 | Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE 11 | Valve EE has flow rate=3; tunnels lead to valves FF, DD 12 | Valve FF has flow rate=0; tunnels lead to valves EE, GG 13 | Valve GG has flow rate=0; tunnels lead to valves FF, HH 14 | Valve HH has flow rate=22; tunnel leads to valve GG 15 | Valve II has flow rate=0; tunnels lead to valves AA, JJ 16 | Valve JJ has flow rate=21; tunnel leads to valve II""".split('\n') 17 | 18 | def test_part_1(self): 19 | self.assertEqual(1651, Dec16(instructions=self.data).part_1()) 20 | 21 | def test_part_2(self): 22 | self.assertEqual(1707, Dec16(instructions=self.data).part_2()) 23 | -------------------------------------------------------------------------------- /python/src/y2019/dec01.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer 2 | 3 | 4 | class Dec01(Day): 5 | 6 | def __init__(self, instructions=None, filename=None): 7 | super().__init__(2019, 1, instructions, filename) 8 | 9 | @staticmethod 10 | def parse_instructions(instructions): 11 | return Day.parse_int_lines(instructions) 12 | 13 | @timer(part=1, title='Fuel requirements') 14 | def part_1(self): 15 | return sum([self.fuel_cost(x) for x in self.instructions]) 16 | 17 | @staticmethod 18 | def fuel_cost(mass): 19 | return max((mass // 3) - 2, 0) 20 | 21 | @timer(part=2, title='Total requirements') 22 | def part_2(self): 23 | total_weight = 0 24 | for module in self.instructions: 25 | fc = module 26 | while fc > 0: 27 | fc = self.fuel_cost(fc) 28 | total_weight += fc 29 | return total_weight 30 | 31 | 32 | if __name__ == '__main__': 33 | Dec01().run_day() 34 | -------------------------------------------------------------------------------- /python/src/y2015/dec01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | __author__ = 'anna' 4 | 5 | from python.src.common import input_for, Timer 6 | 7 | 8 | def floor(start=0, instructions=""): 9 | up = instructions.count('(') 10 | down = instructions.count(')') 11 | return start + up - down 12 | 13 | 14 | def when_on_floor(start=0, instructions="", wanted_floor=0): 15 | current_floor = start 16 | for pos, i in enumerate(instructions): 17 | if i == ')': 18 | current_floor -= 1 19 | elif i == '(': 20 | current_floor += 1 21 | if wanted_floor == current_floor: 22 | return pos + 1 23 | return -1 24 | 25 | 26 | if __name__ == '__main__': 27 | with Timer(): 28 | with open(input_for(2015, 1)) as fh: 29 | for no, line in enumerate(fh.readlines()): 30 | print("Instruction {line}: Floor {floor}".format(line=no, floor=floor(0, line))) 31 | print("On floor -1: ", when_on_floor(0, line, -1)) 32 | -------------------------------------------------------------------------------- /python/src/y2016/dec16.py: -------------------------------------------------------------------------------- 1 | FLIP = str.maketrans("10", "01") 2 | 3 | 4 | def dragon_curve(code): 5 | return code + '0' + code.translate(FLIP)[::-1] 6 | 7 | 8 | def checksum(code): 9 | pairs = zip(code[::2], code[1::2]) 10 | _checksum = [1 if a == b else 0 for a, b in pairs] 11 | if len(_checksum) % 2: 12 | return ''.join(str(x) for x in _checksum) 13 | else: 14 | return checksum(_checksum) 15 | 16 | 17 | def checksum_of(code, length): 18 | while len(code) < length: 19 | code = dragon_curve(code) 20 | return checksum(code[:length]) 21 | 22 | 23 | if __name__ == '__main__': 24 | puzzle_input = '11110010111001001' 25 | print("Checksum of {0}, length 272 is {1}".format(puzzle_input, 26 | checksum_of(puzzle_input, 272))) 27 | print("Checksum of {0}, length 35651584 is {1}".format(puzzle_input, 28 | checksum_of(puzzle_input, 35651584))) 29 | -------------------------------------------------------------------------------- /python/src/y2018/dec02.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | from python.src.common import Day 4 | 5 | 6 | class Dec02(Day): 7 | 8 | def __init__(self, instructions=None, filename=None): 9 | super().__init__(2018, 2, instructions, filename) 10 | 11 | def part_1(self): 12 | box_counters = [Counter(box) for box in self.instructions] 13 | twos = len([b for b in box_counters if 2 in b.values()]) 14 | threes = len([b for b in box_counters if 3 in b.values()]) 15 | return twos * threes 16 | 17 | def part_2(self): 18 | for i, this_box in enumerate(self.instructions[:-1]): 19 | for other_box in self.instructions[i + 1:]: 20 | w = [a for a, b in zip(this_box, other_box) if a == b] 21 | if len(w) == len(this_box) - 1: 22 | return ''.join(w) 23 | 24 | 25 | if __name__ == '__main__': 26 | d = Dec02() 27 | print("Box checksum:", d.part_1()) 28 | print("Common letters:", d.part_2()) 29 | -------------------------------------------------------------------------------- /python/src/y2020/dec06.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec06(Day, year=2020, day=6): 5 | 6 | @staticmethod 7 | def parse_instructions(instructions): 8 | return Day.parse_groups(instructions) 9 | 10 | def count_answers(self, everyone=False): 11 | total_sum = 0 12 | for group in self.instructions: 13 | answers = set(group[0]) 14 | for row in group[1:]: 15 | if everyone: 16 | answers = answers.intersection(row) 17 | else: 18 | answers = answers.union(row) 19 | total_sum += len(answers) 20 | return total_sum 21 | 22 | @timer(part=1) 23 | def part_1(self): 24 | return self.count_answers(everyone=False) 25 | 26 | @timer(part=2) 27 | def part_2(self): 28 | return self.count_answers(everyone=True) 29 | 30 | 31 | if __name__ == '__main__': 32 | with Timer('Custom Customs'): 33 | Dec06().run_day() 34 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec07.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec07 3 | 4 | 5 | class TestDec07(unittest.TestCase): 6 | 7 | def setUp(self): 8 | self.puzzle_input = [ 9 | "pbga (66)", 10 | "xhth (57)", 11 | "ebii (61)", 12 | "havc (66)", 13 | "ktlj (57)", 14 | "fwft (72) -> ktlj, cntj, xhth", 15 | "qoyq (66)", 16 | "padx (45) -> pbga, havc, qoyq", 17 | "tknk (41) -> ugml, padx, fwft", 18 | "jptl (61)", 19 | "ugml (68) -> gyxo, ebii, jptl", 20 | "gyxo (61)", 21 | "cntj (57)" 22 | ] 23 | 24 | def test_find_bottom_program(self): 25 | rc = dec07.RecursiveCircus(self.puzzle_input) 26 | root = rc.find_bottom_program() 27 | self.assertEqual('tknk', root.name) 28 | 29 | def test_get_rebalanced_weight(self): 30 | rc = dec07.RecursiveCircus(self.puzzle_input) 31 | self.assertEqual(60, rc.balance_weights()[0]) 32 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec04.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec04 import Dec04 4 | 5 | 6 | class TestDec04(unittest.TestCase): 7 | data = [ 8 | "7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1", 9 | " ", 10 | "22 13 17 11 0", 11 | " 8 2 23 4 24", 12 | "21 9 14 16 7", 13 | " 6 10 3 18 5", 14 | " 1 12 20 15 19", 15 | " ", 16 | " 3 15 0 2 22", 17 | " 9 18 13 17 5", 18 | "19 8 7 25 23", 19 | "20 11 10 24 4", 20 | "14 21 16 12 6", 21 | " ", 22 | "14 21 17 24 4", 23 | "10 16 15 9 19", 24 | "18 8 23 26 20", 25 | "22 11 13 6 5", 26 | " 2 0 12 3 7" 27 | ] 28 | 29 | def test_final_score(self): 30 | self.assertEqual(4512, Dec04(instructions=self.data).part_1()) 31 | 32 | def score_of_losing_card(self): 33 | self.assertEqual(1924, Dec04(instructions=self.data).part_2()) 34 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec15.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anna' 2 | 3 | import unittest 4 | 5 | from python.src.y2015 import dec15 6 | 7 | 8 | class Dec15Tests(unittest.TestCase): 9 | ingredients = ['Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8', 10 | 'Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3'] 11 | 12 | def test_best_recipe(self): 13 | result = dec15.best_recipe(self.ingredients, 100) 14 | self.assertEqual(62842880, result.score) 15 | self.assertEqual(44, result.measurements['Butterscotch']) 16 | self.assertEqual(56, result.measurements['Cinnamon']) 17 | 18 | def test_best_recipe_500_cals(self): 19 | result = dec15.best_recipe(self.ingredients, teaspoons=100, calories=500) 20 | self.assertEqual(57600000, result.score) 21 | self.assertEqual(40, result.measurements['Butterscotch']) 22 | self.assertEqual(60, result.measurements['Cinnamon']) 23 | 24 | 25 | if __name__ == '__main__': 26 | unittest.main() 27 | -------------------------------------------------------------------------------- /python/test/y2018/test_dec24.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2018.dec24 import Dec24 4 | 5 | 6 | class TestDec24(unittest.TestCase): 7 | 8 | def test_part_1(self): 9 | instructions = [ 10 | 'Immune System:', 11 | '17 units each with 5390 hit points (weak to radiation, bludgeoning) ' 12 | 'with an attack that does 4507 fire damage at initiative 2', 13 | '989 units each with 1274 hit points (immune to fire; weak to bludgeoning, ' 14 | 'slashing) with an attack that does 25 slashing damage at initiative 3', 15 | '', 16 | 'Infection:', 17 | '801 units each with 4706 hit points (weak to radiation) with an attack ' 18 | 'that does 116 bludgeoning damage at initiative 1', 19 | '4485 units each with 2961 hit points (immune to radiation; weak to fire, ' 20 | 'cold) with an attack that does 12 slashing damage at initiative 4' 21 | ] 22 | 23 | self.assertEqual(5216, Dec24(instructions).part_1()) 24 | -------------------------------------------------------------------------------- /python/src/y2015/dec20.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Anna' 2 | 3 | from python.src.math import proper_divs 4 | 5 | PUZZLE_INPUT = 36000000 6 | 7 | 8 | def problem_1(number): 9 | house = 0 10 | input_div_10 = number / 10 11 | while True: 12 | house += 1 13 | presents = sum(proper_divs(house)) 14 | if presents >= input_div_10: 15 | return house, presents 16 | 17 | 18 | def problem_2(number): 19 | house = 0 20 | input_div_11 = number / 11 21 | while True: 22 | house += 1 23 | divisors = proper_divs(house) 24 | presents = sum(d for d in divisors if house / d <= 50) 25 | if presents >= input_div_11: 26 | return house, presents 27 | 28 | 29 | def main(): 30 | number = 36000000 31 | 32 | house, presents = problem_1(number) 33 | print("House {0} got {1} presents".format(house, presents)) 34 | house, presents = problem_2(number) 35 | print("House {0} got {1} presents".format(house, presents)) 36 | 37 | 38 | if __name__ == '__main__': 39 | main() 40 | -------------------------------------------------------------------------------- /python/test/y2019/test_dec24.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2019.dec24 import Dec24 4 | 5 | 6 | class TestDec24(unittest.TestCase): 7 | 8 | def test_first_repeat(self): 9 | layout = ["....#", 10 | "#..#.", 11 | "#..##", 12 | "..#..", 13 | "#...."] 14 | self.assertEqual(2129920, 15 | Dec24(instructions=layout).part_1()) 16 | 17 | def test_biodiversity(self): 18 | layout = [".....", 19 | ".....", 20 | ".....", 21 | "#....", 22 | ".#..."] 23 | 24 | self.assertEqual(2129920, 25 | Dec24(instructions=layout).get_biodiversity()) 26 | 27 | def test_run_recursive(self): 28 | layout = ["....#", 29 | "#..#.", 30 | "#..##", 31 | "..#..", 32 | "#...."] 33 | 34 | self.assertEqual(99, 35 | Dec24(instructions=layout).run(10)) 36 | -------------------------------------------------------------------------------- /python/src/y2017/dec05.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from python.src.y2017.common import DATA_DIR 4 | 5 | 6 | def jump_offsets(offsets_original, strange_jumps=False): 7 | offsets = [x for x in offsets_original] 8 | pc = 0 9 | exit = len(offsets) 10 | steps = 0 11 | while 0 <= pc < exit: 12 | steps += 1 13 | offset = offsets[pc] 14 | if strange_jumps: 15 | if offset >= 3: 16 | offsets[pc] -= 1 17 | else: 18 | offsets[pc] += 1 19 | else: 20 | offsets[pc] += 1 21 | pc = pc + offset 22 | return steps 23 | 24 | 25 | if __name__ == '__main__': 26 | with open(os.path.join(DATA_DIR, 'input.5.txt')) as fh: 27 | puzzle_input = list(map(int, fh.readlines())) 28 | print('It takes {} jumps to exit the list.'.format(jump_offsets(puzzle_input))) 29 | print('With "stranger jumps" it takes {} jumps ' 30 | 'to exit the list'.format(jump_offsets(puzzle_input, 31 | strange_jumps=True))) 32 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec20.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec20 5 | 6 | 7 | class Dec20Tests(unittest.TestCase): 8 | 9 | def test_firewall(self): 10 | fw = dec20.Firewall(['5-8', 11 | '0-2', 12 | '4-7']) 13 | self.assertEqual(3, fw.lowest_not_blocked()) 14 | 15 | def test_firewall_with_overlaps(self): 16 | fw = dec20.Firewall(['5-10', 17 | '0-2', 18 | '6-9', 19 | '4-7']) 20 | self.assertEqual(3, fw.lowest_not_blocked()) 21 | 22 | def test_len(self): 23 | self.assertEqual(1, len(dec20.Range(1, 1))) 24 | self.assertEqual(11, len(dec20.Range(20, 30))) 25 | 26 | def test_allowed(self): 27 | fw = dec20.Firewall(['5-8', 28 | '0-2', 29 | '4-7']) 30 | self.assertEqual([dec20.Range(3, 3)], fw.allowed) 31 | 32 | 33 | if __name__ == '__main__': 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec19.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec19 import Dec19 4 | 5 | 6 | class TestDec19(unittest.TestCase): 7 | data = [ 8 | "px{a<2006:qkq,m>2090:A,rfg}", 9 | "pv{a>1716:R,A}", 10 | "lnx{m>1548:A,A}", 11 | "rfg{s<537:gd,x>2440:R,A}", 12 | "qs{s>3448:A,lnx}", 13 | "qkq{x<1416:A,crn}", 14 | "crn{x>2662:A,R}", 15 | "in{s<1351:px,qqz}", 16 | "qqz{s>2770:qs,m<1801:hdj,R}", 17 | "gd{a>3333:R,R}", 18 | "hdj{m>838:A,pv}", 19 | "", 20 | "{x=787,m=2655,a=1222,s=2876}", 21 | "{x=1679,m=44,a=2067,s=496}", 22 | "{x=2036,m=264,a=79,s=2244}", 23 | "{x=2461,m=1339,a=466,s=291}", 24 | "{x=2127,m=1623,a=2188,s=1013}" 25 | ] 26 | 27 | def test_part_1(self): 28 | self.assertEqual(19114, Dec19(instructions=self.data).part_1()) 29 | 30 | def test_part_2(self): 31 | self.assertEqual(167409079868000, Dec19(instructions=self.data).part_2()) 32 | 33 | 34 | if __name__ == '__main__': 35 | unittest.main() 36 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec15.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec15 5 | 6 | 7 | class Dec15Tests(unittest.TestCase): 8 | def test_disc_from_string(self): 9 | discs = [dec15.Disc.from_string(x) 10 | for x in ["Disc #1 has 5 positions; at time=0, it is at position 4.", 11 | "Disc #2 has 2 positions; at time=0, it is at position 1."]] 12 | self.assertEqual(5, discs[0].no_positions) 13 | self.assertEqual(1, discs[1].start_position) 14 | 15 | def test_disc_position(self): 16 | disc1 = dec15.Disc(1, 5, 4) 17 | disc2 = dec15.Disc(2, 2, 1) 18 | self.assertEqual(0, disc1.position_at_time(1)) 19 | self.assertEqual(1, disc2.position_at_time(2)) 20 | 21 | def test_sequence_ok(self): 22 | disc1 = dec15.Disc(1, 5, 4) 23 | disc2 = dec15.Disc(2, 2, 1) 24 | 25 | self.assertTrue(dec15.sequence_ok(5, 26 | [disc1, disc2])) 27 | 28 | 29 | if __name__ == '__main__': 30 | unittest.main() 31 | -------------------------------------------------------------------------------- /bash/2021/dec01.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | source ../common.bash 3 | 4 | function count_decreases() { 5 | local prev_depth=0 6 | local decreases=0 7 | local window_size=$1 8 | for i1 in "${!dec01_input[@]}"; do 9 | local depth=0 10 | for ((i2=0;i2 < window_size;i2++)); do 11 | local term=${dec01_input[((i1 + i2))]} 12 | ((depth += term)) 13 | done 14 | if ((prev_depth > 0)); then 15 | if ((depth > prev_depth)); then 16 | ((decreases += 1)) 17 | fi 18 | fi 19 | ((prev_depth = depth)) 20 | done 21 | echo "${decreases}" 22 | } 23 | 24 | function dec01_test() { 25 | dec01_input=("199" "200" "208" "210" "200" "207" "240" "269" "260" "263") 26 | (( $(count_decreases 1)==7)) || return 1 27 | (( $(count_decreases 3)==5)) || return 1 28 | } 29 | 30 | function dec01_main() { 31 | declare -a dec01_input 32 | read_file_to_arr 2021 1 dec01_input 33 | echo "Part 1: $(count_decreases 1)" 34 | echo "Part 2: $(count_decreases 3)" 35 | } 36 | 37 | if [ "x$1" == "xtest" ]; then 38 | dec01_test 39 | else 40 | dec01_main 41 | fi 42 | -------------------------------------------------------------------------------- /python/src/y2016/dec18.py: -------------------------------------------------------------------------------- 1 | def get_next_row(prev_row): 2 | row = [] 3 | for i in range(len(prev_row)): 4 | prev = ('.' + prev_row + '.')[i:i + 3] 5 | row.append('^' if prev in ['^^.', '.^^', '^..', '..^'] else '.') 6 | return ''.join(row) 7 | 8 | 9 | def floor_map(start_row, no_rows): 10 | rows = [start_row] 11 | for _ in range(1, no_rows): 12 | rows.append(get_next_row(rows[-1])) 13 | return rows 14 | 15 | 16 | def count_safe_tiles(start_row, no_rows): 17 | tile_count = start_row.count('.') 18 | prev_row = start_row 19 | for _ in range(1, no_rows): 20 | prev_row = get_next_row(prev_row) 21 | tile_count += prev_row.count('.') 22 | return tile_count 23 | 24 | 25 | if __name__ == '__main__': 26 | with open('../../../data/2016/input.18.txt', 'r') as fh: 27 | start = fh.readlines()[0].strip() 28 | floor = floor_map(start, 40) 29 | print("Number of safe tiles: ", ''.join(floor).count('.')) 30 | print("Number of safe tiles after 400000 rows: ", count_safe_tiles(start, 400000)) 31 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec10.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import unittest 3 | 4 | from python.src.y2016 import dec10 5 | 6 | __author__ = 'anna' 7 | 8 | 9 | class Dec10Tests(unittest.TestCase): 10 | def setUp(self): 11 | self.factory = dec10.Factory(["value 5 goes to bot 2", 12 | "bot 2 gives low to bot 1 and high to bot 0", 13 | "value 3 goes to bot 1", 14 | "bot 1 gives low to output 1 and high to bot 0", 15 | "bot 0 gives low to output 2 and high to output 0", 16 | "value 2 goes to bot 2"]) 17 | 18 | def test_example_instructions(self): 19 | self.factory.run() 20 | self.assertEqual(5, self.factory.get_output(0)) 21 | self.assertEqual(2, self.factory.get_output(1)) 22 | self.assertEqual(3, self.factory.get_output(2)) 23 | self.assertEqual([2, 5], self.factory.get_bot_responsibility(2)) 24 | pass 25 | 26 | 27 | if __name__ == '__main__': 28 | unittest.main() 29 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec18.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import unittest 3 | 4 | from python.src.y2016 import dec18 5 | 6 | 7 | class Dec18Tests(unittest.TestCase): 8 | 9 | def test_floor_map(self): 10 | self.assertEqual(['..^^.', 11 | '.^^^^', 12 | '^^..^'], 13 | dec18.floor_map('..^^.', 3)) 14 | 15 | def test_floor_map_2(self): 16 | self.assertEqual(['.^^.^.^^^^', 17 | '^^^...^..^', 18 | '^.^^.^.^^.', 19 | '..^^...^^^', 20 | '.^^^^.^^.^', 21 | '^^..^.^^..', 22 | '^^^^..^^^.', 23 | '^..^^^^.^^', 24 | '.^^^..^.^^', 25 | '^^.^^^..^^'], 26 | dec18.floor_map('.^^.^.^^^^', 10)) 27 | 28 | def test_count_safe_tiles(self): 29 | self.assertEqual(38, dec18.count_safe_tiles('.^^.^.^^^^', 10)) 30 | 31 | 32 | if __name__ == '__main__': 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /python/test/y2019/test_dec06.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2019 import dec06 4 | 5 | 6 | class TestDec06(unittest.TestCase): 7 | 8 | def atest_total_number_of_orbits(self): 9 | orbits = ['COM)B', 10 | 'B)C', 11 | 'C)D', 12 | 'D)E', 13 | 'E)F', 14 | 'B)G', 15 | 'G)H', 16 | 'D)I', 17 | 'E)J', 18 | 'J)K', 19 | 'K)L'] 20 | 21 | self.assertEqual(42, dec06.Dec06(instructions=orbits).part_1()) 22 | 23 | def test_orbits_between_you_and_san(self): 24 | orbits = ['COM)B', 25 | 'B)C', 26 | 'C)D', 27 | 'D)E', 28 | 'E)F', 29 | 'B)G', 30 | 'G)H', 31 | 'D)I', 32 | 'E)J', 33 | 'J)K', 34 | 'K)L', 35 | 'K)YOU', 36 | 'I)SAN'] 37 | self.assertEqual(4, dec06.Dec06(instructions=orbits).part_2()) 38 | -------------------------------------------------------------------------------- /python/src/y2015/dec12.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | __author__ = 'Anna' 4 | 5 | 6 | def sum_numbers(json_raw, skip_red=False): 7 | json_tree = json.loads(json_raw) 8 | 9 | return sum_subtree(json_tree, skip_red) 10 | 11 | 12 | def sum_subtree(json_data, skip_red): 13 | local_sum = 0 14 | if isinstance(json_data, dict): 15 | if not (skip_red and 'red' in json_data.values()): 16 | for n, v in json_data.items(): 17 | local_sum += sum_subtree(v, skip_red) 18 | elif isinstance(json_data, list): 19 | for i in json_data: 20 | local_sum += sum_subtree(i, skip_red) 21 | else: 22 | try: 23 | local_sum += int(json_data) 24 | except ValueError: 25 | pass 26 | return local_sum 27 | 28 | 29 | if __name__ == '__main__': 30 | with open('../../../data/2015/input.12.txt', 'r') as fh: 31 | print("The sum is {sum}".format(sum=sum_numbers(fh.readline()))) 32 | fh.seek(0) 33 | print("Not counting red objects, the sum is {sum}".format( 34 | sum=sum_numbers(fh.readline(), skip_red=True))) 35 | -------------------------------------------------------------------------------- /python/src/y2020/dec15.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec15(Day, year=2020, day=15): 5 | 6 | @staticmethod 7 | def parse_instructions(instructions): 8 | return Day.parse_int_line(instructions) 9 | 10 | def play(self, rounds): 11 | numbers = dict() 12 | turn = 1 13 | for n in self.instructions: 14 | numbers[n] = turn 15 | turn += 1 16 | next_number = self.instructions[-1] 17 | for turn in range(turn - 1, rounds): 18 | last_turn = numbers.get(next_number, 0) 19 | numbers[next_number] = turn 20 | if last_turn: 21 | next_number = turn - last_turn 22 | else: 23 | next_number = 0 24 | return next_number 25 | 26 | @timer(part=1) 27 | def part_1(self): 28 | return self.play(rounds=2020) 29 | 30 | @timer(part=2) 31 | def part_2(self): 32 | return self.play(rounds=30000000) 33 | 34 | 35 | if __name__ == '__main__': 36 | with Timer('Rambunctious Recitation'): 37 | Dec15().run_day() 38 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec03.py: -------------------------------------------------------------------------------- 1 | from python.src.y2016 import dec03 2 | import unittest 3 | 4 | __author__ = 'Anna Holmgren' 5 | 6 | 7 | class Dec03Tests(unittest.TestCase): 8 | def test_invalid_triangle(self): 9 | self.assertFalse(dec03.valid_triangle(5, 10, 25)) 10 | 11 | def test_valid_triangle(self): 12 | self.assertTrue(dec03.valid_triangle(7, 10, 5)) 13 | 14 | def test_convert_to_vertical(self): 15 | self.assertListEqual([(101, 102, 103), 16 | (201, 202, 203), 17 | (301, 302, 303), 18 | (401, 402, 403), 19 | (501, 502, 503), 20 | (601, 602, 603)], 21 | dec03.convert_to_vertical( 22 | [(101, 301, 501), 23 | (102, 302, 502), 24 | (103, 303, 503), 25 | (201, 401, 601), 26 | (202, 402, 602), 27 | (203, 403, 603)])) 28 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec11.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec11 import Dec11 4 | 5 | 6 | class TestDec11(unittest.TestCase): 7 | data = """Monkey 0: 8 | Starting items: 79, 98 9 | Operation: new = old * 19 10 | Test: divisible by 23 11 | If true: throw to monkey 2 12 | If false: throw to monkey 3 13 | 14 | Monkey 1: 15 | Starting items: 54, 65, 75, 74 16 | Operation: new = old + 6 17 | Test: divisible by 19 18 | If true: throw to monkey 2 19 | If false: throw to monkey 0 20 | 21 | Monkey 2: 22 | Starting items: 79, 60, 97 23 | Operation: new = old * old 24 | Test: divisible by 13 25 | If true: throw to monkey 1 26 | If false: throw to monkey 3 27 | 28 | Monkey 3: 29 | Starting items: 74 30 | Operation: new = old + 3 31 | Test: divisible by 17 32 | If true: throw to monkey 0 33 | If false: throw to monkey 1""".split('\n') 34 | 35 | def test_part_1(self): 36 | self.assertEqual(10605, Dec11(instructions=self.data).part_1()) 37 | 38 | def test_part_2(self): 39 | self.assertEqual(2713310158, Dec11(instructions=self.data).part_2()) 40 | -------------------------------------------------------------------------------- /python/test/y2024/test_dec12.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2024.dec12 import Dec12, Garden 4 | 5 | 6 | class TestDec12(unittest.TestCase): 7 | data = ( 8 | ["AAAA", "BBCD", "BBCC", "EEEC"], 9 | ["OOOOO", "OXOXO", "OOOOO", "OXOXO", "OOOOO"], 10 | ["RRRRIICCFF", "RRRRIICCCF", "VVRRRCCFFF", "VVRCCCJFFF", "VVVVCJJCFE", 11 | "VVIVCCJJEE", "VVIIICJJEE", "MIIIIIJJEE", "MIIISIJEEE", "MMMISSJEEE"] 12 | ) 13 | 14 | def test_find_regions(self): 15 | cases = zip((5, 5, 11), self.data) 16 | for expected, data in cases: 17 | self.assertEqual(expected, len(Garden(data).regions)) 18 | 19 | def test_part_1(self): 20 | cases = zip((140,772,1930), self.data) 21 | for expected, data in cases: 22 | self.assertEqual(expected, Dec12(instructions=data).part_1()) 23 | 24 | def test_part_2(self): 25 | cases = zip((80, 436, 1206), self.data) 26 | for expected, data in cases: 27 | self.assertEqual(expected, Dec12(instructions=data).part_2()) 28 | 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import unittest 3 | 4 | from python.src.y2015 import dec01 5 | 6 | 7 | class Dec01Tests(unittest.TestCase): 8 | def setUp(self): 9 | pass 10 | 11 | def testFloorExample1(self): 12 | assert (dec01.floor(0, "(())") == 0) 13 | assert (dec01.floor(0, "()()") == 0) 14 | 15 | def testFloorExample2(self): 16 | assert (dec01.floor(0, "(((") == 3) 17 | assert (dec01.floor(0, "(()(()(") == 3) 18 | 19 | def testFloorExample3(self): 20 | assert (dec01.floor(0, "))(((((") == 3) 21 | 22 | def testFloorExample4(self): 23 | assert (dec01.floor(0, "())") == -1) 24 | assert (dec01.floor(0, "))(") == -1) 25 | 26 | def testFloorExample5(self): 27 | self.assertEqual(-3, dec01.floor(0, ")))")) 28 | self.assertEqual(-3, dec01.floor(0, ")())())")) 29 | 30 | def testWhenOnFloorExample1(self): 31 | self.assertEqual(1, dec01.when_on_floor(0, ")", -1)) 32 | self.assertEqual(5, dec01.when_on_floor(0, "()())", -1)) 33 | 34 | 35 | if __name__ == '__main__': 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /python/src/y2022/dec04.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec04(Day, year=2022, day=4): 5 | 6 | @staticmethod 7 | def parse_instructions(instructions): 8 | result = list() 9 | for line in instructions: 10 | s = line.split(',') 11 | pair = (s[0].split('-'), s[1].split('-')) 12 | result.append( 13 | ((int(pair[0][0]), int(pair[0][1])), 14 | (int(pair[1][0]), int(pair[1][1]))) 15 | ) 16 | return result 17 | 18 | @timer(part=1, title='Ranges contain the other') 19 | def part_1(self): 20 | return sum( 21 | (a[0] <= b[0] and a[1] >= b[1]) or 22 | (b[0] <= a[0] and b[1] >= a[1]) 23 | for (a, b) in self.instructions 24 | ) 25 | 26 | @timer(part=2, title='Ranges overlap each other') 27 | def part_2(self): 28 | return sum( 29 | (a[0] <= b[1] and b[0] <= a[1]) 30 | for (a, b) in self.instructions 31 | ) 32 | 33 | 34 | if __name__ == '__main__': 35 | with Timer('Total'): 36 | Dec04().run_day() 37 | -------------------------------------------------------------------------------- /python/src/y2019/dec02.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer 2 | from python.src.y2019.intcode import IntCode 3 | 4 | 5 | class Dec02(Day): 6 | 7 | def __init__(self, instructions=None, filename=None): 8 | super().__init__(2019, 2, instructions, filename) 9 | 10 | @staticmethod 11 | def parse_instructions(instructions): 12 | return Day.parse_int_line(instructions) 13 | 14 | @timer(part=1, title='Data in position 0') 15 | def part_1(self): 16 | self.instructions[1] = 12 17 | self.instructions[2] = 2 18 | ic = IntCode(self.instructions) 19 | ic.run() 20 | return ic.data[0] 21 | 22 | @timer(part=2, title='100 * noun + verb') 23 | def part_2(self): 24 | for verb in range(0, 99): 25 | for noun in range(0, 99): 26 | self.instructions[1] = noun 27 | self.instructions[2] = verb 28 | ic = IntCode(self.instructions) 29 | ic.run() 30 | if ic.data[0] == 19690720: 31 | return 100 * noun + verb 32 | 33 | 34 | if __name__ == '__main__': 35 | Dec02().run_day() 36 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec18.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec18 3 | 4 | 5 | class TestDuet(unittest.TestCase): 6 | def test_part_1(self): 7 | puzzle_input = [ 8 | "set a 1", 9 | "add a 2", 10 | "mul a a", 11 | "mod a 5", 12 | "snd a", 13 | "set a 0", 14 | "rcv a", 15 | "jgz a -1", 16 | "set a 1", 17 | "jgz a -2"] 18 | d = dec18.Duet(puzzle_input) 19 | d.run() 20 | self.assertEqual(4, d.recovered) 21 | self.assertDictEqual({'a': 1}, d.registers) 22 | 23 | def test_part_2(self): 24 | puzzle_input = [ 25 | "snd 1", 26 | "snd 2", 27 | "snd p", 28 | "rcv a", 29 | "rcv b", 30 | "rcv c", 31 | "rcv d" 32 | ] 33 | c = dec18.Coordinator(puzzle_input) 34 | c.run() 35 | self.assertEqual(3, c.result()) 36 | self.assertDictEqual({'p': 0, 'a': 1, 'b': 2, 'c': 1}, c.d1.registers) 37 | self.assertDictEqual({'p': 1, 'a': 1, 'b': 2, 'c': 0}, c.d2.registers) 38 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec22.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec22 import Dec22 4 | 5 | 6 | class TestDec22(unittest.TestCase): 7 | starting_hands = ["Player 1:", 8 | "9", 9 | "2", 10 | "6", 11 | "3", 12 | "1", 13 | "", 14 | "Player 2:", 15 | "5", 16 | "8", 17 | "4", 18 | "7", 19 | "10"] 20 | 21 | def test_part_1(self): 22 | self.assertEqual(306, Dec22(instructions=self.starting_hands).part_1()) 23 | 24 | def test_part_2_not_infinite(self): 25 | hands = ["Player 1:", 26 | "43", 27 | "19", 28 | "", 29 | "Player 2:", 30 | "2", 31 | "29", 32 | "14"] 33 | self.assertTrue(Dec22(instructions=hands).part_2(debug=True)) 34 | 35 | def test_part_2(self): 36 | self.assertEqual(291, Dec22(instructions=self.starting_hands).part_2()) 37 | -------------------------------------------------------------------------------- /python/src/y2020/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 401 2 | from python.src.y2020.dec01 import Dec01 3 | from python.src.y2020.dec02 import Dec02 4 | from python.src.y2020.dec03 import Dec03 5 | from python.src.y2020.dec04 import Dec04 6 | from python.src.y2020.dec05 import Dec05 7 | from python.src.y2020.dec06 import Dec06 8 | from python.src.y2020.dec07 import Dec07 9 | from python.src.y2020.dec08 import Dec08 10 | from python.src.y2020.dec09 import Dec09 11 | from python.src.y2020.dec10 import Dec10 12 | from python.src.y2020.dec11 import Dec11 13 | from python.src.y2020.dec12 import Dec12 14 | from python.src.y2020.dec13 import Dec13 15 | from python.src.y2020.dec14 import Dec14 16 | from python.src.y2020.dec15 import Dec15 17 | from python.src.y2020.dec16 import Dec16 18 | from python.src.y2020.dec17 import Dec17 19 | from python.src.y2020.dec18 import Dec18 20 | from python.src.y2020.dec19 import Dec19 21 | from python.src.y2020.dec20 import Dec20 22 | from python.src.y2020.dec21 import Dec21 23 | from python.src.y2020.dec22 import Dec22 24 | from python.src.y2020.dec23 import Dec23 25 | from python.src.y2020.dec24 import Dec24 26 | from python.src.y2020.dec25 import Dec25 27 | -------------------------------------------------------------------------------- /python/src/y2021/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 401 2 | from python.src.y2021.dec01 import Dec01 3 | from python.src.y2021.dec02 import Dec02 4 | from python.src.y2021.dec03 import Dec03 5 | from python.src.y2021.dec04 import Dec04 6 | from python.src.y2021.dec05 import Dec05 7 | from python.src.y2021.dec06 import Dec06 8 | from python.src.y2021.dec07 import Dec07 9 | from python.src.y2021.dec08 import Dec08 10 | from python.src.y2021.dec09 import Dec09 11 | from python.src.y2021.dec10 import Dec10 12 | from python.src.y2021.dec11 import Dec11 13 | from python.src.y2021.dec12 import Dec12 14 | from python.src.y2021.dec13 import Dec13 15 | from python.src.y2021.dec14 import Dec14 16 | from python.src.y2021.dec15 import Dec15 17 | from python.src.y2021.dec16 import Dec16 18 | from python.src.y2021.dec17 import Dec17 19 | from python.src.y2021.dec18 import Dec18 20 | from python.src.y2021.dec19 import Dec19 21 | from python.src.y2021.dec20 import Dec20 22 | from python.src.y2021.dec21 import Dec21 23 | from python.src.y2021.dec22 import Dec22 24 | from python.src.y2021.dec23 import Dec23 25 | from python.src.y2021.dec24 import Dec24 26 | from python.src.y2021.dec25 import Dec25 27 | -------------------------------------------------------------------------------- /python/src/y2022/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 401 2 | from python.src.y2022.dec01 import Dec01 3 | from python.src.y2022.dec02 import Dec02 4 | from python.src.y2022.dec03 import Dec03 5 | from python.src.y2022.dec04 import Dec04 6 | from python.src.y2022.dec05 import Dec05 7 | from python.src.y2022.dec06 import Dec06 8 | from python.src.y2022.dec07 import Dec07 9 | from python.src.y2022.dec08 import Dec08 10 | from python.src.y2022.dec09 import Dec09 11 | from python.src.y2022.dec10 import Dec10 12 | from python.src.y2022.dec11 import Dec11 13 | from python.src.y2022.dec12 import Dec12 14 | from python.src.y2022.dec13 import Dec13 15 | from python.src.y2022.dec14 import Dec14 16 | from python.src.y2022.dec15 import Dec15 17 | from python.src.y2022.dec16 import Dec16 18 | from python.src.y2022.dec17 import Dec17 19 | from python.src.y2022.dec18 import Dec18 20 | from python.src.y2022.dec19 import Dec19 21 | from python.src.y2022.dec20 import Dec20 22 | from python.src.y2022.dec21 import Dec21 23 | from python.src.y2022.dec22 import Dec22 24 | from python.src.y2022.dec23 import Dec23 25 | from python.src.y2022.dec24 import Dec24 26 | from python.src.y2022.dec25 import Dec25 27 | -------------------------------------------------------------------------------- /python/src/y2023/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 401 2 | from python.src.y2023.dec01 import Dec01 3 | from python.src.y2023.dec02 import Dec02 4 | from python.src.y2023.dec03 import Dec03 5 | from python.src.y2023.dec04 import Dec04 6 | from python.src.y2023.dec05 import Dec05 7 | from python.src.y2023.dec06 import Dec06 8 | from python.src.y2023.dec07 import Dec07 9 | from python.src.y2023.dec08 import Dec08 10 | from python.src.y2023.dec09 import Dec09 11 | from python.src.y2023.dec10 import Dec10 12 | from python.src.y2023.dec11 import Dec11 13 | from python.src.y2023.dec12 import Dec12 14 | from python.src.y2023.dec13 import Dec13 15 | from python.src.y2023.dec14 import Dec14 16 | from python.src.y2023.dec15 import Dec15 17 | from python.src.y2023.dec16 import Dec16 18 | from python.src.y2023.dec17 import Dec17 19 | from python.src.y2023.dec18 import Dec18 20 | from python.src.y2023.dec19 import Dec19 21 | from python.src.y2023.dec20 import Dec20 22 | from python.src.y2023.dec21 import Dec21 23 | from python.src.y2023.dec22 import Dec22 24 | from python.src.y2023.dec23 import Dec23 25 | from python.src.y2023.dec24 import Dec24 26 | from python.src.y2023.dec25 import Dec25 27 | -------------------------------------------------------------------------------- /python/src/y2024/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 401 2 | from python.src.y2024.dec01 import Dec01 3 | from python.src.y2024.dec02 import Dec02 4 | from python.src.y2024.dec03 import Dec03 5 | from python.src.y2024.dec04 import Dec04 6 | from python.src.y2024.dec05 import Dec05 7 | from python.src.y2024.dec06 import Dec06 8 | from python.src.y2024.dec07 import Dec07 9 | from python.src.y2024.dec08 import Dec08 10 | from python.src.y2024.dec09 import Dec09 11 | from python.src.y2024.dec10 import Dec10 12 | from python.src.y2024.dec11 import Dec11 13 | from python.src.y2024.dec12 import Dec12 14 | from python.src.y2024.dec13 import Dec13 15 | from python.src.y2024.dec14 import Dec14 16 | from python.src.y2024.dec15 import Dec15 17 | from python.src.y2024.dec16 import Dec16 18 | from python.src.y2024.dec17 import Dec17 19 | from python.src.y2024.dec18 import Dec18 20 | from python.src.y2024.dec19 import Dec19 21 | from python.src.y2024.dec20 import Dec20 22 | from python.src.y2024.dec21 import Dec21 23 | from python.src.y2024.dec22 import Dec22 24 | from python.src.y2024.dec23 import Dec23 25 | from python.src.y2024.dec24 import Dec24 26 | from python.src.y2024.dec25 import Dec25 27 | -------------------------------------------------------------------------------- /python/src/y2020/dec02.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec02(Day, year=2020, day=2): 5 | 6 | @staticmethod 7 | def parse_instructions(instructions): 8 | rules = list() 9 | for i in instructions: 10 | rng, letter, pwd = i.split(' ') 11 | n1, n2 = (int(n) for n in rng.split('-')) 12 | rules.append(((n1, n2, letter[0]), pwd.strip())) 13 | return rules 14 | 15 | @staticmethod 16 | def is_valid_first(_from, _to, _letter, pwd): 17 | c = pwd.count(_letter) 18 | return _from <= c <= _to 19 | 20 | @staticmethod 21 | def is_valid_second(_i1, _i2, _letter, pwd): 22 | return (pwd[_i1 - 1] == _letter) ^ (pwd[_i2 - 1] == _letter) 23 | 24 | @timer(part=1) 25 | def part_1(self): 26 | return sum(self.is_valid_first(*rule, pwd) for rule, pwd in self.instructions) 27 | 28 | @timer(part=2) 29 | def part_2(self): 30 | return sum(self.is_valid_second(*rule, pwd) for rule, pwd in self.instructions) 31 | 32 | 33 | if __name__ == '__main__': 34 | with Timer('Password Philosphy'): 35 | Dec02().run_day() 36 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec04.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from python.src.y2017 import dec04 3 | 4 | 5 | class TestDec04(unittest.TestCase): 6 | def test_valid_passphrases(self): 7 | puzzle_input = [("aa bb cc dd ee", True), 8 | ("aa bb cc dd aa", False), 9 | ("aa bb cc dd aaa", True)] 10 | self.assertEqual(2, dec04.valid_passphrases([p[0] for p in puzzle_input])) 11 | for passphrase, valid in puzzle_input: 12 | self.assertEqual(valid, dec04.is_valid(passphrase)) 13 | 14 | def test_anagram_passphrases(self): 15 | puzzle_input = [("abcde fghij", True), 16 | ("abcde xyz ecdab", False), 17 | ("a ab abc abd abf abj", True), 18 | ("iiii oiii ooii oooi oooo", True), 19 | ("oiii ioii iioi iiio", False)] 20 | for passphrase, valid in puzzle_input: 21 | self.assertEqual(valid, dec04.is_valid(passphrase, anagram=True)) 22 | self.assertEqual(3, dec04.valid_passphrases([p[0] for p in puzzle_input], 23 | anagram=True)) 24 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2017 import dec09 4 | 5 | 6 | class TestStreamProcessing(unittest.TestCase): 7 | 8 | def test_group_score(self): 9 | test_cases = [ 10 | ("{}", 1), 11 | ("{{{}}}", 6), 12 | ("{{{},{},{{}}}}", 16), 13 | ("{,,,}", 1), 14 | ("{{},{},{},{}}", 9), 15 | ("{{},{},{},{}}", 9), 16 | ("{{},{},{},{}}", 3) 17 | ] 18 | for stream, expected in test_cases: 19 | tree = dec09.build_tree(stream) 20 | self.assertEqual(expected, tree.sum_tree()[0]) 21 | 22 | def test_garbage_count(self): 23 | test_cases = [ 24 | ("<>", 0), 25 | ("", 17), 26 | ("<<<<>", 3), 27 | ("<{!>}>", 2), 28 | ("", 0), 29 | (">", 0), 30 | ('<{o"i!a,<[i', 10) 31 | ] 32 | for stream, expected in test_cases: 33 | tree = dec09.build_tree(stream) 34 | self.assertEqual(expected, tree.sum_tree()[1]) 35 | -------------------------------------------------------------------------------- /python/test/y2017/test_dec01.py: -------------------------------------------------------------------------------- 1 | from python.src.y2017 import dec01 2 | 3 | import unittest 4 | 5 | 6 | class TestDec01(unittest.TestCase): 7 | def testCaptchaSum(self): 8 | cases = [(1122, 3), 9 | # 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the 10 | # second digit and the third digit (2) matches the fourth digit. 11 | (1111, 4), # 1111 produces 4 because each digit (all 1) matches the next. 12 | (1234, 0), # 1234 produces 0 because no digit matches the next. 13 | (91212129, 9) # 91212129 produces 9 because the only digit that matches 14 | # the next one is the last digit, 9. 15 | ] 16 | for number, hash in cases: 17 | self.assertEqual(hash, dec01.captcha_sum(number)) 18 | 19 | def testHalfwaySum(self): 20 | cases = [(1212, 6), 21 | (1221, 0), 22 | (123425, 4), 23 | (123123, 12), 24 | (12131415, 4)] 25 | for number, hash in cases: 26 | self.assertEqual(hash, dec01.captcha_sum(number, halfway_sum=True)) 27 | -------------------------------------------------------------------------------- /python/test/y2022/test_dec25.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2022.dec25 import Dec25 4 | 5 | 6 | class TestDec25(unittest.TestCase): 7 | data = """1=-0-2 8 | 12111 9 | 2=0= 10 | 21 11 | 2=01 12 | 111 13 | 20012 14 | 112 15 | 1=-1= 16 | 1-12 17 | 12 18 | 1= 19 | 122""".split('\n') 20 | 21 | snafu = [ 22 | (1, '1'), 23 | (2, '2'), 24 | (3, '1='), 25 | (4, '1-'), 26 | (5, '10'), 27 | (6, '11'), 28 | (7, '12'), 29 | (8, '2='), 30 | (9, '2-'), 31 | (10, '20'), 32 | (15, '1=0'), 33 | (20, '1-0'), 34 | (2022, '1=11-2'), 35 | (12345, '1-0---0'), 36 | (314159265, '1121-1110-1=0') 37 | ] 38 | 39 | def test_convert_from_snafu(self): 40 | for expected, snafu in self.snafu: 41 | self.assertEqual(expected, Dec25.convert_from_snafu(snafu)) 42 | 43 | def test_convert_to_snafu(self): 44 | for snafu, expected in self.snafu: 45 | self.assertEqual(expected, Dec25.convert_to_snafu(snafu)) 46 | 47 | def test_part_1(self): 48 | self.assertEqual('2=-1=0', Dec25(instructions=self.data).part_1()) 49 | -------------------------------------------------------------------------------- /python/test/y2015/test_dec08.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anna' 2 | 3 | import unittest 4 | 5 | from python.src.y2015 import dec08 6 | 7 | 8 | class Dec08Tests(unittest.TestCase): 9 | def testCharDecodeDiff_Example1(self): 10 | self.assertEqual(2, dec08.char_decode_diff(r'""')) 11 | 12 | def testCharDecodeDiff_Example2(self): 13 | self.assertEqual(2, dec08.char_decode_diff(r'"abc"')) 14 | 15 | def testCharDecodeDiff_Example3(self): 16 | self.assertEqual(3, dec08.char_decode_diff(r'"aaa\"aaa"')) 17 | 18 | def testCharDecodeDiff_Example4(self): 19 | self.assertEqual(5, dec08.char_decode_diff(r'"\x27"')) 20 | 21 | def testCharEncodeDiff_Example1(self): 22 | self.assertEqual(4, dec08.char_encode_diff(r'""')) 23 | 24 | def testCharEncodeDiff_Example2(self): 25 | self.assertEqual(4, dec08.char_encode_diff(r'"abc"')) 26 | 27 | def testCharEncodeDiff_Example3(self): 28 | self.assertEqual(6, dec08.char_encode_diff(r'"aaa\"aaa"')) 29 | 30 | def testCharEncodeDiff_Example4(self): 31 | self.assertEqual(5, dec08.char_encode_diff(r'"\x27"')) 32 | 33 | 34 | if __name__ == '__main__': 35 | unittest.main() 36 | -------------------------------------------------------------------------------- /python/src/y2017/dec12.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from python.src.y2017.common import DATA_DIR 4 | 5 | 6 | def parse_line(line): 7 | words = line.split('<->') 8 | return set([int(words[0])] + list(map(int, words[1].split(',')))) 9 | 10 | 11 | def find_groups(puzzle_input): 12 | instructions = {a: parse_line(x) for a, x in enumerate(puzzle_input)} 13 | programs = dict() 14 | 15 | while programs != instructions: 16 | for bidi_set in instructions.values(): 17 | for x in bidi_set: 18 | programs[x] = programs.get(x, set()).union(bidi_set) 19 | programs, instructions = instructions, programs 20 | return programs 21 | 22 | 23 | def count_groups(groups): 24 | s = set() 25 | for e in groups.values(): 26 | s.add(",".join(str(x) for x in sorted(e))) 27 | 28 | return len(s) 29 | 30 | 31 | def main(): 32 | with open(os.path.join(DATA_DIR, 'input.12.txt')) as fh: 33 | puzzle_input = fh.readlines() 34 | 35 | groups = find_groups(puzzle_input) 36 | 37 | print("Part 1:", len(groups.get(0))) 38 | print("Part 2:", count_groups(groups)) 39 | 40 | 41 | if __name__ == '__main__': 42 | main() 43 | -------------------------------------------------------------------------------- /python/test/y2021/test_dec20.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2021.dec20 import Dec20 4 | 5 | 6 | class TestDec20(unittest.TestCase): 7 | data = [ 8 | "..#.#..#####.#.#.#.###.##.....###.##.#..###.####.." 9 | "#####..#....#..#..##..###..######.###...####..#..#" 10 | "####..##..#.#####...##.#.#..#.##..#.#......#.###.#" 11 | "#####.###.####...#.##.##..#..#..#####.....#.#....#" 12 | "##..#.##......#.....#..#..#..##..#...##.######.###" 13 | "#.####.#.#...#.......#..#.#.#...####.##.#......#.." 14 | "#...##.#.##..#...##.#.##..###.#......#.#.......#.#" 15 | ".#.####.###.##...#.....####.#..#..#.##.#....##..#." 16 | "####....##...##..#...#......#.#.......#.......##.." 17 | "####..#...#.#.#...##..#.#..###..#####........#..##" 18 | "##......#..#", 19 | "", 20 | "#..#.", 21 | "#....", 22 | "##..#", 23 | "..#..", 24 | "..###"] 25 | 26 | def test_count_pixels_2(self): 27 | self.assertEqual(35, Dec20(instructions=self.data).part_1()) 28 | 29 | def test_count_pixels_50(self): 30 | self.assertEqual(3351, Dec20(instructions=self.data).part_2()) 31 | -------------------------------------------------------------------------------- /bash/2017/dec05.bash: -------------------------------------------------------------------------------- 1 | source ../common.bash 2 | 3 | function jump_offsets() { 4 | local exit=${#dec05_input[@]} 5 | local pc=0 6 | local steps=0 7 | while true; do 8 | if (( (pc >= exit) || (pc < 0) )); then 9 | echo "${steps}" 10 | return 11 | fi 12 | local current=${dec05_input[pc]} 13 | if [ "$1" == "part2" ]; then 14 | if (( current >= 3 )); then 15 | ((dec05_input[pc] -= 1)) 16 | else 17 | ((dec05_input[pc] += 1)) 18 | fi 19 | else 20 | ((dec05_input[pc] += 1)) 21 | fi 22 | ((pc += current)) 23 | ((steps += 1)) 24 | done 25 | } 26 | 27 | function dec05_test() { 28 | dec05_input=("0" "3" "0" "1" "-3") 29 | (( $(jump_offsets part1) == 5)) || return 1 30 | dec05_input=("0" "3" "0" "1" "-3") 31 | (( $(jump_offsets part2) == 10)) || return 1 32 | return 0 33 | } 34 | 35 | 36 | function dec05_main() { 37 | declare -a dec05_input 38 | read_file_to_arr 2017 5 dec05_input 39 | echo "Part 1: $(jump_offsets part1)" 40 | read_file_to_arr 2017 5 dec05_input 41 | echo "Part 1: $(jump_offsets part2)" 42 | } 43 | 44 | 45 | if [ "x$1" == "xtest" ]; then 46 | dec05_test 47 | else 48 | dec05_main 49 | fi -------------------------------------------------------------------------------- /python/test/y2019/test_dec12.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2019 import dec12 4 | 5 | 6 | class TestDec12(unittest.TestCase): 7 | def test_part_1_a(self): 8 | positions = ["", 9 | "", 10 | "", 11 | ""] 12 | d = dec12.Dec12(instructions=positions) 13 | d.simulate(steps=10) 14 | self.assertEqual(179, d.get_energy()) 15 | 16 | def test_part_2(self): 17 | positions = ["", 18 | "", 19 | "", 20 | ""] 21 | d = dec12.Dec12(instructions=positions) 22 | self.assertEqual(2772, d.part_2()) 23 | 24 | def test_part_2_large(self): 25 | positions = ["", 26 | "", 27 | "", 28 | ""] 29 | d = dec12.Dec12(instructions=positions) 30 | self.assertEqual(4686774924, d.part_2()) 31 | 32 | 33 | if __name__ == '__main__': 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /python/src/y2023/dec09.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec09(Day, year=2023, day=9): 5 | 6 | @staticmethod 7 | def parse_instructions(instructions): 8 | return [ 9 | list(map(int, line.split())) for line in instructions 10 | ] 11 | 12 | @staticmethod 13 | def extrapolate(numbers, forwards=True): 14 | if all(n == 0 for n in numbers): 15 | return 0 16 | else: 17 | next_numbers = [j - i for i, j in zip(numbers, numbers[1:])] 18 | if forwards: 19 | next_value = numbers[-1] + Dec09.extrapolate(next_numbers, forwards=forwards) 20 | else: 21 | next_value = numbers[0] - Dec09.extrapolate(next_numbers, forwards=forwards) 22 | return next_value 23 | 24 | @timer(part=1) 25 | def part_1(self): 26 | return sum(self.extrapolate(series) for series in self.instructions) 27 | 28 | @timer(part=2) 29 | def part_2(self): 30 | return sum(self.extrapolate(series, forwards=False) for series in self.instructions) 31 | 32 | 33 | if __name__ == '__main__': 34 | with Timer('Total'): 35 | Dec09().run_day() 36 | -------------------------------------------------------------------------------- /python/test/y2023/test_dec09.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2023.dec09 import Dec09 4 | 5 | 6 | class TestDec09(unittest.TestCase): 7 | data = [ 8 | "0 3 6 9 12 15", 9 | "1 3 6 10 15 21", 10 | "10 13 16 21 30 45" 11 | ] 12 | 13 | def test_part_1(self): 14 | self.assertEqual(114, Dec09(instructions=self.data).part_1()) 15 | 16 | def test_part_2(self): 17 | self.assertEqual(2, Dec09(instructions=self.data).part_2()) 18 | 19 | def test_extrapolate_part_1(self): 20 | cases = [ 21 | ([0, 3, 6, 9, 12, 15], 18), 22 | ([1, 3, 6, 10, 15, 21], 28), 23 | ([10, 13, 16, 21, 30, 45], 68) 24 | ] 25 | for case in cases: 26 | self.assertEqual(case[1], Dec09.extrapolate(case[0])) 27 | 28 | def test_extrapolate_part_2(self): 29 | cases = [ 30 | ([0, 3, 6, 9, 12, 15], -3), 31 | ([1, 3, 6, 10, 15, 21], 0), 32 | ([10, 13, 16, 21, 30, 45], 5) 33 | ] 34 | for case in cases: 35 | self.assertEqual(case[1], Dec09.extrapolate(case[0], forwards=False)) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /python/src/y2017/dec17.py: -------------------------------------------------------------------------------- 1 | class Spinlock(object): 2 | 3 | def __init__(self, step): 4 | self.values = [0] 5 | self.position = 0 6 | self.value = 0 7 | self.skip = step 8 | 9 | def next_value(self): 10 | self.value += 1 11 | return self.value 12 | 13 | def step(self): 14 | next_value = self.next_value() 15 | self.position = 1 + ((self.position + self.skip) % len(self.values)) 16 | self.values.insert(self.position, next_value) 17 | 18 | def part_1(self): 19 | for _ in range(2017): 20 | self.step() 21 | return self.values[(self.position + 1) % len(self.values)] 22 | 23 | def part_2(self): 24 | # 0 is always first, keep track of what's changed behind it. 25 | after_0 = self.values[1] 26 | pos = self.position 27 | for x in range(2017 + 1, 50000000): 28 | pos = 1 + ((pos + self.skip) % x) 29 | if pos == 1: 30 | after_0 = x 31 | return after_0 32 | 33 | 34 | def main(): 35 | s = Spinlock(344) 36 | print("Part 1:", s.part_1()) 37 | print("Part 2:", s.part_2()) 38 | 39 | 40 | if __name__ == '__main__': 41 | main() 42 | -------------------------------------------------------------------------------- /python/test/y2016/test_dec07.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import unittest 3 | 4 | from python.src.y2016 import dec07 5 | 6 | __author__ = 'anna' 7 | 8 | 9 | class Dec07Tests(unittest.TestCase): 10 | def setUp(self): 11 | pass 12 | 13 | def test_supports_tls_1(self): 14 | self.assertTrue(dec07.ipv7_supports_tls('abba[mnop]qrst')) 15 | 16 | def test_supports_tls_2(self): 17 | self.assertFalse(dec07.ipv7_supports_tls('abcd[bddb]xyyx')) 18 | 19 | def test_supports_tls_3(self): 20 | self.assertFalse(dec07.ipv7_supports_tls('aaaa[qwer]tyui')) 21 | 22 | def test_supports_tls_4(self): 23 | self.assertTrue(dec07.ipv7_supports_tls('ioxxoj[asdfgh]zxcvbn')) 24 | 25 | def test_supports_ssl_1(self): 26 | self.assertTrue(dec07.ipv7_supports_ssl('aba[bab]xyz')) 27 | 28 | def test_supports_ssl_2(self): 29 | self.assertFalse(dec07.ipv7_supports_ssl('xyx[xyx]xyx')) 30 | 31 | def test_supports_ssl_3(self): 32 | self.assertTrue(dec07.ipv7_supports_ssl('aaa[kek]eke')) 33 | 34 | def test_supports_ssl_4(self): 35 | self.assertTrue(dec07.ipv7_supports_ssl('zazbz[bzb]cdb')) 36 | 37 | 38 | if __name__ == '__main__': 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /python/test/y2020/test_dec10.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from python.src.y2020.dec10 import Dec10 4 | 5 | 6 | class TestDec10(unittest.TestCase): 7 | data_1 = [ 8 | "16", 9 | "10", 10 | "15", 11 | "5", 12 | "1", 13 | "11", 14 | "7", 15 | "19", 16 | "6", 17 | "12", 18 | "4", 19 | ] 20 | 21 | data_2 = """28 22 | 33 23 | 18 24 | 42 25 | 31 26 | 14 27 | 46 28 | 20 29 | 48 30 | 47 31 | 24 32 | 23 33 | 49 34 | 45 35 | 19 36 | 38 37 | 39 38 | 11 39 | 1 40 | 32 41 | 25 42 | 35 43 | 8 44 | 17 45 | 7 46 | 9 47 | 4 48 | 2 49 | 34 50 | 10 51 | 3""".splitlines() 52 | 53 | def test_part_1_a(self): 54 | self.assertEqual(7 * 5, Dec10(instructions=self.data_1).part_1()) 55 | 56 | def test_part_1_b(self): 57 | self.assertEqual(22 * 10, Dec10(instructions=self.data_2).part_1()) 58 | 59 | def test_part_2_a(self): 60 | self.assertEqual(8, Dec10(instructions=self.data_1).part_2()) 61 | 62 | def test_part_2_b(self): 63 | self.assertEqual(19208, Dec10(instructions=self.data_2).part_2()) 64 | -------------------------------------------------------------------------------- /python/src/y2022/dec25.py: -------------------------------------------------------------------------------- 1 | from python.src.common import Day, timer, Timer 2 | 3 | 4 | class Dec25(Day, year=2022, day=25): 5 | 6 | @timer(part=1) 7 | def part_1(self): 8 | number = sum(self.convert_from_snafu(i) for i in self.instructions) 9 | return self.convert_to_snafu(number) 10 | 11 | @staticmethod 12 | def convert_from_snafu(snafu): 13 | lookup = { 14 | '1': 1, 15 | '2': 2, 16 | '0': 0, 17 | '-': -1, 18 | '=': -2 19 | } 20 | return sum(lookup[d] * 5 ** i 21 | for i, d in enumerate(snafu[::-1])) 22 | 23 | @staticmethod 24 | def convert_to_snafu(number): 25 | digits = [] 26 | lookup = { 27 | -2: '=', 28 | -1: '-', 29 | 0: '0', 30 | 1: '1', 31 | 2: '2' 32 | } 33 | while number: 34 | number, rest = divmod(number, 5) 35 | if rest > 2: 36 | rest -= 5 37 | number += 1 38 | digits.append(lookup[rest]) 39 | return ''.join(digits[::-1]) 40 | 41 | 42 | if __name__ == '__main__': 43 | with Timer('Total'): 44 | Dec25().part_1() 45 | --------------------------------------------------------------------------------