diff --git a/2022/day1.py b/2022/day1.py index f29847d..47da67a 100644 --- a/2022/day1.py +++ b/2022/day1.py @@ -2,19 +2,8 @@ import sys -lines = sys.stdin.readlines() +blocks = sys.stdin.read().split("\n\n") +values = sorted(sum(map(int, block.split())) for block in blocks) -# we store the list of calories for each elf in values, and we use the last element -# of values to accumulate -values: list[int] = [0] -for line in lines: - if not line.strip(): - values = values + [0] - else: - values[-1] += int(line.strip()) - -# part 1 -print(f"answer 1 is {max(values)}") - -# part 2 -print(f"answer 2 is {sum(sorted(values)[-3:])}") +print(f"answer 1 is {values[-1]}") +print(f"answer 2 is {sum(values[-3:])}") diff --git a/2022/day2.py b/2022/day2.py index fc68605..0851812 100644 --- a/2022/day2.py +++ b/2022/day2.py @@ -2,19 +2,6 @@ import sys -lines = sys.stdin.readlines() - -# the solution relies on replacing rock / paper / scissor by values 0 / 1 / 2 and using -# modulo-3 arithmetic -# -# in modulo-3 arithmetic, the winning move is 1 + the opponent move (e.g., winning move -# if opponent plays 0 is 1, or 0 if opponent plays 2 (0 = (2 + 1 % 3))) -# - -# we read the lines in a Nx2 in array with value 0/1/2 instead of A/B/C or X/Y/Z for -# easier manipulation -values = [(ord(row[0]) - ord("A"), ord(row[2]) - ord("X")) for row in lines] - def score_1(ux: int, vx: int) -> int: # here ux and vx are both moves: 0 = rock, 1 = paper, 2 = scissor @@ -48,6 +35,19 @@ def score_2(ux: int, vx: int) -> int: return (ux + vx - 1) % 3 + 1 + vx * 3 +lines = sys.stdin.readlines() + +# the solution relies on replacing rock / paper / scissor by values 0 / 1 / 2 and using +# modulo-3 arithmetic +# +# in modulo-3 arithmetic, the winning move is 1 + the opponent move (e.g., winning move +# if opponent plays 0 is 1, or 0 if opponent plays 2 (0 = (2 + 1 % 3))) +# + +# we read the lines in a Nx2 in array with value 0/1/2 instead of A/B/C or X/Y/Z for +# easier manipulation +values = [(ord(row[0]) - ord("A"), ord(row[2]) - ord("X")) for row in lines] + # part 1 - 13526 print(f"score 1 is {sum(score_1(*v) for v in values)}") diff --git a/2022/day22.py b/2022/day22.py index 6a409ea..f43d477 100644 --- a/2022/day22.py +++ b/2022/day22.py @@ -46,6 +46,7 @@ SIZE = np.gcd(*board.shape) # TODO: deduce this from the actual cube... faces_wrap: dict[int, dict[str, Callable[[int, int], tuple[int, int, str]]]] + if board.shape == (12, 16): # example faces_wrap = { 1: { diff --git a/2022/day3.py b/2022/day3.py index 9391aa7..eda40a1 100644 --- a/2022/day3.py +++ b/2022/day3.py @@ -20,6 +20,6 @@ n_per_group = 3 part2 = sum( priorities[c] for i in range(0, len(lines), n_per_group) - for c in set.intersection(*map(set, (lines[i : i + n_per_group]))) + for c in set(lines[i]).intersection(*lines[i + 1 : i + n_per_group]) ) print(f"score 2 is {part2}") diff --git a/2022/day6.py b/2022/day6.py index 1074273..abc67ad 100644 --- a/2022/day6.py +++ b/2022/day6.py @@ -2,8 +2,6 @@ import sys -data = sys.stdin.read().strip() - def index_of_first_n_differents(data: str, n: int) -> int: for i in range(len(data)): @@ -12,5 +10,8 @@ def index_of_first_n_differents(data: str, n: int) -> int: return -1 +data = sys.stdin.read().strip() + + print(f"answer 1 is {index_of_first_n_differents(data, 4)}") print(f"answer 2 is {index_of_first_n_differents(data, 14)}")