blob: ec1c2f27dbbbcb3a6a15c2b01a1b43dcbc37869f (
plain)
1
2
3
4
5
6
7
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# -*- coding: utf-8 -*-
from abc import ABC
from typing import Iterator
from aoc import BaseAssignment, I
class Assignment(BaseAssignment, ABC):
def parse_item(self, item: str) -> Iterator[I]:
[rotation, *places] = item
places = int("".join(places))
yield -places if rotation == "L" else places
class AssignmentOne(Assignment):
example_result = 3
def run(self, input: Iterator[int]) -> int:
seen_0 = 0
position = 50
for rotation in input:
position = (position + rotation) % 100
if position == 0:
seen_0 += 1
return seen_0
class AssignmentTwo(Assignment):
example_result = 6
@staticmethod
def calculate_new_position(position, rotation) -> tuple[int, int]:
abs_rotation = abs(rotation)
seen_0 = abs(abs_rotation // 100)
if rotation > 0:
remainder_rotation = rotation - (seen_0 * 100)
elif rotation < 0:
remainder_rotation = rotation + (seen_0 * 100)
else:
remainder_rotation = 0
new_position = (position + remainder_rotation) % 100
if remainder_rotation != 0 and new_position == 0:
seen_0 += 1
elif position > 0 and rotation > 0 and position > new_position:
seen_0 += 1
elif position > 0 and rotation < 0 and position < new_position:
seen_0 += 1
return new_position, seen_0
def run(self, input: Iterator[int]) -> int:
seen_0_total = 0
position = 50
for rotation in input:
position, seen_0 = self.calculate_new_position(position, rotation)
seen_0_total += seen_0
return seen_0_total
|