Created
December 15, 2022 04:43
-
-
Save halitanildonmez/1f93711d4cb543b7d91c4a5ab1226c0e to your computer and use it in GitHub Desktop.
Advent of Code 2020 day 16 all
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" | |
with open("aa.txt") as f: | |
notes = f.read().strip() | |
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 | |
valid_tics = [] | |
nearby_tickets = [] | |
for i in range(1, len(near_ticket_lines)): | |
tic_str = near_ticket_lines[i] | |
tt = [] | |
for j, tic in enumerate(tic_str.split(',')): | |
tt.append(int(tic)) | |
nearby_tickets.append(tt) | |
for i in range(1, len(near_ticket_lines)): | |
tic_str = near_ticket_lines[i] | |
all_m = True | |
tt = [] | |
for j, tic in enumerate(tic_str.split(',')): | |
tt.append(int(tic)) | |
for rule in int_map: | |
if int(tic) not in all_intervals: | |
all_m = False | |
break | |
if all_m == True: | |
t = [] | |
for l in tic_str.split(','): | |
t.append(int(l)) | |
valid_tics.append(t) | |
my_tic = [89,179,173,167,157,127,163,113,137,109,151,131,97,149,107,83,79,139,59,53] | |
product = 1 | |
fields = [ | |
(c, int(a_1), int(a_2), int(b_1), int(b_2)) | |
for c, a_1, a_2, b_1, b_2 in re.findall( | |
r"((?:\w+ ?)+): (?:(\d+)-(\d+)) or (?:(\d+)-(\d+))", notes | |
) | |
] | |
columns = set(range(len(fields))) | |
for _ in range(len(fields)): | |
for i, (field, a, b, c, d) in enumerate(fields): | |
candidates = [ | |
col | |
for col in columns | |
if all( | |
a <= int(ticket[col]) <= b or c <= int(ticket[col]) <= d | |
for ticket in valid_tics | |
) | |
] | |
if len(candidates) == 1: | |
columns.remove(candidates[0]) | |
fields = fields[:i] + fields[i + 1 :] | |
if field.startswith("departure"): | |
product *= my_tic[candidates[0]] | |
break | |
print('part2:', product) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment