day 4
This commit is contained in:
		| @@ -10,6 +10,6 @@ Save the session key from https://adventofcode.com/ to .env file in the variable | |||||||
|  |  | ||||||
| ## Execute | ## 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}``` | Then run code easily with ```./run {Number-of-day}``` | ||||||
							
								
								
									
										6
									
								
								deps.txt
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								deps.txt
									
									
									
									
									
								
							| @@ -1,3 +1,3 @@ | |||||||
| cpl-cli==2022.10.1 | cpl-cli==2022.12.0 | ||||||
| cpl-core==2022.10.0.post9 | cpl-core==2022.12.0 | ||||||
| cpl-query==2022.10.0.post2 | cpl-query==2022.12.0 | ||||||
|   | |||||||
							
								
								
									
										67
									
								
								src/day4.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/day4.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||||
		Reference in New Issue
	
	Block a user