Skip to content

Commit 8524ca6

Browse files
committed
Day 2, 2017 - Parts I & II
1 parent 94585ad commit 8524ca6

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

2017/day02.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# --- Day 2: Corruption Checksum ---
2+
#
3+
# As you walk through the door, a glowing humanoid shape yells in your direction.
4+
# "You there! Your state appears to be idle. Come help us repair the corruption
5+
# in this spreadsheet - if we take another millisecond, we'll have to display an
6+
# hourglass cursor!"
7+
#
8+
# The spreadsheet consists of rows of apparently-random numbers. To make sure
9+
# the recovery process is on the right track, they need you to calculate the
10+
# spreadsheet's checksum. For each row, determine the difference between the
11+
# largest value and the smallest value; the checksum is the sum of all of these
12+
# differences.
13+
#
14+
# For example, given the following spreadsheet:
15+
#
16+
# 5 1 9 5
17+
# 7 5 3
18+
# 2 4 6 8
19+
# The first row's largest and smallest values are 9 and 1, and their difference is 8.
20+
# The second row's largest and smallest values are 7 and 3, and their difference is 4.
21+
# The third row's difference is 6.
22+
# In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.
23+
#
24+
# What is the checksum for the spreadsheet in your puzzle input?
25+
# Answer: 41887
26+
#
27+
# --- Part Two ---
28+
#
29+
# "Great work; looks like we're on the right track after all.
30+
# Here's a star for your effort." However, the program seems a little worried.
31+
# Can programs be worried?
32+
#
33+
# "Based on what we're seeing, it looks like all the User wanted is some
34+
# information about the evenly divisible values in the spreadsheet.
35+
# Unfortunately, none of us are equipped for that kind of calculation -
36+
# most of us specialize in bitwise operations."
37+
#
38+
# It sounds like the goal is to find the only two numbers in each row where one
39+
# evenly divides the other - that is, where the result of the division
40+
# operation is a whole number. They would like you to find those numbers on
41+
# each line, divide them, and add up each line's result.
42+
#
43+
# For example, given the following spreadsheet:
44+
#
45+
# 5 9 2 8
46+
# 9 4 7 3
47+
# 3 8 6 5
48+
# In the first row, the only two numbers that evenly divide are 8 and 2;
49+
# the result of this division is 4.
50+
# In the second row, the two numbers are 9 and 3; the result is 3.
51+
# In the third row, the result is 2.
52+
# In this example, the sum of the results would be 4 + 3 + 2 = 9.
53+
54+
# What is the sum of each row's result in your puzzle input?
55+
# Answer: 226
56+
#
57+
# ------------------------------------------------------------------------------
58+
59+
import os
60+
61+
def checksum1(lines):
62+
return sum([min_max_diff(line) for line in lines])
63+
64+
def min_max_diff(line):
65+
return max(line) - min(line)
66+
67+
def checksum2(lines):
68+
return sum([mod(line) for line in lines])
69+
70+
def mod(line):
71+
for n in line:
72+
l2 = line.copy()
73+
l2.remove(n)
74+
for n2 in l2:
75+
if n % n2 == 0:
76+
return int(n / n2)
77+
78+
if __name__ == '__main__':
79+
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "inputs/day02.txt")
80+
f = open(file_path)
81+
data = f.read().split('\n')
82+
data = [[int(e) for e in line.split()] for line in data]
83+
print(checksum1(data))
84+
print(checksum2(data))

2017/inputs/day02.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
116 1259 1045 679 1334 157 277 1217 218 641 1089 136 247 1195 239 834
2+
269 1751 732 3016 260 6440 5773 4677 306 230 6928 7182 231 2942 2738 3617
3+
644 128 89 361 530 97 35 604 535 297 599 121 567 106 114 480
4+
105 408 120 363 430 102 137 283 123 258 19 101 181 477 463 279
5+
873 116 840 105 285 238 540 22 117 125 699 953 920 106 113 259
6+
3695 161 186 2188 3611 2802 157 2154 3394 145 2725 1327 3741 2493 3607 4041
7+
140 1401 110 119 112 1586 125 937 1469 1015 879 1798 122 1151 100 926
8+
2401 191 219 607 267 2362 932 2283 889 2567 2171 2409 1078 2247 2441 245
9+
928 1142 957 1155 922 1039 452 285 467 305 506 221 281 59 667 232
10+
3882 1698 170 5796 2557 173 1228 4630 174 3508 5629 4395 180 5100 2814 2247
11+
396 311 223 227 340 313 355 469 229 162 107 76 363 132 453 161
12+
627 1331 1143 1572 966 388 198 2068 201 239 176 1805 1506 1890 1980 1887
13+
3390 5336 1730 4072 5342 216 3823 85 5408 5774 247 5308 232 256 5214 787
14+
176 1694 1787 1586 3798 4243 157 4224 3603 2121 3733 851 2493 4136 148 153
15+
2432 4030 3397 4032 3952 2727 157 3284 3450 3229 4169 3471 4255 155 127 186
16+
919 615 335 816 138 97 881 790 855 89 451 789 423 108 95 116

2017/inputs/day02_test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
5 1 9 5
2+
7 5 3
3+
2 4 6 8

2017/inputs/day02_test2.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
5 9 2 8
2+
9 4 7 3
3+
3 8 6 5

0 commit comments

Comments
 (0)