From 16a93b1400e5f7ea4b60c7b0c1dd913fa82f7905 Mon Sep 17 00:00:00 2001 From: kleph Date: Sun, 4 Dec 2022 13:25:49 +0100 Subject: [PATCH] 2022 - Day 4 part 2 --- 2022/4/4_2.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2022/4/4_2.py diff --git a/2022/4/4_2.py b/2022/4/4_2.py new file mode 100644 index 0000000..eb82e2d --- /dev/null +++ b/2022/4/4_2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# 2022 - Advent Of Code 4 part 2 + +# file = 'input_example.txt' +file = 'input.txt' + +overlap = 0 +with open(file, encoding="utf-8") as f: + ranges_list = [line.strip().split(',') for line in f.readlines()] + for ranges in ranges_list: + elf1_r = ranges[0].split('-') + elf2_r = ranges[1].split('-') + + if int(elf1_r[0]) <= int(elf2_r[0]) <= int(elf1_r[1]): + overlap += 1 + elif int(elf1_r[0]) <= int(elf2_r[1]) <= int(elf1_r[1]): + overlap += 1 + elif int(elf2_r[0]) <= int(elf1_r[0]) <= int(elf2_r[1]): + overlap += 1 + elif int(elf2_r[0]) <= int(elf1_r[1]) <= int(elf2_r[1]): + overlap += 1 + +print(overlap)