Last active
December 30, 2021 17:55
-
-
Save jkone27/ea84f9478f5b1c347b19787aab6c6f93 to your computer and use it in GitHub Desktop.
aoc 2021 day one in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#extras... | |
# why no Linq / extension methods in python? | |
# even if we wrap the nativ list in an object, we cannot chain methods vertically... | |
# so .map.map.map only works on one line :( | |
class List: | |
# is not private | |
# workarounds ... _ .. | |
# https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes | |
number = 5 | |
def __init__(self, _list): | |
self.wrapped = list(_list) | |
return None | |
def map(self, l): | |
return List(list(map(l, self.wrapped))) | |
def filter(self, l): | |
return List(list(filter(l, self.wrapped))) | |
x = List([1,2,3]) | |
x.number = 6 | |
x.map(lambda x:x+1).map(lambda x:x+2).wrapped | |
# multiline comment is handy ~ F# | |
k = ''' | |
{ "hello" : "json" } | |
''' | |
# dictionaries are objects (like js, no real dict<k,v>) | |
z = { "hello": 1 , "bye": "bye" } | |
g = dict(z) | |
g["bye"] | |
# wow, i wrote this in python and it works, not very happy about it | |
z = None and NotImplemented or 1 and "hello" | |
print(z) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import TYPE_CHECKING, Sequence | |
sample = [ 199, 200, 208,210, 200, 207, 240, 269, 260, 263] | |
sample[1:4] | |
sample[5:] | |
sample[:2] | |
len(sample) | |
def iterativeSol(inputList): | |
# for loop solution | |
results = [] | |
for i,x in enumerate(inputList): | |
if (i > 0): | |
prev = inputList[i-1] | |
if (x > prev): | |
results.append(x) | |
return len(results) | |
#revision after thinking of slicing | |
def iterativeSolWSlice(inputList): | |
# for loop solution | |
results = [] | |
for i,x in enumerate(inputList): | |
if i < (len(inputList) - 1): | |
fst,snd = inputList[i:i+2] | |
if snd > fst: | |
results.append(x) | |
return len(results) | |
def comprehensionSol(inputList): | |
# python doenst have anything like .map .filter .reduce | |
# but just list(map) list(filter) etc.. so always variables needs to be | |
# assigned... kind of verbose | |
# list comprehension seems the way to go (similar to F# comprehensions too) | |
res = [ | |
i | |
for index, i in enumerate(inputList) # enumerate gives access to index | |
if index > 0 and (inputList[index] > inputList[index - 1]) | |
] | |
return len(res) | |
print(iterativeSolWSlice([1,2,1,0,3,2])) | |
# part 1. | |
# read input | |
with open("input001.txt") as file: | |
lines = [(line.strip()) for line in file] | |
intLines = [] | |
for l in lines: | |
il = int(l) | |
intLines.append(il) | |
res = iterativeSol(intLines) | |
print(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import TYPE_CHECKING, Sequence | |
sample = [ 199, 200, 208,210, 200, 207, 240, 269, 260, 263] | |
sample[1:4] | |
sample[5:] | |
sample[:2] | |
len(sample) | |
# part 2. | |
#.. indexes pff | |
def iterativeSol2(inputList): | |
# for loop solution | |
results = [] | |
sums = [] | |
for i,x in enumerate(inputList): | |
list_len = len(inputList) | |
if (i - 2 >= 0): #take previous 3 | |
fst = x | |
snd = inputList[i-1] | |
trd = inputList[i-2] | |
sums.append(fst + snd + trd) | |
sums_len = len(sums) | |
if (sums_len > 1 and sums[sums_len-2] < sums[sums_len-1]): | |
results.append(x) | |
return len(results) | |
print(iterativeSol2(sample)) #5 | |
def comprehensionSol2(inputList): | |
sums = [ | |
sum(inputList[i:i+3]) | |
for i, _ in enumerate(inputList) # enumerate gives access to index | |
if i+3 <= len(inputList) | |
] | |
res = [] | |
for index,s in enumerate(sums): | |
if index > 0: | |
if s > sums[index-1]: | |
res.append(s) | |
return len(res) | |
print(comprehensionSol2(sample)) #5 | |
# read input | |
with open("input001.txt") as file: | |
lines = [(line.strip()) for line in file] | |
intLines = [] | |
for l in lines: | |
il = int(l) | |
intLines.append(il) | |
res = iterativeSol2(intLines) #1190 | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment