-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c441db3
commit cfa7e4e
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
104 240 147 246 123 175 372 71 116 230 260 118 202 270 277 292 | ||
740 755 135 205 429 822 844 90 828 115 440 805 526 91 519 373 | ||
1630 991 1471 1294 52 1566 50 1508 1367 1489 55 547 342 512 323 51 | ||
1356 178 1705 119 1609 1409 245 292 1434 694 405 1692 247 193 1482 1407 | ||
2235 3321 3647 212 1402 3711 3641 1287 2725 692 1235 3100 123 144 104 101 | ||
1306 1224 1238 186 751 734 1204 1275 366 149 1114 166 1118 239 153 943 | ||
132 1547 1564 512 2643 2376 2324 2159 1658 107 1604 145 2407 131 2073 1878 | ||
1845 91 1662 108 92 1706 1815 1797 1728 1150 1576 83 97 547 1267 261 | ||
78 558 419 435 565 107 638 173 93 580 338 52 633 256 377 73 | ||
1143 3516 4205 3523 148 401 3996 3588 300 1117 2915 1649 135 134 182 267 | ||
156 2760 1816 2442 2985 990 2598 1273 167 821 138 141 2761 2399 1330 1276 | ||
3746 3979 2989 161 4554 156 3359 173 3319 192 3707 264 762 2672 4423 2924 | ||
3098 4309 4971 5439 131 171 5544 595 154 571 4399 4294 160 6201 4329 5244 | ||
728 249 1728 305 2407 239 691 2241 2545 1543 55 2303 1020 753 193 1638 | ||
260 352 190 877 118 77 1065 1105 1085 1032 71 87 851 56 1161 667 | ||
1763 464 182 1932 1209 640 545 931 1979 197 1774 174 2074 1800 939 161 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
def get_checksum(spreadsheet): | ||
checksum = 0 | ||
for line in spreadsheet: | ||
checksum += max(line) - min(line) | ||
return checksum | ||
|
||
def find_evenly_divisible_values(spreadsheet_row): | ||
sorted_row = spreadsheet_row.copy() | ||
sorted_row.sort() | ||
max_value_index = len(sorted_row) - 1 | ||
while max_value_index>0: | ||
min_value_index = 0 | ||
while min_value_index < max_value_index: | ||
if sorted_row[max_value_index] % sorted_row[min_value_index] == 0: | ||
return (sorted_row[max_value_index], sorted_row[min_value_index]) | ||
min_value_index += 1 | ||
max_value_index -= 1 | ||
|
||
def get_checksum_using_evenly_divisible_values(spreadsheet): | ||
checksum = 0 | ||
for spreadsheet_row in spreadsheet: | ||
evenly_divisble_values = find_evenly_divisible_values(spreadsheet_row) | ||
checksum += evenly_divisble_values[0] // evenly_divisble_values[1] | ||
return checksum | ||
|
||
|
||
def solve_challenge_part_1(): | ||
print(get_checksum(get_puzzle_input())) | ||
|
||
def solve_challenge_part_2(): | ||
print(get_checksum_using_evenly_divisible_values(get_puzzle_input())) | ||
|
||
def get_puzzle_input(): | ||
puzzle_input_file = open('python/day2part1input.txt', 'r') | ||
puzzle_input = [] | ||
for line in puzzle_input_file.read().strip().split('\n'): | ||
puzzle_input.append([int(number) for number in line.split(' ') if number != '' ]) | ||
puzzle_input_file.close() | ||
return puzzle_input | ||
|
||
if __name__ == "__main__": | ||
print("=== Part 1 ===") | ||
solve_challenge_part_1() | ||
print("=== Part 2 ===") | ||
solve_challenge_part_2() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import unittest | ||
from . import day2solution as solution | ||
|
||
class TestDay2Part1Solution(unittest.TestCase): | ||
def test_solution_returns_correct_checksum(self): | ||
array_to_test = [[5, 1, 9, 5], | ||
[7, 5, 3], | ||
[2, 4, 6, 8]] | ||
self.assertEqual(solution.get_checksum(array_to_test), 18) | ||
|
||
class TestDay2Part2Solution(unittest.TestCase): | ||
def test_evenly_divisible_values_can_be_found(self): | ||
self.assertEqual(solution.find_evenly_divisible_values([5,9,2,8]), (8, 2)) | ||
self.assertEqual(solution.find_evenly_divisible_values([9,4,7,3]), (9, 3)) | ||
self.assertEqual(solution.find_evenly_divisible_values([3,8,6,5]), (6, 3)) | ||
|
||
def test_final_sum_of_values_is_calculated_correctly(self): | ||
array_to_test = [[5,9,2,8], | ||
[9,4,7,3], | ||
[3,8,6,5]] | ||
self.assertEqual(solution.get_checksum_using_evenly_divisible_values(array_to_test), 9) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |