Skip to content

Commit

Permalink
2021: python: general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mreishus committed Dec 28, 2021
1 parent 3e65405 commit 6a4cc23
Show file tree
Hide file tree
Showing 22 changed files with 240 additions and 114 deletions.
4 changes: 1 addition & 3 deletions 2021/python2021/aoc/day01.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ def increase_count(data: List[int], window: int) -> int:


class Day01:
""" AoC 2021 Day 01 """
"""AoC 2021 Day 01"""

@staticmethod
def part1(filename: str) -> int:
""" Given a filename, solve 2021 day 01 part 1 """
data = parse(filename)
return increase_count(data, 1)

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 01 part 2 """
data = parse(filename)
return increase_count(data, 3)
4 changes: 0 additions & 4 deletions 2021/python2021/aoc/day02.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ def parse_line(line):


class Day02:
""" AoC 2021 Day 02 """

@staticmethod
def part1(filename: str) -> int:
""" Given a filename, solve 2021 day 02 part 1 """
data = parse(filename)

loc = complex(0, 0)
Expand All @@ -37,7 +34,6 @@ def part1(filename: str) -> int:

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 02 part 2 """
data = parse(filename)

loc = complex(0, 0)
Expand Down
4 changes: 0 additions & 4 deletions 2021/python2021/aoc/day03.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ def bit_criteria_selection(data: List[str], most_common=True) -> int:


class Day03:
"""AoC 2021 Day 03"""

@staticmethod
def part1(filename: str) -> int:
"""Given a filename, solve 2021 day 03 part 1"""
data = parse(filename)

common = ""
Expand All @@ -60,7 +57,6 @@ def part1(filename: str) -> int:

@staticmethod
def part2(filename: str) -> int:
"""Given a filename, solve 2021 day 03 part 2"""
data = parse(filename)

o2 = bit_criteria_selection(data, True)
Expand Down
6 changes: 3 additions & 3 deletions 2021/python2021/aoc/day04.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def is_winner(self):


class Day04:
""" AoC 2021 Day 04 """
"""AoC 2021 Day 04"""

@staticmethod
def part1(filename: str) -> int:
""" Given a filename, solve 2021 day 04 part 1 """
"""Given a filename, solve 2021 day 04 part 1"""
(first, boards_raw) = parse(filename)

boards = [Board(board_raw) for board_raw in boards_raw]
Expand All @@ -96,7 +96,7 @@ def part1(filename: str) -> int:

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 04 part 2 """
"""Given a filename, solve 2021 day 04 part 2"""
(first, boards_raw) = parse(filename)

boards = [Board(board_raw) for board_raw in boards_raw]
Expand Down
13 changes: 5 additions & 8 deletions 2021/python2021/aoc/day13.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""
Advent Of Code 2021 Day 13
https://adventofcode.com/2021/day/8
https://adventofcode.com/2021/day/13
"""
import re
import numpy as np
Expand Down Expand Up @@ -59,11 +59,11 @@ def fold(grid, var, amt):


class Day13:
""" AoC 2021 Day 13 """
"""AoC 2021 Day 13"""

@staticmethod
def part1(filename: str) -> int:
""" Given a filename, solve 2021 day 13 part 1 """
"""Given a filename, solve 2021 day 13 part 1"""
# (You guessed 722.)
coords, instructs = parse(filename)

