Working on 2015/08 P1

This commit is contained in:
FrederikBaerentsen 2024-12-05 23:13:04 +01:00
parent 88b01fc9bd
commit 6a94713884
3 changed files with 114 additions and 6 deletions

View File

@ -27,20 +27,24 @@ def parse(lst:str):
t = get_re(r"^(.+) -> (.+)",lst)
return convert_list(['SET',t.group(1),t.group(2)])
def checkValue(x,values):
if x not in values and not isinstance(x, int):
return x
def v_return(x,values):
if isinstance(x, str):
if x not in values:
return 0 # the values is not 0 if it hasn't been used.
return # the values is not 0 if it hasn't been used.
else:
return values[x]
return x
if part == 1:
missing = []
instructions = loadFile(input_f)
values = {}
for i in instructions:
t = parse(i)
#print(i)
def parseValues(t):
if t[0] == 'SET':
values[t[2]] = v_return(t[1],values)
elif t[0] == 'AND':
@ -53,7 +57,17 @@ if part == 1:
values[t[3]] = v_return(t[1],values) >> v_return(t[2],values)
elif t[0] == 'NOT':
values[t[2]] = 65535-v_return(t[1],values)
#pprint(values)
for i in instructions:
t = parse(i)
print(t)
if isinstance(t[1], str) or isinstance(t[2], str):
if checkValue(t[1],values) is not None or checkValue(t[2],values) is not None:
missing.append(t)
else:
parseValues(t)
print(values)
print(missing)
#input()
pprint(values['a'])

51
2015/08/8.md Normal file
View File

@ -0,0 +1,51 @@
## \-\-- Day 8: Matchsticks \-\--
Space on the sleigh is limited this year, and so Santa will be bringing
his list as a digital copy. He needs to know how much space it will take
up when stored.
It is common in many programming languages to provide a way to
escape
special characters in strings. For example,
[C](https://en.wikipedia.org/wiki/Escape_sequences_in_C),
[JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String),
[Perl](http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators),
[Python](https://docs.python.org/2.0/ref/strings.html), and even
[PHP](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double)
handle special characters in very similar ways.
However, it is important to realize the difference between the number of
characters *in the code representation of the string literal* and the
number of characters *in the in-memory string itself*.
For example:
- `""` is `2` characters of code (the two double quotes), but the
string contains zero characters.
- `"abc"` is `5` characters of code, but `3` characters in the string
data.
- `"aaa\"aaa"` is `10` characters of code, but the string itself
contains six \"a\" characters and a single, escaped quote character,
for a total of `7` characters in the string data.
- `"\x27"` is `6` characters of code, but the string itself contains
just one - an apostrophe (`'`), escaped using hexadecimal notation.
Santa\'s list is a file that contains many double-quoted string
literals, one on each line. The only escape sequences used are `\\`
(which represents a single backslash), `\"` (which represents a lone
double-quote character), and `\x` plus two hexadecimal characters (which
represents a single character with that ASCII code).
Disregarding the whitespace in the file, what is *the number of
characters of code for string literals* minus *the number of characters
in memory for the values of the strings* in total for the entire file?
For example, given the four strings above, the total number of
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).
Answer:

43
2015/08/solution.py Normal file
View File

@ -0,0 +1,43 @@
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile
input_f = 'test'
part = 1
#########################################
# #
# 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)
print(total)
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
exit()