Skip to content

Commit f0d6650

Browse files
author
Zoe Peterson
committed
Day 4 completed
1 parent a4da8e7 commit f0d6650

File tree

6 files changed

+2657
-0
lines changed

6 files changed

+2657
-0
lines changed

day3.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import re
2+
import numpy as np
3+
4+
def find_max_width_and_height(claims):
5+
max_width = 0
6+
max_height = 0
7+
for claim in claims:
8+
claim_list = re.split('@ |x|,|: |\n', claim)
9+
x_start = int(claim_list[1])
10+
y_start = int(claim_list[2])
11+
width = int(claim_list[3])
12+
height = int(claim_list[4])
13+
if ((x_start + width) > max_width):
14+
max_width = (x_start + width)
15+
if ((y_start + height) > max_height):
16+
max_height = (y_start + height)
17+
return max_width, max_height
18+
19+
def calculate_array_of_all_claims(claims, max_width, max_height):
20+
array_of_all_claims = np.zeros([max_height, max_width])
21+
22+
for claim in claims:
23+
claim_list = re.split('@ |x|,|: |\n', claim)
24+
x_start = int(claim_list[1])
25+
y_start = int(claim_list[2])
26+
width = int(claim_list[3])
27+
height = int(claim_list[4])
28+
for y in range(y_start, (y_start + height)):
29+
for x in range(x_start, (x_start + width)):
30+
array_of_all_claims[y][x] = array_of_all_claims[y][x] + 1
31+
return array_of_all_claims
32+
33+
def find_overlaps(array_of_all_claims):
34+
number_of_overlaps = 0
35+
for row in range(len(array_of_all_claims)):
36+
for column in range(len(array_of_all_claims[0])):
37+
if (array_of_all_claims[row][column] > 1):
38+
number_of_overlaps += 1
39+
return number_of_overlaps
40+
41+
def claim_that_does_not_overlap(array_of_all_claims):
42+
print(array_of_all_claims)
43+
44+
45+
file_object = open("test_input_day_3.txt", "r")
46+
claims = file_object.readlines()
47+
max_width, max_height = find_max_width_and_height(claims)
48+
array_of_all_claims = calculate_array_of_all_claims(claims, max_width, max_height)
49+
print(find_overlaps(array_of_all_claims))
50+
print(claim_that_does_not_overlap(array_of_all_claims))
51+
52+
file_object.close()

day4.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
file_object = open("input_day_4.txt", "r")
2+
entries = file_object.readlines()
3+
file_object.close()
4+
5+
#We need to put the entries in time order
6+
entries_list = []
7+
for entry in entries:
8+
entries_list.append(entry)
9+
entries_list.sort()
10+
11+
current_guard_id = 0
12+
time_fell_asleep = 0
13+
guard_sleep_amounts = {}
14+
current_sleepiest_guard = 0
15+
current_longest_time_slept = 0
16+
for entry in entries_list:
17+
split_entry = entry.strip().replace('[', "").split(']')
18+
#Case for "Guard #1871 begins shift"
19+
if split_entry[1][1] == "G":
20+
current_guard_id = split_entry[1].split()[1][1:]
21+
#Case for falls asleep
22+
elif split_entry[1].strip() == "falls asleep":
23+
time_fell_asleep = int(split_entry[0][-2:])
24+
#Case for wakes up
25+
else:
26+
time_slept = (int(split_entry[0][-2:]) - time_fell_asleep)
27+
if current_guard_id in guard_sleep_amounts.keys():
28+
guard_sleep_amounts[current_guard_id] += time_slept
29+
else:
30+
guard_sleep_amounts[current_guard_id] = time_slept
31+
#If adding this interval makes this guard the sleepiest so far, update
32+
if guard_sleep_amounts[current_guard_id] > current_longest_time_slept:
33+
current_longest_time_slept = guard_sleep_amounts[current_guard_id]
34+
current_sleepiest_guard = current_guard_id
35+
36+
sleepiest_guard = current_sleepiest_guard
37+
print("sleepiest guard", current_sleepiest_guard)
38+
39+
#Find the minute this guard slept the most
40+
times_guard_slept_during_this_minute = [0] * 60
41+
42+
current_guard_id = 0
43+
time_fell_asleep = 0
44+
for entry in entries_list:
45+
split_entry = entry.strip().replace('[', "").split(']')
46+
#Case for "Guard #1871 begins shift"
47+
if split_entry[1][1] == "G":
48+
current_guard_id = split_entry[1].split()[1][1:]
49+
#Case for falls asleep
50+
elif split_entry[1].strip() == "falls asleep":
51+
time_fell_asleep = int(split_entry[0][-2:])
52+
#Case for wakes up
53+
else:
54+
if current_guard_id == sleepiest_guard:
55+
for i in range(time_fell_asleep, int(split_entry[0][-2:])):
56+
times_guard_slept_during_this_minute[i] += 1
57+
58+
sleepiest_minute = times_guard_slept_during_this_minute.index(max(times_guard_slept_during_this_minute))
59+
print("sleepiest minute", sleepiest_minute)
60+
print("answer", int(sleepiest_guard) * sleepiest_minute)

day4_part2.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
file_object = open("input_day_4.txt", "r")
2+
entries = file_object.readlines()
3+
file_object.close()
4+
5+
#We need to put the entries in time order
6+
entries_list = []
7+
for entry in entries:
8+
entries_list.append(entry)
9+
entries_list.sort()
10+
11+
current_guard_id = 0
12+
time_fell_asleep = 0
13+
guard_sleep_amounts = {}
14+
current_sleepiest_guard = 0
15+
current_sleepiest_minute_for_one_guard = 0
16+
for entry in entries_list:
17+
split_entry = entry.strip().replace('[', "").split(']')
18+
#Case for "Guard #1871 begins shift"
19+
if split_entry[1][1] == "G":
20+
current_guard_id = split_entry[1].split()[1][1:]
21+
#Case for falls asleep
22+
elif split_entry[1].strip() == "falls asleep":
23+
time_fell_asleep = int(split_entry[0][-2:])
24+
#Case for wakes up
25+
else:
26+
if not current_guard_id in guard_sleep_amounts.keys():
27+
guard_sleep_amounts[current_guard_id] = [0] * 60
28+
29+
for i in range(time_fell_asleep, int(split_entry[0][-2:])):
30+
guard_sleep_amounts[current_guard_id][i] += 1
31+
if guard_sleep_amounts[current_guard_id][i] > current_sleepiest_minute_for_one_guard:
32+
current_sleepiest_minute_for_one_guard = guard_sleep_amounts[current_guard_id][i]
33+
current_sleepiest_guard = current_guard_id
34+
35+
sleepiest_guard = current_sleepiest_guard
36+
print("sleepiest guard", sleepiest_guard)
37+
sleepiest_minute = guard_sleep_amounts[sleepiest_guard].index(max(guard_sleep_amounts[sleepiest_guard]))
38+
print("sleepiest minute", sleepiest_minute)
39+
print("answer", int(sleepiest_guard) * sleepiest_minute)

0 commit comments

Comments
 (0)