-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc_day1.py
More file actions
112 lines (89 loc) · 3.1 KB
/
Copy pathaoc_day1.py
File metadata and controls
112 lines (89 loc) · 3.1 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# aoc_day1.py
# https://adventofcode.com/2025/day/1
from colorama import Fore, Style, init
init(autoreset=True)
C_INPUT = Fore.CYAN
C_POS = Fore.GREEN
C_X = Fore.YELLOW
C_GREY = Fore.LIGHTBLACK_EX
C_BLACK = Fore.BLACK
C_CYAN = Fore.LIGHTCYAN_EX
C_BLUE = Fore.LIGHTBLUE_EX
C_MAGENTA = Fore.LIGHTMAGENTA_EX
C_HEAD = Fore.MAGENTA
C_RST = Style.RESET_ALL
START_POS = 50
MIN_POS = 0
MAX_POS = 99
DIAL_SIZE = MAX_POS - MIN_POS + 1 # 100
def day1():
instructions: list[tuple[str, int]] = []
with open("day1_input.txt", "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
direction = line[0] # "L" or "R"
value = int(line[1:]) # rest of the string
instructions.append((direction, value))
print(f"Instructions ({len(instructions)}) loaded.")
day1_task1(instructions)
day1_task2(instructions)
def day1_task1(instructions: list[tuple[str, int]] = []):
print("\n##### Day 1 - Task 1 #####")
zero_counter = 0
cur_pos = START_POS
# print(f"Starting pos:", cur_pos)
for direction, val in instructions:
# print("Input:", direction, val)
if direction == "L":
# move left (backwards)
cur_pos = ((cur_pos - val - MIN_POS) % DIAL_SIZE) + MIN_POS
elif direction == "R":
# move right (forwards)
cur_pos = ((cur_pos + val - MIN_POS) % DIAL_SIZE) + MIN_POS
else:
raise ValueError(f"Unexpected direction {direction!r}")
# print("Now at:", cur_pos)
if cur_pos == 0:
zero_counter += 1
print("Times on 0:", zero_counter)
def step_and_count(cur_pos: int, direction: str, val: int) -> tuple[int, int]:
# +val for R, -val for L
delta = val if direction == "R" else -val
# how many times we cross/hit 0 during this move
if delta > 0:
# moving right: positions p+1 .. p+delta
crosses = (cur_pos + delta) // DIAL_SIZE
elif delta < 0:
# moving left: mirror the dial and reuse the "right" logic
d = -delta
mirror_pos = (DIAL_SIZE - cur_pos) % DIAL_SIZE
crosses = (mirror_pos + d) // DIAL_SIZE
else:
crosses = 0
# new wrapped position
new_pos = (cur_pos + delta) % DIAL_SIZE
return crosses, new_pos
def day1_task2(instructions: list[tuple[str, int]]):
print("\n##### Day 1 - Task 2 #####")
zero_counter = 0
cur_pos = START_POS
for direction, val in instructions:
crosses, cur_pos = step_and_count(cur_pos, direction, val)
zero_counter += crosses
# debug if you want:
color = (
C_GREY if crosses == 0 else
C_CYAN if crosses == 1 else
C_MAGENTA
)
print(f"{C_BLACK}Input:{C_RST} {direction} {val}")
print(f"{C_POS}Current val {cur_pos}{C_RST} {C_X}(+0 {color}x{crosses}){C_RST}")
print(f"{C_HEAD}Times passing 0: {zero_counter}{C_RST}")
if __name__ == "__main__":
try:
day1()
except Exception as e:
print(f"Fatal error: {e}")
exit(-1)