-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_01.py
66 lines (55 loc) · 1.75 KB
/
Day_01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
"""
Advent of Code 2023, Day 1
https://adventofcode.com/2023/day/1
2023-12-04, 15:45; 2023-12-04, 23:35; 2023-12-09, 12:11
"""
### Load Data #################################################################
# read input
filename = 'Day_01.txt'
f = open(filename)
text = f.readlines()
# example input (1)
# text = ['1abc2', 'pqr3stu8vwx', 'a1b2c3d4e5f', 'treb7uchet']
# example input (2)
# text = ['two1nine', 'eightwothree', 'abcone2threexyz', 'xtwone3four', '4nineeightseven2', 'zoneight234', '7pqrstsixteen']
### Part One ##################################################################
# analyze input
my_sum = 0
for line in text:
digits = [c for c in line if c in '0123456789']
number = int(digits[0]+digits[-1])
my_sum = my_sum + number
print(my_sum)
### Part Two ##################################################################
# analyze input
my_sum = 0
digits = '123456789'
numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
for line in text:
num_chars = len(line)
first = ''
last = ''
found = False
for i in range(num_chars):
if line[i] in digits:
first = line[i]
found = True
for j, number in enumerate(numbers):
if line[i:(i+len(number))] == number:
first = str(j+1)
found = True
if found: break
found = False
for i in range(num_chars-1,-1,-1):
if line[i] in digits:
last = line[i]
found = True
for j, number in enumerate(numbers):
if line[i:(i+len(number))] == number:
last = str(j+1)
found = True
if found: break
number = int(first+last)
my_sum = my_sum + number
print(my_sum)