Skip to content

Commit a3ceaa4

Browse files
committed
day 4
1 parent 61d85af commit a3ceaa4

File tree

2 files changed

+1040
-0
lines changed

2 files changed

+1040
-0
lines changed

2022/04/code.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Advent of Code 2022 - Day 4
2+
# https://adventofcode.com/2022/day/4
3+
# Author: Alexandre MALFREYT
4+
5+
with open('input.txt', 'r') as f:
6+
pairs = [line.replace('\n', '') for line in f.readlines()]
7+
8+
# "a-b,xy" => [['a', 'b'], ['x', 'y']]
9+
pairs = [
10+
[
11+
[
12+
int(x)
13+
for x in elf.split('-')
14+
]
15+
for elf in pair.split(',')
16+
]
17+
for pair in pairs
18+
]
19+
20+
21+
# Part 1
22+
total = 0
23+
for pair in pairs:
24+
# Check if the range of one elf if fully included in the range of the other
25+
if (pair[0][0] <= pair[1][0] and pair[0][1] >= pair[1][1])\
26+
or (pair[1][0] <= pair[0][0] and pair[1][1] >= pair[0][1]):
27+
total += 1
28+
29+
print(f'Part 1 : {total}')
30+
31+
32+
# Part 2
33+
total = 0
34+
for pair in pairs:
35+
# Check if the range of one elf overlaps the range of the other
36+
if (pair[0][0] <= pair[1][0] and pair[0][1] >= pair[1][0])\
37+
or (pair[1][0] <= pair[0][0] and pair[1][1] >= pair[0][0]):
38+
total += 1
39+
40+
print(f'Part 2 : {total}')

0 commit comments

Comments
 (0)