diff --git a/README.md b/README.md index a786c7e..bd2bf66 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,6 @@ Save the session key from https://adventofcode.com/ to .env file in the variable ## Execute -First create a venv and install deps with ```pip install -r deps.txt --extra-index-url https://pip.sh-edraft.de``` +First create a venv and install deps with ```pip install -r deps.txt --upgrade --extra-index-url https://pip.sh-edraft.de``` Then run code easily with ```./run {Number-of-day}``` \ No newline at end of file diff --git a/deps.txt b/deps.txt index 9ccabc6..2644646 100644 --- a/deps.txt +++ b/deps.txt @@ -1,3 +1,3 @@ -cpl-cli==2022.10.1 -cpl-core==2022.10.0.post9 -cpl-query==2022.10.0.post2 +cpl-cli==2022.12.0 +cpl-core==2022.12.0 +cpl-query==2022.12.0 diff --git a/src/day4.py b/src/day4.py new file mode 100644 index 0000000..4501ac9 --- /dev/null +++ b/src/day4.py @@ -0,0 +1,67 @@ +from typing import Optional + +from cpl_core.console import Console +from cpl_core.utils import String +from cpl_query.enumerable import Enumerable +from cpl_query.extension import List +from cpl_core.pipes import * + +from aoc.aoc import get_input + +# global vars +day = 4 +aoc_input = get_input(2022, day) +# aoc_input = """2-4,6-8 +# 2-3,4-5 +# 5-7,7-9 +# 2-8,3-7 +# 6-6,4-6 +# 2-6,4-8 +# """ + + +def part1() -> List[List]: + groups = List(List) + overlaps = 0 + for pairs in aoc_input.splitlines(): + pair1, pair2 = pairs.split(',') + g1 = List(int).extend(range(int(pair1.split('-')[0]), int(pair1.split('-')[1]) + 1)) + g2 = List(int).extend(range(int(pair2.split('-')[0]), int(pair2.split('-')[1]) + 1)) + + if set(g1).issubset(g2): + overlaps += 1 + + if set(g2).issubset(g1) or set(g1).issubset(g2): + overlaps += 1 + + groups.add(g1) + groups.add(g2) + + Console.write_line(groups.count(), overlaps) + return groups + + +def part2(groups: List[List]): + pairs = List(List) + + pair = List(List) + for i, group in enumerate(groups): + pair.add(group) + if i % 2 != 0: + pairs.add(pair) + pair = List(List) + + overlaps = 0 + for pair in pairs: + pair: List = pair + if pair[0].any(lambda x: x in pair[1]) or pair[1].any(lambda x: x in pair[0]): + overlaps += 1 + + Console.write_line(overlaps) + + +if __name__ == '__main__': + Console.write_line(f'Advent of code day {day}') + groups = part1() + part2(groups) + Console.write_line()