Expand All @@ -72,7 +72,6 @@ def part1(filename: str) -> int:
for x, y in coords:
max_x = max(x, max_x)
max_y = max(y, max_y)
print(f"{max_x} {max_y}")
if max_x == 1305:
max_x = 1310
if max_y == 893:
Expand All @@ -81,24 +80,21 @@ def part1(filename: str) -> int:
grid = np.zeros((max_y + 1, max_x + 1), dtype=int)
for x, y in coords:
grid[y][x] = 1
# print("")
# print(grid)
for var, amt in instructs:
grid = fold(grid, var, amt)
break
return np.count_nonzero(grid)

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 13 part 2 """
"""Given a filename, solve 2021 day 13 part 2"""
coords, instructs = parse(filename)

max_x = 0
max_y = 0
for x, y in coords:
max_x = max(x, max_x)
max_y = max(y, max_y)
print(f"{max_x} {max_y}")
if max_x == 1305:
max_x = 1310
if max_y == 893:
Expand All @@ -109,5 +105,6 @@ def part2(filename: str) -> int:
grid[y][x] = 1
for var, amt in instructs:
grid = fold(grid, var, amt)
np.set_printoptions(linewidth=150)
print(grid)
return "Look at grid :)"
8 changes: 4 additions & 4 deletions 2021/python2021/aoc/day14.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""
Advent Of Code 2021 Day 14
https://adventofcode.com/2021/day/8
https://adventofcode.com/2021/day/14
"""
from typing import Tuple, Dict
from collections import defaultdict
Expand Down Expand Up @@ -56,7 +56,7 @@ def expand(pairs, rules):


class Day14:
""" AoC 2021 Day 14 """
"""AoC 2021 Day 14"""

@staticmethod
def partX(filename: str, steps: int) -> int:
Expand All @@ -70,10 +70,10 @@ def partX(filename: str, steps: int) -> int:

@staticmethod
def part1(filename: str) -> int:
""" Given a filename, solve 2021 day 14 part 1 """
"""Given a filename, solve 2021 day 14 part 1"""
return Day14.partX(filename, 10)

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 14 part 2 """
"""Given a filename, solve 2021 day 14 part 2"""
return Day14.partX(filename, 40)
9 changes: 4 additions & 5 deletions 2021/python2021/aoc/day15.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python
"""
Advent Of Code 2021 Day 15
https://adventofcode.com/2021/day/8
https://adventofcode.com/2021/day/15
"""
import re
from collections import defaultdict
from queue import PriorityQueue

Expand Down Expand Up @@ -97,11 +96,11 @@ def possible_edges2(loc, grid, xs, ys):


class Day15:
""" AoC 2021 Day 15 """
"""AoC 2021 Day 15"""

@staticmethod
def part1(filename: str) -> int:
""" Given a filename, solve 2021 day 15 part 1 """
"""Given a filename, solve 2021 day 15 part 1"""
grid, xs, ys = parse(filename)

loc = (0, 0)
Expand Down Expand Up @@ -135,7 +134,7 @@ def part1(filename: str) -> int:

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 15 part 2 """
"""Given a filename, solve 2021 day 15 part 2"""
grid, xs, ys = parse(filename)

loc = (0, 0)
Expand Down
4 changes: 2 additions & 2 deletions 2021/python2021/aoc/day16.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""
Advent Of Code 2021 Day 16
https://adventofcode.com/2021/day/8
https://adventofcode.com/2021/day/16
"""
from itertools import islice
import math
Expand Down Expand Up @@ -100,7 +100,7 @@ def p2(all_bits):


class Day16:
""" AoC 2021 Day 16 """
"""AoC 2021 Day 16"""

@staticmethod
def part1(filename: str) -> int:
Expand Down
4 changes: 2 additions & 2 deletions 2021/python2021/aoc/day17.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""
Advent Of Code 2021 Day 17
https://adventofcode.com/2021/day/8
https://adventofcode.com/2021/day/17
"""
from typing import List
import re
Expand Down Expand Up @@ -57,7 +57,7 @@ def whatever(bound, xv, yv):


class Day17:
""" AoC 2021 Day 17 """
"""AoC 2021 Day 17"""

@staticmethod
@lru_cache(maxsize=None)
Expand Down
8 changes: 4 additions & 4 deletions 2021/python2021/aoc/day19.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""
Advent Of Code 2021 Day 19
https://adventofcode.com/2021/day/8
https://adventofcode.com/2021/day/19
"""
from typing import List
import re
Expand Down Expand Up @@ -36,7 +36,7 @@ class Orientation:


def all_orientations():
""" Returns 48 orientations instead of 24 """
"""Returns 48 orientations instead of 24"""
for swap in range(2):
for roll in range(3):
for fx in [False, True]:
Expand Down Expand Up @@ -129,7 +129,7 @@ def match(scan1, scan2):


class Day19:
""" AoC 2021 Day 19 """
"""AoC 2021 Day 19"""

@staticmethod
@lru_cache(maxsize=None)
Expand Down Expand Up @@ -189,7 +189,7 @@ def part1(filename: str) -> int:

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 19 part 2 """
"""Given a filename, solve 2021 day 19 part 2"""
size, solved, scanners = Day19.partX(filename)
max_dist = 0
for i in range(size):
Expand Down
7 changes: 3 additions & 4 deletions 2021/python2021/aoc/day21.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def wrap(num):


class Day21:
""" AoC 2021 Day 21 """
"""AoC 2021 Day 21"""

@staticmethod
def part1(filename: str) -> int:
""" Given a filename, solve 2021 day 21 part 1 """
"""Given a filename, solve 2021 day 21 part 1"""
data = [9, 3]
d = Die()

Expand All @@ -40,13 +40,12 @@ def part1(filename: str) -> int:
if scores[i] >= 1000:
break
i = (i + 1) % 2
print(scores)
j = (i + 1) % 2
return scores[j] * d.rolls

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 21 part 2 """
"""Given a filename, solve 2021 day 21 part 2"""
roll1 = [3, 4, 5, 4, 5, 6, 5, 6, 7]
roll2 = [x + 1 for x in roll1]
roll3 = [x + 2 for x in roll1]
Expand Down
34 changes: 17 additions & 17 deletions 2021/python2021/aoc/day23.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
https://adventofcode.com/2021/day/23
"""
from collections import namedtuple, defaultdict, deque
import math
import itertools
from heapq import heappush, heappop

Expand Down Expand Up @@ -142,7 +141,8 @@ def solve(self):
while not pq.empty():
(state, length) = pq.pop_task()
if len(pq.pq) % 1000 == 0:
print(len(pq.pq))
# print(len(pq.pq))
pass
# if len(pq.pq) > 40000:
# i = 0
# for cost, counter, state in pq.pq:
Expand Down Expand Up @@ -177,18 +177,18 @@ def solve(self):
for k, v in dist_to.items():
if is_winner(k):
# return v
print("Found winner:")
print(f"{v} {k}")
win_cost = v
actual_remaining_costs = {k: 0}
all_states = [k]
while k in edge_to:
all_states.append(k)
# print(edge_to[k])
k = edge_to[k]
actual_remaining_costs[k] = -1 * (dist_to[k] - win_cost)

self.animate(list(reversed(all_states)), actual_remaining_costs)
# print("Found winner:")
# print(f"{v} {k}")
# win_cost = v
# actual_remaining_costs = {k: 0}
# all_states = [k]
# while k in edge_to:
# all_states.append(k)
# # print(edge_to[k])
# k = edge_to[k]
# actual_remaining_costs[k] = -1 * (dist_to[k] - win_cost)

# self.animate(list(reversed(all_states)), actual_remaining_costs)
return v
return -1

Expand Down Expand Up @@ -454,17 +454,17 @@ def is_winner(state: State):


class Day23:
""" AoC 2021 Day 23 """
"""AoC 2021 Day 23"""

@staticmethod
def part1(filename: str) -> int:
""" Given a filename, solve 2021 day 23 part 1 """
"""Given a filename, solve 2021 day 23 part 1"""
m = Maze(filename)
return m.solve()

@staticmethod
def part2(filename: str) -> int:
""" Given a filename, solve 2021 day 23 part 2 """
"""Given a filename, solve 2021 day 23 part 2"""
return -1


Expand Down
Loading

0 comments on commit 6a4cc23

Please sign in to comment.