forked from uchicago-cs/python-practice-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc2022-21.py
More file actions
76 lines (55 loc) · 1.88 KB
/
aoc2022-21.py
File metadata and controls
76 lines (55 loc) · 1.88 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
"""
Starter code for Advent of Code 2022 Day #21
You must implement functions part1 and part2
"""
import sys
import os
def part1(monkeys):
"""
Solves Part 1 (see problem statement for more details)
Parameter:
- monkeys: A dictionary mapping monkey names to a list of
strings representing what the monkey "shouts".
If the list contains a single string, it will represent
an integer. If it contains three strings, it represents
an arithmetic operation. For example:
{'root': ['pppw', '+', 'sjmn'],
'dbpl': ['5'],
'cczh': ['sllz', '+', 'lgvd']}
Returns an integer
"""
### Replace with your code
return None
def part2(monkeys):
"""
Solves Part 2 (see problem statement for more details)
Parameter:
- monkeys: Same as Part 1.
Returns an integer
"""
### Replace with your code
return None
############################################
### ###
### Do not modify the code below ###
### EXCEPT ###
### to comment/uncomment the calls ###
### to the functions above ###
### ###
############################################
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"USAGE: python3 {os.path.basename(sys.argv[0])} <INPUT FILE>")
sys.exit(1)
input_file = sys.argv[1]
if not os.path.exists(input_file):
print(f"ERROR: No such file: {input_file}")
sys.exit(1)
with open(input_file) as f:
lines = [line.split(": ") for line in f.read().split("\n")]
monkeys = {}
for monkey, message in lines:
monkeys[monkey] = message.split()
print(f"Part 1:", part1(monkeys))
# Uncomment the following line when you're ready to work on Part 2
#print(f"Part 2:", part2(monkeys))