day 1
This commit is contained in:
4036
2025/01/input.txt
Normal file
4036
2025/01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
81
2025/01/solution.py
Executable file
81
2025/01/solution.py
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import pathlib
|
||||
|
||||
def read_input():
|
||||
path = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else pathlib.Path("input.txt")
|
||||
return path.read_text().rstrip("\n")
|
||||
|
||||
def parse(data):
|
||||
return data.splitlines()
|
||||
|
||||
def part1(data):
|
||||
password = 0
|
||||
postion = 50
|
||||
|
||||
for rotation in data:
|
||||
direction = rotation[0]
|
||||
dist = int(rotation[1:])
|
||||
|
||||
if direction == "R":
|
||||
postion += dist
|
||||
else: # left case
|
||||
postion -= dist
|
||||
postion %= 100
|
||||
if postion == 0:
|
||||
password += 1
|
||||
|
||||
return password
|
||||
|
||||
def part2(data):
|
||||
password = 0
|
||||
pos = 50
|
||||
modulo = 100
|
||||
|
||||
for rotation in data:
|
||||
direction = rotation[0]
|
||||
num = int(rotation[1:])
|
||||
old_pos = pos
|
||||
|
||||
if direction == 'L':
|
||||
pos -= num
|
||||
else:
|
||||
pos += num
|
||||
|
||||
if old_pos != pos:
|
||||
old = old_pos
|
||||
new = pos
|
||||
if old < new:
|
||||
first = (old // modulo + 1) * modulo
|
||||
last = (new // modulo) * modulo
|
||||
if first <= new:
|
||||
crosses = (last - first) // modulo + 1
|
||||
else:
|
||||
crosses = 0
|
||||
else:
|
||||
if new % modulo == 0:
|
||||
first = new
|
||||
else:
|
||||
first = (new // modulo + 1) * modulo
|
||||
last = ((old - 1) // modulo) * modulo
|
||||
if first <= last:
|
||||
crosses = (last - first) // modulo + 1
|
||||
else:
|
||||
crosses = 0
|
||||
else:
|
||||
crosses = 0
|
||||
|
||||
password += crosses
|
||||
|
||||
return password
|
||||
|
||||
def main():
|
||||
raw = read_input()
|
||||
data = parse(raw)
|
||||
r1 = part1(data)
|
||||
r2 = part2(data)
|
||||
print(r1)
|
||||
print(r2)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user