d01 with python

This commit is contained in:
JonOfUs 2023-12-05 19:12:27 +01:00
parent 9c8fc68651
commit 83b170f6b9
2 changed files with 25 additions and 3 deletions

22
python/day01.py Normal file
View file

@ -0,0 +1,22 @@
import re
path = 'res/01/input'
nums = ['zero','one','two','three','four','five','six','seven','eight','nine']
pattern_str = r'one|two|three|four|five|six|seven|eight|nine'
result1 = result2 = 0
with open(path, 'r') as file:
for line in file:
numbers = [c for c in line if c.isdigit()]
result1 += int(numbers[0] + numbers[-1])
first = re.search(r'\d|' + pattern_str, line).group()
second = re.search(r'\d|' + pattern_str[::-1], line[::-1]).group()[::-1]
num = first + ' ' + second
for i,n in enumerate(nums):
num = num.replace(n,str(i))
result2 += int(num.replace(" ", ""), 10)
print(result1)
print(result2)

View file

@ -118,9 +118,9 @@ fn parse_maps(seeds_arc: &Arc<Mutex<Vec<usize>>>, seeds_len: usize, contents: &V
let mut seeds = seeds_arc.lock().unwrap();
if seeds[i] >= range_start && seeds[i] <= range_end {
seeds[i] = ranges[j][0] + (seeds[i] - range_start);
break;
break
}
}
});
});
})
})
}