diff options
author | Prefetch | 2022-12-31 22:21:39 +0100 |
---|---|---|
committer | Prefetch | 2022-12-31 22:21:39 +0100 |
commit | 68615a9ad2c942254135cffb00cf25a84a3b1356 (patch) | |
tree | 1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /14/main.py |
Initial commit
Diffstat (limited to '14/main.py')
-rwxr-xr-x | 14/main.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/14/main.py b/14/main.py new file mode 100755 index 0000000..4af4940 --- /dev/null +++ b/14/main.py @@ -0,0 +1,75 @@ +#!/usr/bin/python + + + +class Reindeer: + pass + + + +def parse_input(lines): + result = [] + for line in lines: + words = line.split() + + r = Reindeer() + r.name = words[0] + r.dist = 0 + r.score = 0 + r.state = "moving" + r.speed = int(words[ 3]) + r.tmove = int(words[ 6]) + r.trest = int(words[13]) + r.timer = 0 + + result.append(r) + + return result + + + +def solve_partn(partn, lines, tmax): + contestants = parse_input(lines) + + for t in range(tmax): + for r in contestants: + if r.state == "moving": + r.dist += r.speed + r.timer += 1 + if r.timer >= r.tmove: + r.state = "resting" + r.timer = 0 + else: # r.state == "resting" + r.timer += 1 + if r.timer >= r.trest: + r.state = "moving" + r.timer = 0 + + # Award 1 point to leader (and to others in case of a tie) + # Only for part 2, but doesn't hurt part 1, so not disabled. + leader = max(contestants, key = lambda r: r.dist) + for r in contestants: + if r.dist == leader.dist: + r.score += 1 + + if partn == 1: + winner = max(contestants, key = lambda r: r.dist) + return winner.dist + else: # partn == 2 + winner = max(contestants, key = lambda r: r.score) + return winner.score + + + +def main(): + # Read reindeer stats from input text file + with open("input.txt", "r") as f: + lines = f.read().splitlines() + + print("Part 1 solution:", solve_partn(1, lines, 2503)) # 2640 for me + print("Part 2 solution:", solve_partn(2, lines, 2503)) # 1102 for me + + + +if __name__ == "__main__": + main() |