Skip to content

Commit b100519

Browse files
committed
2015 Day 08 part 1 & 2
1 parent eadb108 commit b100519

File tree

6 files changed

+496
-1
lines changed

6 files changed

+496
-1
lines changed

2015/day-08.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use adventofcode\Year2015\StringParser;
6+
7+
/**
8+
* --- Day 8: Matchsticks ---
9+
*
10+
* Space on the sleigh is limited this year, and so Santa will be bringing his list as a digital copy. He needs to
11+
* know how much space it will take up when stored.
12+
*
13+
* It is common in many programming languages to provide a way to escape special characters in strings. For example,
14+
* C, JavaScript, Perl, Python, and even PHP handle special characters in very similar ways.
15+
*
16+
* However, it is important to realize the difference between the number of characters in the code representation of
17+
* the string literal and the number of characters in the in-memory string itself.
18+
*
19+
* For example:
20+
*
21+
* - "" is 2 characters of code (the two double quotes), but the string contains zero characters.
22+
* - "abc" is 5 characters of code, but 3 characters in the string data.
23+
* - "aaa\"aaa" is 10 characters of code, but the string itself contains six "a" characters and a single, escaped
24+
* quote character, for a total of 7 characters in the string data.
25+
* - "\x27" is 6 characters of code, but the string itself contains just one - an apostrophe ('),
26+
* escaped using hexadecimal notation.
27+
*
28+
* Santa's list is a file that contains many double-quoted string literals, one on each line. The only escape
29+
* sequences used are \\ (which represents a single backslash), \" (which represents a lone double-quote character),
30+
* and \x plus two hexadecimal characters (which represents a single character with that ASCII code).
31+
*
32+
* Disregarding the whitespace in the file, what is the number of characters of code for string literals minus
33+
* the number of characters in memory for the values of the strings in total for the entire file?
34+
*
35+
* For example, given the four strings above, the total number of characters of string code (2 + 5 + 10 + 6 = 23)
36+
* minus the total number of characters in memory for string values (0 + 3 + 7 + 1 = 11) is 23 - 11 = 12.
37+
*/
38+
39+
$stringParser = new StringParser();
40+
$input = file(__DIR__ . '/inputs/day-08.input', FILE_IGNORE_NEW_LINES);
41+
42+
$result = $stringParser->computeStringUsage($input);
43+
print(
44+
'The total number of characters of string code (' .
45+
$result['charactersOfStringCode'] . ') minus the total number of characters in memory (' .
46+
$result['charactersInMemory'] . ') is ' .
47+
$result['charactersOfStringCode'] - $result['charactersInMemory'] .
48+
".\n"
49+
);
50+
51+
/**
52+
* --- Part Two ---
53+
*
54+
* Now, let's go the other way. In addition to finding the number of characters of code, you should now encode each
55+
* code representation as a new string and find the number of characters of the new encoded representation,
56+
* including the surrounding double quotes.
57+
*
58+
* For example:
59+
*
60+
* - "" encodes to "\"\"", an increase from 2 characters to 6.
61+
* - "abc" encodes to "\"abc\"", an increase from 5 characters to 9.
62+
* - "aaa\"aaa" encodes to "\"aaa\\\"aaa\"", an increase from 10 characters to 16.
63+
* - "\x27" encodes to "\"\\x27\"", an increase from 6 characters to 11.
64+
*
65+
* Your task is to find the total number of characters to represent the newly encoded strings minus the number of
66+
* characters of code in each original string literal. For example, for the strings above, the total encoded
67+
* length (6 + 9 + 16 + 11 = 42) minus the characters in the original code representation (23, just like in the
68+
* first part of this puzzle) is 42 - 23 = 19.
69+
*/
70+
71+
$result = $stringParser->computeEncodedStringUsage($input);
72+
print(
73+
'The total number of characters of encoded string code (' .
74+
$result['charactersOfEncodedStrings'] . ') minus the total number of characters of string code (' .
75+
$result['charactersOfStringCode'] . ') is ' .
76+
$result['charactersOfEncodedStrings'] - $result['charactersOfStringCode'] .
77+
".\n"
78+
);

2015/inputs/day-08-sample.input

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
""
2+
"abc"
3+
"aaa\"aaa"
4+
"\x27"

