Created
December 14, 2022 17:24
-
-
Save halitanildonmez/e1da5de5807de939a39a707e2f988707 to your computer and use it in GitHub Desktop.
Advent of Code 2020 day 16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import collections | |
import itertools | |
import re | |
import ast | |
import functools | |
import numpy as np | |
from collections import defaultdict | |
T = "bb.txt" | |
M = "aa.txt" | |
file1 = open(M, 'r') | |
lines = file1.read().splitlines() | |
all_intervals = set() | |
def get_interval(s): | |
res = set() | |
a, b = s.split('-') | |
a, b = sorted((int(a), int(b))) | |
diff = b-a | |
all_intervals.add(a) | |
res.add(a) | |
for i in range(diff+1): | |
all_intervals.add(a+i) | |
res.add(a+i) | |
all_intervals.add(b) | |
res.add(b) | |
return res | |
def get_interval_2(s): | |
res = set() | |
a, b = s.split('-') | |
a, b = sorted((int(a), int(b))) | |
return a, b | |
intervals = [] | |
near_ticket_lines = [] | |
interval_map = {} | |
ticket_lines = [] | |
mod = 0 | |
for line in lines: | |
if len(line.strip()) == 0: | |
mod += 1 | |
continue | |
if mod == 0: | |
intervals.append(line) | |
elif mod == 1: | |
ticket_lines.append(line) | |
else: | |
near_ticket_lines.append(line) | |
int_map = {} | |
for interval in intervals: | |
vals = interval.split(': ')[1] | |
ints = vals.split(' or ') | |
tmp = set() | |
for g in get_interval(ints[0]): | |
tmp.add(g) | |
for g in get_interval(ints[1]): | |
tmp.add(g) | |
interval_map[interval.split(': ')[0]] = tmp | |
int_map[interval.split(': ')[0]] = [get_interval_2(ints[0]), get_interval_2(ints[1])] | |
pt1 = 0 | |
for i in range(1, len(near_ticket_lines)): | |
tic_str = near_ticket_lines[i] | |
for j, tic in enumerate(tic_str.split(',')): | |
if int(tic) not in all_intervals: | |
pt1 += int(tic) | |
print('part1:',pt1) | |
def is_within(val, x): | |
a, b = int(x[0][0]), int(x[0][1]) | |
c, d = int(x[1][0]), int(x[1][1]) | |
return a <= val <= b or c <= val <= d | |
other_tickets = [] | |
test = defaultdict(set) | |
test_2 = defaultdict(set) | |
ttx = set(range(20)) | |
allo = set() | |
for inter in int_map: | |
c = set() | |
for i in range(1, len(near_ticket_lines)): | |
for ti, x in enumerate(near_ticket_lines[i].split(',')): | |
if is_within(int(x), int_map[inter]) == True: | |
c.add(ti) | |
else: | |
c.discard(ti) | |
print(inter, c, len(c)) | |
#Manual work: | |
#0 89 departure track | |
#3 167 departure date | |
#4 157 departure location | |
#7 113 departure time | |
#9 109 departure platform | |
#11 131 departure station | |
print(ttx) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment