Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 094b837

Browse files
committed
2018 Day 2 solution
1 parent c4d4043 commit 094b837

File tree

3 files changed

+357
-1
lines changed

3 files changed

+357
-1
lines changed

aoc2018/day2.input

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
xrecqmdonskvzupalfkwhjctdb
2+
xrlgqmavnskvzupalfiwhjctdb
3+
xregqmyonskvzupalfiwhjpmdj
4+
areyqmyonskvzupalfiwhjcidb
5+
xregqpyonskvzuaalfiwhjctdy
6+
xwegumyonskvzuphlfiwhjctdb
7+
xregumymnskvzupalfiwhjctib
8+
xregqmyonjkvzupalfvwijctdb
9+
xrmgqmyonsdvzupalfiwhjcthb
10+
xrpgqmyonskvzupalfiwhcitdb
11+
xregvmysnsjvzupalfiwhjctdb
12+
xregqsyonskvzupgqfiwhjctdb
13+
qreuqmyonskvzupalfiwyjctdb
14+
xrecqmyenskvzupalyiwhjctdb
15+
xmegqmyonskvzhpalfcwhjctdb
16+
xiegqmyonskvzupalfkwhjjtdb
17+
xreaqmyofskfzupalfiwhjctdb
18+
xregqmypnskvzupalmiwhjltdb
19+
xretqmmonskvzupalfiwhwcfdb
20+
xrexqmkonskvzupalfiwjjctdb
21+
xrigqmyonskvgupplfiwhjctdb
22+
xregqmyotskvzupalfywhjctdm
23+
xcegmmyonsvvzupalfiwhjctdb
24+
xrezqmypnskvznpalfiwhjctdb
25+
xragqmyonskvzupblfiwajctdb
26+
xregqmyonskvzwpapfiwhjctqb
27+
xoegqmyoyskvzupaufiwhjctdb
28+
xrcgqmyjnskvzupalfcwhjctdb
29+
xregqmyonskvzudalfipajctdb
30+
xsegqmyonsklzupalwiwhjctdb
31+
xregqmyocskvduaalfiwhjctdb
32+
xhegqmyfnskvzupalflwhjctdb
33+
xregqmymnykvzupalfiwhjctdm
34+
xregqmybnskvzupacfiwdjctdb
35+
xaegqmlonskvzfpalfiwhjctdb
36+
xoegtmyonskvzupalfiwhwctdb
37+
xregqmyohskvzupaqfiwhjccdb
38+
xoegqmyonstvzupalfiwhjctbb
39+
mregnmyonskszupalfiwhjctdb
40+
xreoqmycnskvzupalfiphjctdb
41+
xregqmyocskvdupacfiwhjctdb
42+
xregqmyonskvzupajqiahjctdb
43+
xregqmyonslvwupalfiwhjcfdb
44+
xregqmyonskvzapalfiwhqcthb
45+
xrerqmyonskwzupalfiwhjctdt
46+
xrefqmfonskvzupalfiwcjctdb
47+
xregqmyonskvzupadfiwhjxedb
48+
iregqhyonskvzupaliiwhjctdb
49+
iregqmyotskvzucalfiwhjctdb
50+
xrbgqmaonrkvzupalfiwhjctdb
51+
xregqmyonskvzupalfixhdctdf
52+
xrehqmyonskvzupalfiwijctdd
53+
xvegqmyonskvzupaleuwhjctdb
54+
xregqmyiyskvzupalfiwqjctdb
55+
hregqmyonskvzupaxfiwhjptdb
56+
xregamyznskbzupalfiwhjctdb
57+
xreyqmyonskvgupalziwhjctdb
58+
xregqmysnskvzupalfiwhgctdu
59+
xojgqmyonskvzupalfiwbjctdb
60+
xrkgqmyonskvlupalfiwhjcwdb
61+
xregqmyonwkvxupalfiwajctdb
62+
xregqmyonsuvzupalfjwhjcxdb
63+
xregqmyonskgzucalfiwhjstdb
64+
xaegqmyonfkvzupalfiwhjcttb
65+
xlegqmyonskvzupazfiwhjctqb
66+
xrejqmyonskvzqpaldiwhjctdb
67+
xreguryonskvzupalfiwhjctdz
68+
xregqsyoeskvzupalfiwhjctdt
69+
xregqmyonskvzubalfirhjctdp
70+
xrepqmymnskvzupadfiwhjctdb
71+
xregqayonskvzuoalfichjctdb
72+
xreqqmyonskvzunalfiwojctdb
73+
xregqmyonsivzufalciwhjctdb
74+
xregqqeonskvzupanfiwhjctdb
75+
xoegqmyunskvzppalfiwhjctdb
76+
xregqmyonskvzupalfqwhnftdb
77+
xregqmyonskvzuralkiwhjcudb
78+
xrwgqmymnskvzupalfiwhjcgdb
79+
xvrgqmyonskvzupalfiwhjcthb
80+
xregemyonskkzupalfiwhjctbb
81+
xregqmyonsevzupalfiwhjjtdl
82+
xregqmyonckvcupajfiwhjctdb
83+
xregqmysnskvzunalfnwhjctdb
84+
xreowmyonskvkupalfiwhjctdb
85+
xregqmyonskvjupalfiwhjytdr
86+
xregqmyonskyzupaffiwhmctdb
87+
xrsgqmyonszvzupmlfiwhjctdb
88+
xzegqmyonskvnupalfiwfjctdb
89+
qregqmyonskvzupalfiwhrctjb
90+
xpegqmyonsivzupqlfiwhjctdb
91+
xregqmyoyskrzupalfiwhjctdx
92+
xregqmyonsqvzupalfiwhjdndb
93+
xregjmyonskvzppalfiwhjcgdb
94+
xregqmyziskvzupalfiwhjctib
95+
xregqmyonmkvbupalfiwhjckdb
96+
xtegamyonskvzupalniwhjctdb
97+
xregqpyonskvzhpwlfiwhjctdb
98+
xvegqmfonskvzupalfiwhjcadb
99+
xregqmyonskvzupaysiwhjctxb
100+
xrejqmyonudvzupalfiwhjctdb
101+
llegqmyonskvzbpalfiwhjctdb
102+
tcegqmbonskvzupalfiwhjctdb
103+
lregqmyohskvzupalfiwhjcttb
104+
xrngqmcfnskvzupalfiwhjctdb
105+
xregqmyonspvzuuplfiwhjctdb
106+
xrxgqmyonslvzupalfiwhjctdo
107+
xregqmyonskvzulalfuwhjdtdb
108+
xregqmnonskvzupalfvwhjckdb
109+
xregqbyfnskvzupaltiwhjctdb
110+
xregqmyodsovzwpalfiwhjctdb
111+
xregomyonskvhrpalfiwhjctdb
112+
xregqmfdnskvzupalliwhjctdb
113+
xregqmyonskvzupaabithjctdb
114+
xrngamyonskvzupalfiwhjcttb
115+
xrhgqmyonskvzupaldifhjctdb
116+
xrygzmyonskvzupatfiwhjctdb
117+
xregqmyonskvzupiqtiwhjctdb
118+
xregqmyonfkvzupalfiwxjcsdb
119+
xregqsyunskvzupalfiwhjctde
120+
xrzgqmyolskvzupasfiwhjctdb
121+
xgegqmyoyskvzupalfiwfjctdb
122+
xrvgqlyohskvzupalfiwhjctdb
123+
xregcmyonskvzuprlyiwhjctdb
124+
xregqmyonskvwjpalfiwsjctdb
125+
xrfgqmyonskvzupalfidhactdb
126+
xcegqmyonwkvzdpalfiwhjctdb
127+
nregqmyrnskvzupalciwhjctdb
128+
xcegqmyonskvzvpalfiwhjctdj
129+
xregqmyonskvzupqssiwhjctdb
130+
xregcmyonskvzupalfinhjutdb
131+
xregqmyonskvzupzlfiwcjctnb
132+
xnegqmyozskvzbpalfiwhjctdb
133+
xregvmponskvzupalfiwhsctdb
134+
xregqmyonskvpupalqichjctdb
135+
xreqqmyonskvzupauuiwhjctdb
136+
xregqryonskvzupatfiwhjctyb
137+
hregqmyonokvzupalfiwhmctdb
138+
xreuqmionckvzupalfiwhjctdb
139+
xregqmyoiskvzupanfiwhjntdb
140+
xrdgqmronskvzupaluiwhjctdb
141+
xadgqmyunskvzupalfiwhjctdb
142+
eregqmzonskvzupakfiwhjctdb
143+
xiegqmyonskvnupblfiwhjctdb
144+
yregqmzonskvzupalfiwhjotdb
145+
xregqmyonskvjupalfiwhjhtvb
146+
wregqmyonskvzzprlfiwhjctdb
147+
xregqmyovskvzupalgiuhjctdb
148+
xregqmyonskjzupelfuwhjctdb
149+
xregqmysuskvpupalfiwhjctdb
150+
xrebqkyonskvzupalfiwpjctdb
151+
xregcmyonskvzipalfiwhjcttb
152+
xregqmyonskdyupalfiwgjctdb
153+
xregcmyonskvzupalfiwijctnb
154+
xregqmyonsovdupalfrwhjctdb
155+
xregqmaonskvzupalnkwhjctdb
156+
xregqmysnfkvzupalfiwhictdb
157+
xregqmyonswvzupalfiyhjctdf
158+
xreoqmyrnskvzupalfihhjctdb
159+
tregqmydnskvzupalfizhjctdb
160+
xregxmyonykvzupalfnwhjctdb
161+
xzegqnyonskuzupalfiwhjctdb
162+
xregqmfonszvvupalfiwhjctdb
163+
xrerqmyjnskvzupalfiwhpctdb
164+
xregqmyanskvzupalffphjctdb
165+
rregqmyogskvzupalfiehjctdb
166+
xrpgqmyonspvzupalfiwgjctdb
167+
xuegqmppnskvzupalfiwhjctdb
168+
xregqmyonskvzqpalsiwhjhtdb
169+
xregqzyonskvzkpalfiwujctdb
170+
xrdgqmyonskvzupglfiwhjctdu
171+
xregqmyonskqzupahciwhjctdb
172+
treqqmyonskvzupalfiwhjcqdb
173+
vlegqmyonskvzupalfiwhjwtdb
174+
xregjmyonskviupglfiwhjctdb
175+
xreggmyanskvzupalfiwhjcydb
176+
xregqmybnskvzuprlfiwhjmtdb
177+
xrsgqmyonskizupagfiwhjctdb
178+
xregqmyenskvzupalfvwhjctib
179+
lrygqmyonsrvzupalfiwhjctdb
180+
xregqmjonskvqupalfiwhjctdu
181+
xregqmyonsknzmpzlfiwhjctdb
182+
xregqmyonhkvzupllfiwhjctdz
183+
xregqmronskvdumalfiwhjctdb
184+
xrpgqmyonskvzupalfhwhjhtdb
185+
xfegqmeonskvzupasfiwhjctdb
186+
xregqqyonskvzrpalfiwijctdb
187+
xretqmmonskvzupalfiwhjcfdb
188+
xregqmyonskvznpalniwhjztdb
189+
xregqmyqnskvzuoalfiwhhctdb
190+
xregqmyonsbvzupalviwhjxtdb
191+
xregqmyonskvzupazmiwhhctdb
192+
xregqmyosskvzupalflwhjctdw
193+
xtegqmyonskvzupamciwhjctdb
194+
xregamyonskvzbpalfiwhqctdb
195+
xregqmgonskvzupalfiwhictxb
196+
xregqmyonskvjupvlfnwhjctdb
197+
xrthqmyonskvzupalfiwhjctub
198+
xrexqmyoyskvzupalfiwhjcadb
199+
xvegqmyonskvxupalfiwhjztdb
200+
xregqmyonskgzupalhiwhjptdb
201+
xregqmysnskvzufalpiwhjctdb
202+
xregqmyonskvbipalfighjctdb
203+
xregqmyonskvzupylfiwhjwvdb
204+
gregqmyonskvzupalfikhjctdt
205+
ujegqmyonskvzupalfiwhjctlb
206+
nreqqmyonskjzupalfiwhjctdb
207+
xregqmyonskvzupanfbwhjchdb
208+
xregqyyoeskwzupalfiwhjctdb
209+
xregqmyokskvzgpalfiwhnctdb
210+
lregqmyonskvzupalfawsjctdb
211+
xtegqmyonskvzmpalfiwhjctmb
212+
xtegqvyonskvzupalfiwhjdtdb
213+
xpegqpyonekvzupalfiwhjctdb
214+
qregqmyonskvzupalfiwmjctdn
215+
xregqnyosskvzupalfibhjctdb
216+
xregqmyonsknzupalflwhjctfb
217+
xregqmxoyskvzuealfiwhjctdb
218+
xregdmyoeskvzupalfiwhfctdb
219+
xremmmyonskvzupalfiwhxctdb
220+
xregqmconskvzupylfuwhjctdb
221+
xregqmyonskvzupawiiwhictdb
222+
xlegsmyonskvzupalfiwhbctdb
223+
xregqmyonsavzopalyiwhjctdb
224+
xregqmyonskczupalfibhvctdb
225+
xregqmyonskvzvpalfiunjctdb
226+
xregqmyonskvdupalfiwhjczdp
227+
xregqmyonskvzupklfswhhctdb
228+
xrelqmyonskvzupalyiwhjctdi
229+
xrcgqmyonskvzupalfieqjctdb
230+
xregqmnonskvzupacfewhjctdb
231+
xrwgqmyonskvzuealfiwhcctdb
232+
xregqiyonsevzmpalfiwhjctdb
233+
xregqmyonjyvzupalfiwhjckdb
234+
xregqmyonyklzupadfiwhjctdb
235+
xregqmyanskvzupolfiwhjctpb
236+
xdbgqmyonskvzupslfiwhjctdb
237+
xregqmhonykvzupalfawhjctdb
238+
xregqmqonsivzupalfifhjctdb
239+
xregqgyonsrvzupalfiwhjctib
240+
xregqmyofskvzupalfiwlfctdb
241+
xregqmyovskvzupllftwhjctdb
242+
xregqmyonskvzupaciiwhuctdb
243+
xregqmyonsdvzuhalfiwhjhtdb
244+
xreiqmyonskvzupalfiwhncldb
245+
xregqmyongkvzugalfiwhjctxb
246+
xregqsyonskvzrpmlfiwhjctdb
247+
xrogqmyonskvzxpalfiwhbctdb
248+
xregqmkonskvzuqalfiwhjptdb
249+
xregqmyonskvvxpalfiwhactdb
250+
xregqmyonskvzupsliiwhwctdb