2015/inputs/day-08.input

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
"\xa8br\x8bjr\""
2+
"nq"
3+
"zjrfcpbktjmrzgsz\xcaqsc\x03n\"huqab"
4+
"daz\\zyyxddpwk"
5+
"draes\xa2n\\g\x27ek\"lj\"\\viqych"
6+
"nnx\\krnrfomdnt\x2flbl\xd2xpo\"cp\"k"
7+
"kwdaapalq"
8+
"u\"ptk"
9+
"ckhorczuiudfjmmcc\\u\"wozqxibsfjma"
10+
"ydctdrxat\"pd\"lwi\"bjesevfw\xe8"
11+
"v\"\xa8rrzep\"\"r"
12+
"nbydghkfvmq\\\xe0\"lfsrsvlsj\"i\x61liif"
13+
"jsas\"u"
14+
"odipikxlo"
15+
"\"rnubsgwltqkbsu\"pcpcs"
16+
"eitk\\f\\mhcqqoym\\ji"
17+
"vnedc"
18+
"\"lhcaurdqzyjyu"
19+
"haxzsa\"zcn\"y\"foclgtjfcnv\"m\x68krc"
20+
"\"eoeggg\"tmiydvcay\"vfavc"
21+
"snqvyqoncwxcvwbdktoywch"
22+
"rnfgjsyr\xd5wacy"
23+
"ik\"hebrpvsts"
24+
"txw"
25+
"\x15pxtdkogd\"urbm\"gevhh\"nxr\x3erxtk"
26+
"cetqtcy"
27+
"inleep\\mgl"
28+
"uflwbxvww\x2cxzezqnaply\"yh\"qlllzk"
29+
"eepak\"xqtedzt"
30+
"na\x61qzfieafvyrsnwkssznohjmc"
31+
"yceaonylz\xc1\\jrlbbkzwsidfi"
32+
"ybqafngkcqpbp"
33+
"\xaft"
34+
"yidjpaobqydso"
35+
"ju\\ldxig\\lrdrhjcmm\x77rc"
36+
"tylacqeslnwj\x48ds\"tjxa"
37+
"efbfm"
38+
"\\fxkgoprgdcjgyajykg\\dtbrz"
39+
"eujvva"
40+
"h\x7acwfpikme\\vwthyvrqdnx\""
41+
"rbpbrxm\\\"\"\"voxx"
42+
"ykiw\"tkb\\lforu\"rsf\\tf\"x\"rqti"
43+
"e\\wh\x77aqeugiq\\ihhfqfuaij"
44+
"g\"t\\o"
45+
"nxzo\"hf\\xp"
46+
"dxiaqfo\xea"
47+
"kali\\zczhiqkqzybjj\"fgdjnik"
48+
"zdkgrqmdv"
49+
"bimxim\xb6lrwsaj\"ui\"a"
50+
"\"rrznitibgx\\olpsjmjqzctxaubdifsq"
51+
"zb\"khzixaacmhuzmlymoformipdzml"
52+
"qfwi"
53+
"hjwsxfpphttjy\"\"zixais\xbblgnqfto"
54+
"puj\\qmyu\"nqgaqfthbwjokbmrpbhpi"
55+
"cyxdpkh\\\""
56+
"q"
57+
"m"
58+
"tbxdzzllarlo"
59+
"gbtys"
60+
"gytilk\\vlqxvcuutjunrqc"
61+
"uugkvcuzan\\eyhb"
62+
"yaxr\"genlbgw\"\\uc"
63+
"nrgecjeip\\sjdvgqaqxwsqactopu"
64+
"pu\"r\"txpyrkfny\\zmwfneyvwmnkkdipv"
65+
"jm\xa3bhwvq"
66+
"qxojmnml\"w\x9airr"
67+
"xbzsuihs\x4dcedy\xaclrhgii\\\""
68+
"drgjirusrekrwmvxllwdm"
69+
"\x28hfxnfpycmpnkku\"csuf\xaarxlqyg\"x"
70+
"\"zvz\\rmg\"\\sxxoifffyqfyn\"iq\"ps"
71+
"\"z"
72+
"zbwkmk\"sgzos\x93gtc\""
73+
"bvm\x28aa\\\\\"pywuhaniox\\z\\hbp\xd7mold"
74+
"aszgvsyna"
75+
"qf\"vdwuss"
76+
"lnohni\"qwiacjsjegstlbfq\\kyjhyd"
77+
"c\\naawulxlqplnacvytspry\xf5ytxxqq"
78+
"razwqmsqgbaaxcd\\f"
79+
"radggyrjrg\"zx"
80+
"\"pu\x11t\\ajcjuieinlkvya"
81+
"veggiskh"
82+
"eglfhjxiet\"kouqfskwsy\"hpthsldel"
83+
"mv\xc1b\"f\\shrssnjwcpmurepdxdlcj"
84+
"dlayjd\"suvzotgdtc"
85+
"\xa9pvxeopn"
86+
"lpplsaxy\"oiwaq"
87+
"hqwh\\lusv"
88+
"hykykwlx\"\xa5atkgh\\d\x63dff"
89+
"vfktanpjy\"xxetc"
90+
"dnhwkgjnsmsswfuelvihvjl\"jtf"
91+
"x\"dwvzra\"nbbsewftehczgbvfzd\"rau"
92+
"csfi\"mzejnjqkqupwadrgti\"von"
93+
"xckf\xf7xsm\\pgvlpetjndpyblais\\z"
94+
"yecy\x6fuj\x58bwpgeuiw\"mdu"
95+
"fgb"
96+
"c\\lx\x3efthet\xfdelgvwvpem"
97+
"kgyrmarvfwjinlowt"
98+
"yzte"
99+
"vc\"z"
100+
"sxevqfzmmdwsuu\""
101+
"fxbaercmcy\xb6md"
102+
"f"
103+
"m\x44gqbcppho\\b"
104+
"gtafr\x57m\x11jy\"\"erwmmpiwjkbckuw"
105+
"ufdjt\"kssprzxqixzxmq\x58q"
106+
"yzbyo\"lfdbyaxexyfbnyv\\\xe8xmre"
107+
"u\x43ntr\\\\byyfjr\"iveujvnwsqbnpuvrta"
108+
"us\xf6bai"
109+
"c\\edh"
110+
"tzckolphexfq\\\x23\xfbdqv\\\"m"
111+
"yjafhbvhhj\x1b\"bplb"
112+
"\"o"
113+
"rubahvmp\""
114+
"qmkukrnrmqumh"
115+
"wdpxyvyidhwjf\\nabbijwhr\xc5bksvy\"p"
116+
"u\"prlpg\""
117+
"nsvcquyxbwilsxxemf\xd9leq"
118+
"y\xcetxuafl"
119+
"it"
120+
"kwdlysf\\xjpelae"
121+
"viwh\x58wpjjlnvryuti\x2chngrx\\nhtkui"
122+
"vhn\x9ehre\xc3ncsqbozms\"nl"
123+
"ytc\xa3mgeeogjcqavmmmd"
124+
"xzlexlitseozoxtpzzutfq"
125+
"cish\x07lmovj"
126+
"ekbflwqzaiivdr\"pq\\azrfbntqwkn"
127+
"uc\"xdbegmlmhksofzohavtrnxf"
128+
"xfdnrdqdrcjzbe"
129+
"ndg\"ckgrpisib\"rg\"p\\lmpfzlssnvk"
130+
"witfjwpbyyzlop"
131+
"zonlww\"emrbcsgdtrg\"rjzy\x64zqntlw"
132+
"dvgb\"zn\\vrbzema\"ckmd"
133+
"\\vdlmxhlvldk\"pmzazeip"
134+
"\"\"r"
135+
"rsntinv"
136+
"iy"
137+
"lr\x20efh"
138+
"csgexlb\"zqdavlxxhtdbh\"\"\x0fkpvhiphm"
139+
"ouwhp\"ogbft"
140+
"cm\\ckltng\"dw\x8brf\xf0eppgckd"
141+
"zmnlsgalhpkejsizfsbtnfliu\"nhc"
142+
"pnrkaayqvwpdjbhcrbb\"yfeq\"aq"
143+
"ozh\\hoxow\x2csrtr\\r\""
144+
"bqxabj\"u\\s"
145+
"cpsjti\"gy"
146+
"aa\"p\\nki\\ajijkqev"
147+
"q\"\"lfdentjgd\\"
148+
"bmokvpoebutfki"
149+
"pielvcbne\xf6efvzxn"
150+
"kx"
151+
"zlgmqagcrbhrwtwtmmg"
152+
"aiyhmntcqjbpv\xb5hhswxbryoedvos"
153+
"tdpaxrb"
154+
"fu\"\x7dttkyvhrlwko"
155+
"oirc\\\"cqlnqffjqt\\k"
156+
"edxlia\\tcyby"
157+
"jpeybgwfayerfrfbvfog\"ol"
158+
"ysr"
159+
"bzwzilgwfugjk"
160+
"tlcc\x75nukvwjgftetjcs\xaecwc"
161+
"dsqssa\"vzrf\"sewbp\\ahhlmhbeihlh"
162+
"qtgmjck\"n\"guki\"gmdivwqxismqj"
163+
"\"f"
164+
"wuorvlovucngbzdszqpikyk"
165+
"dfrdsacoukmgvhbq\"\"iwto"
166+
"\"ey\"ch\\wcgioe\\\"ouvligmsw"
167+
"ciqlszzgs"
168+
"\\tzyrkaoi\"sopjaq"
169+
"lmtnv"
170+
"ar\"fqoroigiertjjlm\"ymgi\\kkjewsxd"
171+
"wehcimlvudpxtamdn\"rwy"
172+
"hr\"zvrwthr\"vruzqfrldn\"b"
173+
"sggekodkiwvym\"mhsco"
174+
"ltlkfbrrdvk\\"
175+
"uut\"sfjnz\"\\ef"
176+
"hxilg\\"
177+
"zsredsiwlzrpedibn"
178+
"vtfi"
179+
"\\h"
180+
"qekfrc\xf6wduodbwrguqcng\\n"
181+
"\"lljlfdrxftwidn\\pkv\xd9ij"
182+
"mrvgqynpehkliuijlpp"
183+
"gikjph"
184+
"yoxcdrdt\"wbaurnyhoyxoihu"
185+
"onmomwuxuammbzxe"
186+
"rnrr\\twviz\x61gqaljr\x0dmtw"
187+
"r\"vupaoi"
188+
"l"
189+
"sei"
190+
"jwxtdtbkd\\kxd"
191+
"\x22v\\"
192+
"ahd"
193+
"j\"bjqxs"
194+
"\\i\x24gglxub\\nzsajokt"
195+
"lviwpu\"uxdlh\\zuy\"xqy\"ytdzlx\"r"
196+
"kptfmys"
197+
"fwxzikfhczkjwyjszqdbkepaeellc"
198+
"nlqpsvbrbd\\ns"
199+
"qryuwkjiodw\"\"vaqyq\"dmyifm"
200+
"tw\x15kdmaudjl\\zorhp\"alwh"
201+
"aatrvczesykekkjfyb\"kb"
202+
"usqcutbqbxxhucwxo\xc1ltb\"j\"bghjcvws"
203+
"ilhsrnzxkz"
204+
"bianqfdfdhvw"
205+
"hqibqs\x7ax\"qoxqoaqtcsz"
206+
"htxtoojbbauztwxuiq\\ngyfy\\obzc"
207+
"rxn\\moxlj"
208+
"mtus\x84erh\"dbe"
209+
"asx\x50huvsitcxadt"
210+
"\"bugggtnrc\"\"kl\"hmpu\x83hqrvhpo"
211+
"ewisbp\"\"vuzf\\w\x5fvalszdhl"
212+
"scusplpwxfnxu\x57\"zynpn\x99xerc\\ri"
213+
"m\\kinmkke\x0cl"
214+
"xhuzit\x7fd"
215+
"kfbo\x04\x50ruqirn"
216+
"t\"\"xpbdscmdoug"
217+
"punvpsgnbgyxe\"sptmpz"
218+
"bxukkazijr"
219+
"nxyrcdaoo\"rjkk\"wntehcvcip\"vrd"
220+
"rdpvqskmihqaw"
221+
"p\\gwdhtqnpgthod"
222+
"nwnuf\"\"yebycearom\"nqym\"\xd4sii\xccle"
223+
"alda\"ptspo\"wkkv\"zoi\"hkb\"vnntyd"
224+
"ixpgpfzbqv"
225+
"znui\"\\fzn\x03qozabh\"rva\"pv\x67"
226+
"e\"zswmwuk"
227+
"hcccygwfa"
228+
"ngmace\\rtyllolr\"\x68bw"
229+
"\\c\"jyufbry\"ryo\"xpo\x26ecninfeckh\\s"
230+
"hdnpngtuc\"dzbvvosn\x31fwtpzbrt"
231+
"hesbpd\xd4"
232+
"dsdbstuzrdfmrnyntufs\"dmv"
233+
"d\xeeibcwhcvkt"
234+
"fvzwrsfjdqdmy\"\"v"
235+
"ns\"dqafz\\lkyoflnazv\"mn\x37\"o\"yj\"e"
236+
"dypilgbwzccayxa\"bnmuernx"
237+
"q\xa9ztqrhreb\"\"kxfeyodqb"
238+
"iz\xa5qjxqulaawuwz\"rqmpcj\\yel"
239+
"z\"\\pq\"\"y\x67zpjtn"
240+
"ifxqvivp\"kiiftdoe"
241+
"jxzebj\"\x35\"qr\"ecglcutuoyywqumcs\"kk"
242+
"q"
243+
"yob\x85qmpuwexptczbkrl"
244+
"cjiavv\"uudpozvibyycnmxhxpxmpjoz"
245+
"xro\\uiqyrcid"
246+
"nod\\k"
247+
"d\"neiec"
248+
"tqyrqvwyvmz\\pzgzzcqsqsrgbqbtapoz"
249+
"r\"xvocpeuxfxslgueb\x05kzyyie\"aoec"
250+
"\"du\\uirlhcbgv\\cjqhfreqnvn"
251+
"zp\x04\x15\"pbjwhrjtmiba"
252+
"\\cv\""
253+
"k\"rwnb\\hiu\"rqd\"rc\\nyakrhly"
254+
"klrmafjzandiddodgz"
255+
"xipzhqzhvlpykzcuppx"
256+
"zdvrvn\xd0mtfvpylbn\\\\sxcznrzugwznl"
257+
"ody\\pvm\"kpjiudzhxazirgxzvumeat\"o"
258+
"kllvhdp\"prjikzrrc\"adgpegc\\\"gk"
259+
"sqtpug\xbcaauxaamw"
260+
"wegxxrrxdvpivrqievfeokmnojsk"
261+
"\\bo"
262+
"gijhz"
263+
"ylowluvabwrigssdgtxdwsiorxev\xdd"
264+
"\""
265+
"ghnsrnsqtxpygikahkrl"
266+
"\"rcfqkbjf\"sgxg\"vnd\\rotn"
267+
"ap\"smgsuexjrbuqs\"mpbstogj\"x"
268+
"koaunz\\sgt\"opv"
269+
"yialiuzwix"
270+
"yp\"ndxgwzml\"bt"
271+
"lpcjxmggfsy\\szbxccarjkqzasqkb\xcfd\x0c"
272+
"x"
273+
"mgakc"
274+
"vjieunoh\x73fjwx"
275+
"erbvv\"qulsd"
276+
"mimycrbfhqkarmz"
277+
"tihfbgcszuej\"c\xfbvoqskkhbgpaddioo"
278+
"mziavkwrmekriqghw"
279+
"izk\\tnjd\\ed\\emokvjoc"
280+
"c\"nhbqzndro\\g"
281+
"usfngdo"
282+
"aypljdftvptt"
283+
"ym\"afvq\xbcc"
284+
"zabi\"wjpvugwhl"
285+
"ebvptcjqjhc\"n\"p\"dxrphegr\\"
286+
"mzlqqxokhye\xd9\\rffhnzs"
287+
"hnipqknwpsjakanuewe"
288+
"rqgbfcjdrmiz\"h"
289+
"kzzp\\z\\txmkwaouxictybwx"
290+
"yzmspjkqrteiydswlvb"
291+
"gjpxklgpzv\"txri\\hotpuiukzzzd"
292+
"p\"rxergtbsxmjmkeeqwvoagnki\""
293+
"santipvuiq"
294+
"\"ihjqlhtwbuy\"hdkiv\"mtiqacnf\\"
295+
"oliaggtqyyx"
296+
"fwwnpmbb"
297+
"yrtdrieazfxyyneo"
298+
"nywbv\\"
299+
"twc\\ehfqxhgomgrgwpxyzmnkioj"
300+
"qludrkkvljljd\\xvdeum\x4e"

0 commit comments

Comments
 (0)