2022 - Day 1 (Calorie Counting)

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3

#Advent of Code
#2022 Day 1, 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):
    elves = [0]
    for line in puzzle_input:
        if line == "":
            elves.append(0)
        else:
            elves[-1] += int(line)

    return max(elves)

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

    calories = solve(puzzle_input)

    print("The elf with the highest number of calories is carrying " + str(calories) + " calories.")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["1000",
                                       "2000",
                                       "3000",
                                       "",
                                       "4000",
                                       "",
                                       "5000",
                                       "6000",
                                       "",
                                       "7000",
                                       "8000",
                                       "9000",
                                       "",
                                       "10000"]), 24000)

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
42
43
44
45
46
47
#!/usr/bin/env python3

#Advent of Code
#2022 Day 1, 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):
    elves = [0]
    for line in puzzle_input:
        if line == "":
            elves.append(0)
        else:
            elves[-1] += int(line)

    return sum(sorted(elves)[-3:])

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

    calories = solve(puzzle_input)

    print("The elves with the three highest numbers of calories are carrying " + str(calories) + " calories.")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["1000",
                                       "2000",
                                       "3000",
                                       "",
                                       "4000",
                                       "",
                                       "5000",
                                       "6000",
                                       "",
                                       "7000",
                                       "8000",
                                       "9000",
                                       "",
                                       "10000"]), 45000)

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