zerosleeps

Since 2010

Advent of Code 2022 day 1

My Python solution for Advent of Code 2022 day 1. About 90% of my time was spent working out how nested list comprehension works - it’s always been one of my downfalls in these things!

import unittest
import utils


def part_one(raw_input):
    return max([sum(elf) for elf in parse_raw_input(raw_input)])


def part_two(raw_input):
    return sum(sorted([sum(elf) for elf in parse_raw_input(raw_input)])[-3:])


def parse_raw_input(raw_input):
    return [
        [int(calories) for calories in elf.splitlines()]
        for elf in [elves for elves in raw_input.split("\n\n")]
    ]


class TestExamples(unittest.TestCase):
    def setUp(self):
        self.raw_input = (
            "1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000"
        )

    def test_part_one(self):
        self.assertEqual(part_one(self.raw_input), 24000)

    def test_part_two(self):
        self.assertEqual(part_two(self.raw_input), 45000)


if __name__ == "__main__":
    raw_input = utils.get_raw_input_as_str("day_01_input.txt")

    print(f"Part one: {part_one(raw_input)}")
    print(f"Part two: {part_two(raw_input)}")