Coverage for src\aoc2022\days\day1.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.0.0, created at 2022-12-23 05:19 +0000

1from typing import List, Tuple 

2 

3from src.aoc2022.utils import get_raw_data 

4 

5 

6def parse_data_to_array(raw_data: str) -> List[List[int]]: 

7 """ 

8 Parses the raw text input into lists of numerical values, 

9 with one list per grouping of lines 

10 

11 :param raw_data: Raw text data to be parsed 

12 :type raw_data: str 

13 

14 :return: Parsed integer values 

15 :rtype: List[List[int]] 

16 """ 

17 raw_data = raw_data.replace(" ", "").strip() 

18 line_groups = [group for group in raw_data.split("\n\n") if len(group) > 0] 

19 

20 parsed_data = [[int(e) for e in g.split("\n")] for g in line_groups] 

21 

22 return parsed_data 

23 

24 

25def get_largest_group_sum(groups: List[List[int]]) -> Tuple[int, int]: 

26 """Finds the group with the largest sum 

27 

28 :param groups: A list of groups, where each group is a list of integers 

29 :type groups: List[List[int]] 

30 

31 :return: Index of the group with the largest sum and corresponding sum 

32 :rtype: Tuple[int, int] 

33 """ 

34 

35 max_idx = 0 

36 max_value = -1 # smallest since all integers are guaranteed positive 

37 for idx, group in enumerate(groups): 

38 sum_ = sum(group) 

39 if sum_ > max_value: 

40 max_idx = idx 

41 max_value = sum_ 

42 

43 return max_idx, max_value 

44 

45 

46def get_3_largest_group_sum(groups: List[List[int]]) -> List[Tuple[int, int]]: 

47 """Finds the 3 top group with the largests sums 

48 

49 :param groups: A list of groups, where each group is a list of integers 

50 :type groups: List[List[int]] 

51 

52 :return: Index of the 3 groups with the largests sums + corresponding sums 

53 :rtype: List[Tuple[int, int]] 

54 """ 

55 

56 top_val = -1 # smallest since all integers are guaranteed positive 

57 top_idx = 0 

58 

59 mid_val = -1 # smallest since all integers are guaranteed positive 

60 mid_idx = 0 

61 

62 bot_val = -1 # smallest since all integers are guaranteed positive 

63 bot_idx = 0 

64 

65 for idx, group in enumerate(groups): 

66 sum_ = sum(group) 

67 if sum_ <= bot_val: 

68 pass 

69 elif (sum_ > bot_val) and (sum_ <= mid_val): 

70 bot_val = sum_ 

71 bot_idx = idx 

72 elif (sum_ > mid_val) and (sum_ <= top_val): 

73 bot_val = mid_val 

74 bot_idx = mid_idx 

75 

76 mid_val = sum_ 

77 mid_idx = idx 

78 else: 

79 bot_val = mid_val 

80 bot_idx = mid_idx 

81 

82 mid_val = top_val 

83 mid_idx = top_idx 

84 

85 top_val = sum_ 

86 top_idx = idx 

87 

88 return [(bot_idx, bot_val), (mid_idx, mid_val), (top_idx, top_val)] 

89 

90 

91if __name__ == "__main__": # pragma: no cover 

92 data = get_raw_data(file_path="./src/aoc2022/data/day1.txt") 

93 parsed_data = parse_data_to_array(data) 

94 idx, sum_ = get_largest_group_sum(parsed_data) 

95 print( 

96 f"The elf carrying most calories is #{idx+1}" 

97 f" with a total of {sum_} calories" 

98 ) 

99 

100 top_groups = get_3_largest_group_sum(parsed_data) 

101 top_calories = sum([t[1] for t in top_groups]) 

102 print(f"The top 3 elves are carrying {top_calories} calories")