Solved 2015/08 P1+P2

This commit is contained in:
2024-12-06 17:44:30 +01:00
parent 4274b0bb89
commit 99bb81be32
4 changed files with 56 additions and 23 deletions
+35 -2
View File
@@ -45,7 +45,40 @@ characters of string code (`2 + 5 + 10 + 6 = 23`) minus the total number
of characters in memory for string values (`0 + 3 + 7 + 1 = 11`) is
`23 - 11 = 12`.
To begin, [get your puzzle input](8/input).
Your puzzle answer was `1342`.
Answer:
## \-\-- Part Two \-\-- {#part2}
Now, let\'s go the other way. In addition to finding the number of
characters of code, you should now *encode each code representation as a
new string* and find the number of characters of the new encoded
representation, including the surrounding double quotes.
For example:
- `""` encodes to `"\"\""`, an increase from `2` characters to `6`.
- `"abc"` encodes to `"\"abc\""`, an increase from `5` characters to
`9`.
- `"aaa\"aaa"` encodes to `"\"aaa\\\"aaa\""`, an increase from `10`
characters to `16`.
- `"\x27"` encodes to `"\"\\x27\""`, an increase from `6` characters
to `11`.
Your task is to find *the total number of characters to represent the
newly encoded strings* minus *the number of characters of code in each
original string literal*. For example, for the strings above, the total
encoded length (`6 + 9 + 16 + 11 = 42`) minus the characters in the
original code representation (`23`, just like in the first part of this
puzzle) is `42 - 23 = 19`.
Your puzzle answer was `2074`.
Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2015) and
try another puzzle.
If you still want to see it, you can [get your puzzle
input](8/input).
+14 -13
View File
@@ -1,31 +1,25 @@
#!/bin/python3
import sys,re
import sys,re,ast,json
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile
input_f = 'test'
input_f = 'input'
part = 1
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
def parse(line):
line = get_re(r'^"(.*)"$',line)
tmp = line
if part == 1:
instructions = loadFile(input_f)
char = 0
total = 0
for i in instructions:
total += len(i)
print(i)
total += (len(i)-len(ast.literal_eval(i)))
print(total)
@@ -40,4 +34,11 @@ if part == 1:
# #
#########################################
if part == 2:
exit()
instructions = loadFile(input_f)
total = 0
for i in instructions:
total += (len(json.dumps(i)) - len(i))
print(total)