return False def solve_puzzle(arr): ind = [0, 0] if (not has_empty(arr, ind)): return True row = ind[0] col = ind[1] for num in range(1, 10): if (check_safe(arr, row, col, num)): arr[row][col] = num if (solve_puzzle(arr)): return True arr[row][col] = '.' return False if __name__ == "__main__": grid, x = puzzles.get_puzzle() print("\nPuzzle #{}:\n".format(x + 1)) display_grid(grid) print("\nSolution :\n") start = time.time() if (solve_puzzle(grid)): display_grid(grid) print("\n(Time taken to solve: {} secs)".format(time.time() - start)) else: print("No solution exists")
"""Wed, 2 Dec""" from puzzles import get_puzzle puzzle = get_puzzle(2) def test(): example = [ "1-3 a: abcde", "1-3 b: cdefg", "2-9 c: ccccccccc", ] assert one(example) == 2 assert two(example) == 1 def one(puzzle: list): valid = 0 for line in puzzle: limits, pattern, password = line.split() char = pattern[0] lower, upper = map(int, limits.split("-")) num = 0 for c in password: if c == char: num += 1 if lower <= num and num <= upper: valid += 1
"""Thu, 3 Dec""" from puzzles import get_puzzle puzzle = get_puzzle(3) def test(): example = [ "..##.......", "#...#...#..", ".#....#..#.", "..#.#...#.#", ".#...##..#.", "..#.##.....", ".#.#.#....#", ".#........#", "#.##...#...", "#...##....#", ".#..#...#.#", ] assert one(example) == 7 assert two(example) == 336 def one(puzzle: list, right=3, down=1): n_trees = 0 i = 0 for line in puzzle[::down]: if line[i] == "#":
"""Sun, 6 Dec""" from puzzles import get_puzzle puzzle = get_puzzle(6) def test(): example = [ "abc", "", "a", "b", "c", "", "ab", "ac", "", "a", "a", "a", "a", "", "b", ] assert one(example) == 11 assert len(get_groups(example)) == 5 assert two(example) == 6
"""Thu, 10 Dec""" from puzzles import get_puzzle PUZZLE = get_puzzle(10) EXAMPLE = [ "16", "10", "15", "5", "1", "11", "7", "19", "6", "12", "4", ] EXAMPLE_LARGE = [ "28", "33", "18", "42", "31", "14", "46", "20", "48", "47", "24", "23", "49",
"""Sat, 5 Dec""" from puzzles import get_puzzle puzzle = get_puzzle(5) def test(): examples = [ "FBFBBFFRLR", "BFFFBBFRRR", "FFFBBBFRRR", "BBFFBBFRLL", ] r, c = get_row_column(examples[0]) assert r == 44 assert c == 5 assert id_from_seat(r, c) == 357 assert one(examples) == 820 def id_from_seat(row: int, column: int) -> int: return int(row * 8 + column) def get_row_column(line: str, max_row: int = 127, max_col: int = 7) -> (int, int): min_row = 0 min_col = 0 for c in line:
"""Mon, 7 Dec""" from puzzles import get_puzzle from pprint import pprint PUZZLE = get_puzzle(7) EXAMPLE = [ "light red bags contain 1 bright white bag, 2 muted yellow bags.", "dark orange bags contain 3 bright white bags, 4 muted yellow bags.", "bright white bags contain 1 shiny gold bag.", "muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.", "shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.", "dark olive bags contain 3 faded blue bags, 4 dotted black bags.", "vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.", "faded blue bags contain no other bags.", "dotted black bags contain no other bags.", ] def test_one(): """Test Part One""" assert one(EXAMPLE) == 4 def test_two(): """Test Part One""" assert two(EXAMPLE) == False def one(puzzle: list, my_bag='shiny gold'): """Part One"""
"""Fri, 4 Dec""" from puzzles import get_puzzle puzzle = get_puzzle(4) def test(): example = [ "ecl:gry pid:860033327 eyr:2020 hcl:#fffffd", "byr:1937 iyr:2017 cid:147 hgt:183cm", "", "iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884", "hcl:#cfa07d byr:1929", "", "hcl:#ae17e1 iyr:2013", "eyr:2024", "ecl:brn pid:760753108 byr:1931", "hgt:179cm", "", "hcl:#cfa07d eyr:2025 pid:166559648", "iyr:2011 ecl:brn hgt:59in", ] assert one(example) == 2 # --- part two --- invalid_examples = [ "eyr:1972 cid:100", "hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926", "", "iyr:2019",
"""Tue, 22 Dec""" from puzzles import get_puzzle PUZZLE = get_puzzle(22) EXAMPLE = [ "Player 1:", "9", "2", "6", "3", "1", "", "Player 2:", "5", "8", "4", "7", "10", ] def test_one(): """Test Part One""" assert one(EXAMPLE) == 306 def test_two(): """Test Part Two""" assert two(EXAMPLE) == False
"""Tue, 1 Dec""" from puzzles import get_puzzle puzzle = get_puzzle(1) puzzle = [int(p.strip()) for p in puzzle] def test(): example = [1721, 979, 366, 299, 675, 1456] assert one(example) == 514579 assert two(example) == 241861950 def one(puzzle: list): puzzle = sorted(puzzle) for i, a in enumerate(puzzle): for b in puzzle[i:]: if a + b == 2020: return a * b def two(puzzle: list): puzzle = sorted(puzzle) for i, a in enumerate(puzzle): for j, b in enumerate(puzzle, i): for c in puzzle[j:]: if a + b + c == 2020: return a * b * c