summaryrefslogtreecommitdiff
path: root/2018/aoc2018-d02.py
blob: 714253799a23101335752fbf72a583b1cb08db76 (plain)
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
#advent of code 2018
#day 02
#part 1 and 2

PuzzleInput = open("02.in","r");
boxes = [];
for line in PuzzleInput:
	line = line.replace("\n","");
	boxes.append(line);

duplicates = dict();
duplicates[2] = set();
duplicates[3] = set();
for box in boxes:
	boxdict = dict.fromkeys(set(list(box)),0);
	for bchar in boxdict:
		boxdict[bchar] = box.count(bchar);
	for num in [2,3]:
		if num in boxdict.values():
			duplicates[num].add(box);
	
p1 = len(duplicates[3]) * len(duplicates[2]);
print("part 1 =", p1);

visited = set();
p2 = None;
for box1 in boxes:
	for box2 in boxes:
		if box1 == box2: continue;
		if box2 in visited: continue;
		matched = 0;
		difference = set();
		for i in range(len(box1)):
			if box1[i] != box2[i] : difference.add(box1[i]);
			if len(difference) > 1: break;
		if len(difference) == 1:
			DifferentChar = difference.pop();
			p2 = box1.replace(DifferentChar,"");
			break;
	if p2: break;
	visited.add(box1);

print("part 2 =", p2);