2017 - Day 2 (Corruption Checksum)

View puzzle description on AdventOfCode.com
Part 1
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
#!/usr/bin/env python3

#Advent of Code
#2017 Day 2, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    checksum = 0

    for row in puzzle_input:
        temp = [int(x) for x in row.split(" ") if x != ""]
        checksum += max(temp) - min(temp)

    return checksum

def main():
    puzzle_input = util.read.as_lines()

    checksum = solve(puzzle_input)

    print("The checksum of the spreadsheet is " + str(checksum) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve(["5 1 9 5", "7 5 3", "2 4 6 8"]), 18)

if __name__ == "__main__":
    run(main)
Part 2
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
#!/usr/bin/env python3

#Advent of Code
#2017 Day 2, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    checksum = 0

    for row in puzzle_input:
        temp = sorted([int(x) for x in row.split() if x != ""], reverse=True)
        found = False
        for i in range(len(temp)):
            for j in range(i + 1, len(temp)):
                if temp[i] % temp[j] == 0:
                    checksum += int(temp[i] / temp[j])
                    found = True
                    break
            if found:
                break

    return checksum    

def main():
    puzzle_input = util.read.as_lines()

    checksum = solve(puzzle_input)

    print("The checksum of the spreadsheet is " + str(checksum) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve(["5 9 2 8", "9 4 7 3", "3 8 6 5"]), 9)

if __name__ == "__main__":
    run(main)
Return to solution index