aoc2018/day2.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python
2+
"""
3+
--- Day 2: Inventory Management System ---
4+
5+
You stop falling through time, catch your breath, and check the screen on the device. "Destination reached. Current Year: 1518. Current Location: North Pole Utility Closet 83N10." You made it! Now, to find those anomalies.
6+
7+
Outside the utility closet, you hear footsteps and a voice. "...I'm not sure either. But now that so many people have chimneys, maybe he could sneak in that way?" Another voice responds, "Actually, we've been working on a new kind of suit that would let him fit through tight spaces like that. But, I heard that a few days ago, they lost the prototype fabric, the design plans, everything! Nobody on the team can even seem to remember important details of the project!"
8+
9+
"Wouldn't they have had enough fabric to fill several boxes in the warehouse? They'd be stored together, so the box IDs should be similar. Too bad it would take forever to search the warehouse for two similar box IDs..." They walk too far away to hear any more.
10+
11+
Late at night, you sneak to the warehouse - who knows what kinds of paradoxes you could cause if you were discovered - and use your fancy wrist device to quickly scan every box and produce a list of the likely candidates (your puzzle input).
12+
13+
To make sure you didn't miss any, you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to what your device predicts.
14+
15+
For example, if you see the following box IDs:
16+
17+
abcdef contains no letters that appear exactly two or three times.
18+
bababc contains two a and three b, so it counts for both.
19+
abbcde contains two b, but no letter appears exactly three times.
20+
abcccd contains three c, but no letter appears exactly two times.
21+
aabcdd contains two a and two d, but it only counts once.
22+
abcdee contains two e.
23+
ababab contains three a and three b, but it only counts once.
24+
25+
Of these box IDs, four of them contain a letter which appears exactly twice, and three of them contain a letter which appears exactly three times. Multiplying these together produces a checksum of 4 * 3 = 12.
26+
27+
What is the checksum for your list of box IDs?
28+
29+
--- Part Two ---
30+
31+
Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.
32+
33+
The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:
34+
35+
abcde
36+
fghij
37+
klmno
38+
pqrst
39+
fguij
40+
axcye
41+
wvxyz
42+
43+
The IDs abcde and axcye are close, but they differ by two characters (the second and fourth). However, the IDs fghij and fguij differ by exactly one character, the third (h and u). Those must be the correct boxes.
44+
45+
What letters are common between the two correct box IDs? (In the example above, this is found by removing the differing character from either ID, producing fgij.)
46+
47+
"""
48+
from __future__ import print_function
49+
import collections
50+
import itertools
51+
import os
52+
53+
54+
def checksum(data):
55+
doubles = 0
56+
triples = 0
57+
for x in data:
58+
counts = collections.Counter(x)
59+
if 2 in counts.values():
60+
doubles += 1
61+
if 3 in counts.values():
62+
triples += 1
63+
64+
return doubles * triples
65+
66+
67+
def compare(a, b):
68+
count_same = 0
69+
common = []
70+
for i, j in zip(a, b):
71+
if i == j:
72+
common.append(i)
73+
else:
74+
count_same += 1
75+
if count_same == 1:
76+
return common
77+
return []
78+
79+
80+
def common_letters(data):
81+
for a, b in itertools.combinations(data, 2):
82+
common = compare(a, b)
83+
if common:
84+
return "".join(common)
85+
86+
87+
def solve(data, flag=False):
88+
if not flag:
89+
return checksum(data)
90+
else:
91+
return common_letters(data)
92+
93+
94+
if __name__ == "__main__":
95+
this_dir = os.path.dirname(__file__)
96+
with open(os.path.join(this_dir, "day2.input")) as f:
97+
data = f.read().splitlines()
98+
print(f"The checksum is {solve(data, False)}")
99+
print(f"The common letters in the box IDs are {solve(data, True)}")

aoc2018/test_aoc.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from . import day1
8+
from . import day1, day2
99

1010

1111
@pytest.mark.parametrize(
@@ -22,3 +22,10 @@
2222
)
2323
def test_day_1(data, answer, flag):
2424
assert day1.solve(data, flag) == answer
25+
26+
@pytest.mark.parametrize('data,answer,flag', [
27+
(["abcdef", "bababc", "abbcde", "abcccd", "aabcdd", "abcdee", "ababab"], 12, False),
28+
(["abcde", "fghij", "klmno", "pqrst", "fguij", "axcye", "wvxyz"], "fgij", True)
29+
])
30+
def test_day_2(data, answer, flag):
31+
assert day2.solve(data, flag) == answer

0 commit comments

Comments
 (0)