Part 1 day 5 at least so far ;-;
This commit is contained in:
parent
431c304645
commit
9fac5539f0
47
5/1.py
Normal file
47
5/1.py
Normal file
@ -0,0 +1,47 @@
|
||||
def mid(values: list[str]):
|
||||
return values[len(values)//2]
|
||||
|
||||
rules,updates = {}, []
|
||||
with open('sample.data') as data:
|
||||
for line in data:
|
||||
if '|' in line:
|
||||
left, right = [int(i) for i in line.split('|')]
|
||||
if left in rules:
|
||||
rules[left].append(right)
|
||||
else:
|
||||
rules[left] = [right]
|
||||
if ',' in line:
|
||||
updates.append([int(i) for i in line.split(',')])
|
||||
|
||||
def sort_updates(rules: dict[int, list[int]], updates: list) -> [list[int], list[int]]:
|
||||
correct, incorrect = [], []
|
||||
for update in updates:
|
||||
for idx, val in enumerate(update):
|
||||
fail = False
|
||||
for after in update[idx:]:
|
||||
if after == val:
|
||||
continue
|
||||
try:
|
||||
if after not in rules[val]:
|
||||
fail = True
|
||||
incorrect.append(update)
|
||||
break
|
||||
except KeyError:
|
||||
fail = True
|
||||
incorrect.append(update)
|
||||
break
|
||||
if fail:
|
||||
break
|
||||
else:
|
||||
# 75,47,61,53,29
|
||||
# 97,61,53,29,13
|
||||
# 75,29,13
|
||||
print('good', update)
|
||||
correct.append(mid(update))
|
||||
return correct, incorrect
|
||||
|
||||
correct, incorrect = sort_updates(rules, updates)
|
||||
print('COrrect', sum(correct))
|
||||
print('incorrect', *incorrect)
|
||||
|
||||
|
17
5/2.py
Normal file
17
5/2.py
Normal file
@ -0,0 +1,17 @@
|
||||
def mid(values: list[str]):
|
||||
return values[len(values)//2]
|
||||
|
||||
rules,updates = {}, []
|
||||
with open('sample.data') as data:
|
||||
for line in data:
|
||||
if '|' in line:
|
||||
left, right = [int(i) for i in line.split('|')]
|
||||
if left in rules:
|
||||
rules[left].append(right)
|
||||
else:
|
||||
rules[left] = [right]
|
||||
if ',' in line:
|
||||
updates.append([int(i) for i in line.split(',')])
|
||||
|
||||
|
||||
|
1387
5/input.data
Normal file
1387
5/input.data
Normal file
File diff suppressed because it is too large
Load Diff
28
5/sample.data
Normal file
28
5/sample.data
Normal file
@ -0,0 +1,28 @@
|
||||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
75,47,61,53,29
|
||||
97,61,53,29,13
|
||||
75,29,13
|
||||
75,97,47,61,53
|
||||
61,13,29
|
||||
97,13,75,29,47
|
Loading…
Reference in New Issue
Block a user