eh bored again

This commit is contained in:
a 2021-05-14 03:53:06 -05:00
parent 4960ab9dfe
commit 8566841ccc
4 changed files with 1397 additions and 2 deletions

View file

@ -1,12 +1,13 @@
list = File.read!("input.txt")
|> String.split("\n")
|> String.split(~r/\R/)
|> Enum.map(&String.to_integer/1)
ans1 = list
|> Enum.sum()
IO.puts(ans1)
reducer = fn i, {current, seen} -> frequency = current + i
reducer = fn i, {current, seen} ->
frequency = current + i
if MapSet.member?(seen, frequency) do
{:halt, frequency}
else

45
2018/01/01.py Normal file
View file

@ -0,0 +1,45 @@
with open("input.txt") as f:
lines = f.read().splitlines()
freqs = [int(line) for line in lines]
print("Part 1:", sum(freqs))
## what follows is a O(n log n) solution using modulos to avoid cycling manually.
## personally i just did it in elixir with Stream.cycle()...
from itertools import accumulate
sums = [0] + list(accumulate(freqs))
seen = set()
# check first cycle
for s in sums:
if s in seen:
ans2 = s
seen.add(s)
# check if last element is 0 (can't do mod 0)
tail = sums[-1]
if tail == 0:
ans2 = 0
# calculate mapping of all mods and indexes
sums = sums[:-1] # the last sum is part of the next iteration -- we start at 0
from collections import defaultdict
mods = defaultdict(list)
for i, s in enumerate(sums):
mods[s % tail].append((i,s))
mini, mind, minf = None, None, None
for m in mods.values():
ss = list(sorted(m, key = lambda t: t[1])) # sort by second element of tuple
for i in range(1,len(ss)):
diff = ss[i][1] - ss[i-1][1]
index = ss[i-1][0] if tail > 0 else ss[i][0]
freq = ss[i][1] if tail > 0 else ss[i-1][1]
if mind is None or diff < mind or (diff == mind and index < mini):
mini = index
mind = diff
minf = freq
ans2 = minf
print("Part 2:", ans2)

26
2018/03/03.py Normal file
View file

@ -0,0 +1,26 @@
with open("input.txt") as f:
lines = f.read().splitlines()
import re
claims = [[*map(int, re.findall(r'\d+', line))] for line in lines]
from collections import Counter
fabric = Counter(
(i,j)
for id, x, y, width, height in claims
for i in range(x,x+width)
for j in range(y,y+height)
)
ans1 = sum(1 for c in fabric.values() if c > 1) # more than 1 claim
print("Part 1:",ans1)
ans2 = next(
id
for id, x, y, width, height in claims
if all(fabric[(i,j)] == 1
for i in range(x,x+width)
for j in range(y,y+height)
)
)
print("Part 2:",ans2)

1323
2018/03/input.txt Normal file

File diff suppressed because it is too large Load diff