add: part 1
This commit is contained in:
parent
5db1f6a717
commit
299a70970e
33
2023/day05/example1.txt
Normal file
33
2023/day05/example1.txt
Normal file
@ -0,0 +1,33 @@
|
||||
seeds: 79 14 55 13
|
||||
|
||||
seed-to-soil map:
|
||||
50 98 2
|
||||
52 50 48
|
||||
|
||||
soil-to-fertilizer map:
|
||||
0 15 37
|
||||
37 52 2
|
||||
39 0 15
|
||||
|
||||
fertilizer-to-water map:
|
||||
49 53 8
|
||||
0 11 42
|
||||
42 0 7
|
||||
57 7 4
|
||||
|
||||
water-to-light map:
|
||||
88 18 7
|
||||
18 25 70
|
||||
|
||||
light-to-temperature map:
|
||||
45 77 23
|
||||
81 45 19
|
||||
68 64 13
|
||||
|
||||
temperature-to-humidity map:
|
||||
0 69 1
|
||||
1 0 69
|
||||
|
||||
humidity-to-location map:
|
||||
60 56 37
|
||||
56 93 4
|
42
2023/day05/src/part1.py
Normal file
42
2023/day05/src/part1.py
Normal file
@ -0,0 +1,42 @@
|
||||
text: str = open("input.txt", "r").read()
|
||||
|
||||
_value: int = 0
|
||||
|
||||
lines = text.splitlines()
|
||||
|
||||
current_values = []
|
||||
next_convertion = {}
|
||||
|
||||
for value in lines[0][7:].split(" "):
|
||||
current_values.append(int(value))
|
||||
|
||||
def extract_map_data(lines: [str]):
|
||||
lst = []
|
||||
for line in lines:
|
||||
if (line == ""):
|
||||
break
|
||||
bozo = line.split()
|
||||
length = int(bozo[2])
|
||||
destination = int(bozo[0]) - int(bozo[1])
|
||||
source_range_start = range(int(bozo[1]), int(bozo[1]) + length)
|
||||
lst.append({"destination": destination, "source_range_start": source_range_start})
|
||||
return lst
|
||||
|
||||
def translation(lst: list[dict], value: int):
|
||||
for map in lst:
|
||||
if value in map["source_range_start"]:
|
||||
value += map["destination"]
|
||||
break
|
||||
return value
|
||||
|
||||
lines = lines[2:]
|
||||
for i, line in enumerate(lines):
|
||||
lst = []
|
||||
if (line.endswith(" map:")):
|
||||
lst = extract_map_data(lines[i + 1:])
|
||||
for j, value in enumerate(current_values):
|
||||
current_values[j] = translation(lst, value)
|
||||
|
||||
_value = min(current_values)
|
||||
|
||||
print(_value)
|
Loading…
Reference in New Issue
Block a user