diff --git a/day1/input1.txt b/day1/input.txt similarity index 100% rename from day1/input1.txt rename to day1/input.txt diff --git a/day1/part1.py b/day1/part1.py index 10f93be..7f770a5 100644 --- a/day1/part1.py +++ b/day1/part1.py @@ -1,4 +1,4 @@ -file = "input1.txt" +file = "input.txt" def main(): current = 50 diff --git a/day1/part2.py b/day1/part2.py new file mode 100644 index 0000000..c991c1c --- /dev/null +++ b/day1/part2.py @@ -0,0 +1,24 @@ +file = "input.txt" + +def main(): + current, count = 50, 0 + print(f"{current} count:{count}") + for line in open(file): + direction, magnitude = line[:1], int(line[1:]) + scalar = 1 if direction == "R" else -1 + pre = current + current += scalar * magnitude + if current <= 0 and pre != 0: count += 1 + count += abs(current) // 100 + current = normalize(current) + print(f"{direction} {magnitude} => {current} count:{count}") + +def normalize(x): + if 0 <= x <= 99: return x + if x <= 0: + return normalize(100 + x) + else: + return normalize(x - 100) + +if __name__ == "__main__": + main() diff --git a/day1/test.txt b/day1/test.txt new file mode 100644 index 0000000..53287c7 --- /dev/null +++ b/day1/test.txt @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82