From eb9c42dd0e09d6b7ef19de05b73f2af2d8494558 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 5 Dec 2025 13:05:40 +0100 Subject: [PATCH] Added 2025 day5 --- 2025/05/solution.py | 100 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 +- 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 2025/05/solution.py diff --git a/2025/05/solution.py b/2025/05/solution.py new file mode 100644 index 0000000..b0ad076 --- /dev/null +++ b/2025/05/solution.py @@ -0,0 +1,100 @@ +#!/bin/python3 +import sys,time,re,os,copy +from pprint import pprint +sys.path.insert(0, '../../') +from fred import * + +from itertools import chain + +start_time = time.time() + +if sys.argv[1] == 'test': + input_f = 'test' +elif sys.argv[1] == 'input': + input_f = 'input' +else: + print('No argv provided') + exit() + + +######################################### +# # +# Part 1 # +# # +######################################### + +def part1(): + + score = 0 + lines = loadFile(input_f) + ranges = [] + fruits = [] + split = False + for l in lines: #split the input into two groups. I use a boolean, so i don't have to do a special comparison on each line (eg. check for -) + if l == '': + split = True + elif not split: + ranges.append(l) + else: + fruits.append(l) + for f in list2int(fruits): + found = False + while not found: + for r in ranges: + + [start,end] = list2int(r.split('-')) + if start <= f <= end: + found = True + score += 1 + break + break + + + return score + +start_time = time.time() +p1 = part1() +print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms') + +######################################### +# # +# Part 2 # +# # +######################################### + +def part2(): + score = 0 + lines = loadFile(input_f) + ranges = [] + for l in lines: + if l == '': + break + ranges.append(l) + + total_ranges = [] + for r in ranges: + start,end = list2int(r.split('-')) + total_ranges.append([start,end]) + + total_ranges = sorted(total_ranges) + + new_range = [] + c = total_ranges[0] #current + for x in range(1,len(total_ranges)): + n = total_ranges[x] #next + if c[1] >= n[0]: + c = [c[0], max(c[1],n[1])] + else: + new_range.append(c) + c = n + new_range.append(c) + + for x in new_range: + score += (x[1]-x[0]+1) + + return score + +start_time = time.time() +p2 = part2() +print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms') + diff --git a/README.md b/README.md index 3c167a7..be4930b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ \_]__--|_|___[]_[]_[]__//_| 3 ** ____________//___ __________________________ ..| \ '''''' // @@| 4 ** - + |_ ___ | .--. () () |.' ..__[#]_@@__//_@@@| + |_\_|^|_]_|==|_T_T_T_T_T_...' 5 **