Post-christmas cleaning.
This commit is contained in:
parent
f291b0aa3f
commit
10f67e6bfd
19
2022/day1.py
19
2022/day1.py
@ -2,19 +2,8 @@
|
|||||||
|
|
||||||
import sys
|
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
|
print(f"answer 1 is {values[-1]}")
|
||||||
# of values to accumulate
|
print(f"answer 2 is {sum(values[-3:])}")
|
||||||
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:])}")
|
|
||||||
|
26
2022/day2.py
26
2022/day2.py
@ -2,19 +2,6 @@
|
|||||||
|
|
||||||
import sys
|
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:
|
def score_1(ux: int, vx: int) -> int:
|
||||||
# here ux and vx are both moves: 0 = rock, 1 = paper, 2 = scissor
|
# 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
|
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
|
# part 1 - 13526
|
||||||
print(f"score 1 is {sum(score_1(*v) for v in values)}")
|
print(f"score 1 is {sum(score_1(*v) for v in values)}")
|
||||||
|
|
||||||
|
@ -46,6 +46,7 @@ SIZE = np.gcd(*board.shape)
|
|||||||
|
|
||||||
# TODO: deduce this from the actual cube...
|
# TODO: deduce this from the actual cube...
|
||||||
faces_wrap: dict[int, dict[str, Callable[[int, int], tuple[int, int, str]]]]
|
faces_wrap: dict[int, dict[str, Callable[[int, int], tuple[int, int, str]]]]
|
||||||
|
|
||||||
if board.shape == (12, 16): # example
|
if board.shape == (12, 16): # example
|
||||||
faces_wrap = {
|
faces_wrap = {
|
||||||
1: {
|
1: {
|
||||||
|
@ -20,6 +20,6 @@ n_per_group = 3
|
|||||||
part2 = sum(
|
part2 = sum(
|
||||||
priorities[c]
|
priorities[c]
|
||||||
for i in range(0, len(lines), n_per_group)
|
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}")
|
print(f"score 2 is {part2}")
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
data = sys.stdin.read().strip()
|
|
||||||
|
|
||||||
|
|
||||||
def index_of_first_n_differents(data: str, n: int) -> int:
|
def index_of_first_n_differents(data: str, n: int) -> int:
|
||||||
for i in range(len(data)):
|
for i in range(len(data)):
|
||||||
@ -12,5 +10,8 @@ def index_of_first_n_differents(data: str, n: int) -> int:
|
|||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
data = sys.stdin.read().strip()
|
||||||
|
|
||||||
|
|
||||||
print(f"answer 1 is {index_of_first_n_differents(data, 4)}")
|
print(f"answer 1 is {index_of_first_n_differents(data, 4)}")
|
||||||
print(f"answer 2 is {index_of_first_n_differents(data, 14)}")
|
print(f"answer 2 is {index_of_first_n_differents(data, 14)}")
|
||||||
|
Loading…
Reference in New Issue
Block a user