Compare commits
66 Commits
08b0b82100
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b989b8d68 | |||
| 4fa993c840 | |||
| 43fcaef247 | |||
| 65eafb1d56 | |||
| c27b981663 | |||
| 0c1fc940b3 | |||
| bc168219f3 | |||
| 9b098af432 | |||
| 62342c7aa9 | |||
| 86dc7fcebe | |||
| 6da7eb85a0 | |||
| e331b926eb | |||
| afa56835c4 | |||
| 07621181b0 | |||
| b1816dcbf8 | |||
| c82ee691a1 | |||
| a9801cc262 | |||
| edfc914c7b | |||
| 324a593c99 | |||
| 6bd452a4ec | |||
| e700358bda | |||
| d641552737 | |||
| f146c4e0a3 | |||
| e63c471b96 | |||
| 1d06422f87 | |||
| f96520e34a | |||
| 9400f23a40 | |||
| 37568edf3f | |||
| 65a5f421a0 | |||
| 279e82054e | |||
| f288097f2b | |||
| d7557ba116 | |||
| e2c0fba68c | |||
| 77ad783a6a | |||
| 081bf29b8c | |||
| 322d1bfefe | |||
| 81cd38b592 | |||
| 5849a1bab2 | |||
| eadc405a0f | |||
| aac89964d2 | |||
| b1928afa6c | |||
| baaba9b6da | |||
| 6c48e11b47 | |||
| c9314ad089 | |||
| b5badd44bd | |||
| d621db89ad | |||
| b5172e0f22 | |||
| 1c4ffaaf49 | |||
| 120a015fbf | |||
| b53e041014 | |||
| 5498f566a0 | |||
| f9ace9f63d | |||
| 51dd7d367a | |||
| 6b716bf476 | |||
| c5d4b8066b | |||
| 89afb3d530 | |||
| 25bf2b3234 | |||
| de49850989 | |||
| 4ae0d2af7b | |||
| e72d8fa268 | |||
| 6428567595 | |||
| ecb3f23dbe | |||
| fd0a43d8ff | |||
| 6a96972bae | |||
| a578c4df97 | |||
| 89bd320991 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1 +1,4 @@
|
||||
**/input.txt
|
||||
**/input*.txt
|
||||
**/test*.txt
|
||||
.DS_Store
|
||||
.env
|
||||
0
2022/.gitkeep
Normal file
0
2022/.gitkeep
Normal file
@@ -9,9 +9,9 @@ n = 0
|
||||
|
||||
for x in f:
|
||||
if x.strip():
|
||||
n = n + int(x)
|
||||
n += int(x)
|
||||
else:
|
||||
i = i+1
|
||||
i += 1
|
||||
elves.append(n)
|
||||
n = 0
|
||||
elves.append(n)
|
||||
@@ -11,7 +11,7 @@ for l in f:
|
||||
result = 'draw'
|
||||
v = choices[0]
|
||||
o = choices[1].strip()
|
||||
|
||||
|
||||
round = 0
|
||||
round2 = 0
|
||||
|
||||
@@ -23,7 +23,7 @@ for l in f:
|
||||
round2 = 1
|
||||
if o == 'Z':
|
||||
result = 'loss'
|
||||
round2=2
|
||||
round2 = 2
|
||||
|
||||
if v == 'B':
|
||||
if o == 'X':
|
||||
@@ -45,21 +45,19 @@ for l in f:
|
||||
if o == 'Z':
|
||||
round2 = 1
|
||||
|
||||
if o == 'X':
|
||||
round2 = round2 + 0
|
||||
if o == 'Y':
|
||||
round2 = round2 + 3
|
||||
round2 += 3
|
||||
if o == 'Z':
|
||||
round2 = round2 + 6
|
||||
round2 += 6
|
||||
|
||||
tot2 = tot2 + round2
|
||||
tot2 += round2
|
||||
|
||||
if o == 'X':
|
||||
round = round + 1
|
||||
round += 1
|
||||
if o == 'Y':
|
||||
round = round + 2
|
||||
round += 2
|
||||
if o == 'Z':
|
||||
round = round + 3
|
||||
round += 3
|
||||
|
||||
res = 3
|
||||
if result == 'loss':
|
||||
@@ -67,7 +65,7 @@ for l in f:
|
||||
if result == 'win':
|
||||
res = 6
|
||||
|
||||
tot = tot + round + res
|
||||
tot += round + res
|
||||
|
||||
print(tot)
|
||||
print(tot2)
|
||||
21
2022/03/c.py
Normal file
21
2022/03/c.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import os
|
||||
|
||||
rucksacks = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").read().splitlines()
|
||||
|
||||
tot, tot2 = 0, 0
|
||||
|
||||
priorities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
for data in rucksacks:
|
||||
tot += priorities.rfind(''.join(set(data[:len(data)//2])
|
||||
& set(data[len(data)//2:])))+1
|
||||
|
||||
print("First answer: " + str(tot))
|
||||
|
||||
for i in range(0, len(rucksacks), 3):
|
||||
three = rucksacks[i:i+3]
|
||||
tot2 += priorities.rfind(''.join(set(three[0])
|
||||
& set(three[1]) & set(three[2])))+1
|
||||
|
||||
print("Second answer: " + str(tot2))
|
||||
24
2022/04/d.py
Normal file
24
2022/04/d.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import os
|
||||
|
||||
pairs = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").read().splitlines()
|
||||
|
||||
tot1 = 0
|
||||
tot2 = 0
|
||||
|
||||
for p in pairs:
|
||||
a = p.split(',')[0].split('-')
|
||||
b = p.split(',')[1].split('-')
|
||||
|
||||
set_a = set(range(int(a[0]), int(a[1])+1))
|
||||
set_b = set(range(int(b[0]), int(b[1])+1))
|
||||
|
||||
if (set_a.issubset(set_b) or set_b.issubset(set_a)):
|
||||
tot1 += 1
|
||||
|
||||
if (set_a.intersection(set_b)):
|
||||
tot2 += 1
|
||||
|
||||
|
||||
print("First answer: " + str(tot1))
|
||||
print("Second answer: " + str(tot2))
|
||||
50
2022/05/e.py
Normal file
50
2022/05/e.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").read().split('\n\n')
|
||||
|
||||
stack_input = input[0].splitlines()
|
||||
moves = input[1].splitlines()
|
||||
|
||||
stacks = []
|
||||
|
||||
stack_input.pop()
|
||||
stack_input.reverse()
|
||||
|
||||
for i in [1, 5, 9, 13, 17, 21, 25, 29, 33]:
|
||||
temp = []
|
||||
for l in range(0, len(stack_input), 1):
|
||||
|
||||
if stack_input[l][i].isalpha():
|
||||
temp.append(stack_input[l][i])
|
||||
stacks.append(temp)
|
||||
|
||||
stacks2 = [row[:] for row in stacks]
|
||||
|
||||
for move in moves:
|
||||
move = move.split(' ')
|
||||
amount = int(move[1])
|
||||
move_from = int(move[3])
|
||||
move_to = int(move[5])
|
||||
i = 0
|
||||
while (i < amount):
|
||||
i += 1
|
||||
moved = stacks[move_from-1].pop()
|
||||
stacks[move_to-1].append(moved)
|
||||
|
||||
index = len(stacks2[move_from-1])-amount
|
||||
tomove = stacks2[move_from-1][index:]
|
||||
stacks2[move_from-1] = stacks2[move_from-1][:index]
|
||||
stacks2[move_to-1].extend(tomove)
|
||||
|
||||
res = ''
|
||||
for stack in stacks:
|
||||
res += stack[len(stack)-1]
|
||||
|
||||
print("First answer: " + res)
|
||||
|
||||
res2 = ''
|
||||
for stack in stacks2:
|
||||
res2 += stack[len(stack)-1]
|
||||
|
||||
print("Second answer: " + res2)
|
||||
15
2022/06/f.py
Normal file
15
2022/06/f.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").read()
|
||||
|
||||
|
||||
def findUniq(length: int):
|
||||
for i in range(len(input)):
|
||||
if len(set(input[i:i+length])) == length:
|
||||
return i+length
|
||||
return -1
|
||||
|
||||
|
||||
print("First answer: " + str(findUniq(4)))
|
||||
print("Second answer: " + str(findUniq(14)))
|
||||
97
2022/07/g.py
Normal file
97
2022/07/g.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").readlines()
|
||||
|
||||
|
||||
class Directory(object):
|
||||
def __init__(self, name: str, parent):
|
||||
self.name = name
|
||||
self.files = {}
|
||||
self.dirs = {}
|
||||
self.parent = parent
|
||||
|
||||
def add_file(self, name: str, size: int):
|
||||
self.files[name] = size
|
||||
|
||||
def add_directory(self, name, dir):
|
||||
self.dirs[name] = dir
|
||||
|
||||
def get_directory(self, dir: str):
|
||||
return self.dirs[dir]
|
||||
|
||||
def get_parent(self):
|
||||
return self.parent
|
||||
|
||||
def size(self):
|
||||
tot = 0
|
||||
for i in self.files.values():
|
||||
tot += int(i)
|
||||
for d in self.dirs.values():
|
||||
tot += int(d.size())
|
||||
return tot
|
||||
|
||||
def directories(self):
|
||||
return self.dirs.values()
|
||||
|
||||
def pwd(self):
|
||||
str = ''
|
||||
if self.parent != None:
|
||||
str = self.parent.pwd() + '/' + self.name
|
||||
return str
|
||||
|
||||
def __str__(self):
|
||||
return self.pwd() + " " + str(self.size())
|
||||
|
||||
|
||||
class Dictlist(dict):
|
||||
def __setitem__(self, key, value):
|
||||
try:
|
||||
self[key]
|
||||
except KeyError:
|
||||
super(Dictlist, self).__setitem__(key, [])
|
||||
self[key].append(value)
|
||||
|
||||
|
||||
dirs = {'/': Directory('/', None)}
|
||||
|
||||
|
||||
def parseLine(line: str):
|
||||
return line
|
||||
|
||||
|
||||
curdir = dirs['/']
|
||||
|
||||
for l in input:
|
||||
cmd = l.strip().split(' ')
|
||||
|
||||
if cmd[1] == "cd":
|
||||
if cmd[2] == '..':
|
||||
curdir = curdir.get_parent()
|
||||
elif cmd[2] != '/':
|
||||
curdir = curdir.get_directory(cmd[2])
|
||||
|
||||
if (cmd[0] == 'dir'):
|
||||
dirs[curdir.pwd()+cmd[1]] = Directory(cmd[1], curdir)
|
||||
curdir.add_directory(cmd[1], dirs[curdir.pwd()+cmd[1]])
|
||||
|
||||
if str(cmd[0]).isnumeric():
|
||||
curdir.add_file(cmd[1], cmd[0])
|
||||
|
||||
|
||||
tot = 0
|
||||
usedspace = dirs['/'].size()
|
||||
freespace = 70000000 - usedspace
|
||||
candidates = []
|
||||
|
||||
for i in dirs.values():
|
||||
dirsize = i.size()
|
||||
if dirsize <= 100000:
|
||||
tot += dirsize
|
||||
if (freespace + dirsize) > 30000000:
|
||||
candidates.append(dirsize)
|
||||
|
||||
print("Part 1: " + str(tot))
|
||||
|
||||
candidates.sort()
|
||||
print("Part 2: " + str(candidates[0]))
|
||||
88
2022/08/h.py
Normal file
88
2022/08/h.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").readlines()
|
||||
|
||||
trees = []
|
||||
|
||||
for l in input:
|
||||
l = l.strip()
|
||||
tmp = []
|
||||
for x in l:
|
||||
tmp.append([int(x), False])
|
||||
trees.append(tmp)
|
||||
|
||||
rows = len(trees)
|
||||
cols = len(trees[0])
|
||||
|
||||
|
||||
def calcScore(x: int, y: int):
|
||||
h = trees[y][x][0]
|
||||
ls, rs, us, ds = 1, 1, 1, 1
|
||||
|
||||
# right
|
||||
i = x+1
|
||||
while (i < cols-1 and trees[y][i][0] < h):
|
||||
i += 1
|
||||
rs += 1
|
||||
|
||||
# left
|
||||
i = x-1
|
||||
while (i > 0 and trees[y][i][0] < h):
|
||||
i -= 1
|
||||
ls += 1
|
||||
|
||||
# down
|
||||
i = y+1
|
||||
while (i < rows - 1 and trees[i][x][0] < h):
|
||||
i += 1
|
||||
ds += 1
|
||||
|
||||
# up
|
||||
i = y-1
|
||||
while (i > 0 and trees[i][x][0] < h):
|
||||
i -= 1
|
||||
us += 1
|
||||
|
||||
return ls*rs*us*ds
|
||||
|
||||
|
||||
scenic_scores = []
|
||||
|
||||
for x in range(0, cols, 1):
|
||||
height = 0
|
||||
for y in range(0, cols, 1):
|
||||
if trees[y][x][0] > height or (x == 0) or (y == 0) or (x == cols-1) or (y == rows-1):
|
||||
trees[y][x][1] = True
|
||||
height = trees[y][x][0]
|
||||
|
||||
height = 0
|
||||
for y in range(cols-1, -1, -1):
|
||||
if trees[y][x][0] > height or (x == 0) or (y == 0) or (x == cols-1) or (y == rows-1):
|
||||
trees[y][x][1] = True
|
||||
height = trees[y][x][0]
|
||||
|
||||
|
||||
for y in range(0, rows, 1):
|
||||
height = 0
|
||||
for x in range(0, cols, 1):
|
||||
if trees[y][x][0] > height or (x == 0) or (y == 0) or (x == cols-1) or (y == rows-1):
|
||||
trees[y][x][1] = True
|
||||
height = trees[y][x][0]
|
||||
if (x != 0) and (y != 0) and (x != cols-1) and (y != rows-1):
|
||||
scenic_scores.append(calcScore(x, y))
|
||||
|
||||
height = 0
|
||||
for x in range(cols-1, -1, -1):
|
||||
if trees[y][x][0] > height or (x == 0) or (y == 0) or (x == cols-1) or (y == rows-1):
|
||||
trees[y][x][1] = True
|
||||
height = trees[y][x][0]
|
||||
|
||||
tot = 0
|
||||
for y in range(0, rows, 1):
|
||||
for x in range(0, cols, 1):
|
||||
if trees[y][x][1]:
|
||||
tot += 1
|
||||
|
||||
print("Part 1: " + str(tot))
|
||||
print("Part 2: " + str(max(scenic_scores)))
|
||||
46
2022/09/i.py
Normal file
46
2022/09/i.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").readlines()
|
||||
|
||||
|
||||
def dir(d):
|
||||
match d:
|
||||
case 'R': return (1, 0)
|
||||
case 'L': return (-1, 0)
|
||||
case 'U': return (0, 1)
|
||||
case 'D': return (0, -1)
|
||||
|
||||
|
||||
max = 10
|
||||
|
||||
tails = []
|
||||
rope = [(0, 0) for i in range(max)]
|
||||
|
||||
for l in input:
|
||||
moves = l.split()
|
||||
m = dir(moves[0])
|
||||
for steps in range(int(moves[1])):
|
||||
rope[0] = [rope[0][0]+m[0], rope[0][1]+m[1]]
|
||||
for r in range(max-1):
|
||||
k, kn = rope[r:r+2]
|
||||
dx, dy = k[0]-kn[0], k[1] - kn[1]
|
||||
dist = abs(dx) + abs(dy)
|
||||
if dist == 2:
|
||||
if k[0] == kn[0]:
|
||||
rope[r+1] = (rope[r+1][0], rope[r+1][1]+dy//2)
|
||||
elif k[1] == kn[1]:
|
||||
rope[r+1] = (rope[r+1][0]+dx//2, rope[r+1][1])
|
||||
elif dist == 3:
|
||||
if abs(dx) == 2:
|
||||
rope[r+1] = (rope[r+1][0]+dx//2, rope[r][1])
|
||||
elif abs(dy) == 2:
|
||||
rope[r+1] = (rope[r][0], rope[r+1][1]+dy//2)
|
||||
elif dist == 4:
|
||||
rope[r+1] = (rope[r+1][0]+dx//2, rope[r+1][1]+dy//2)
|
||||
tails.append(rope[-1])
|
||||
|
||||
l = []
|
||||
for i in tails:
|
||||
l.append(str(i[0])+","+str(i[1]))
|
||||
print(len(set(l)))
|
||||
53
2022/10/j.py
Normal file
53
2022/10/j.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").readlines()
|
||||
|
||||
cost = {
|
||||
'noop': 1,
|
||||
'addx': 2
|
||||
}
|
||||
|
||||
H = 6
|
||||
W = 40
|
||||
|
||||
screen = [[False for i in range(W)] for j in range(H)]
|
||||
|
||||
cycle = 1
|
||||
X = 1
|
||||
strs = []
|
||||
line = 1
|
||||
|
||||
for l in input:
|
||||
cmd = l.split()
|
||||
for i in range(cost[cmd[0]]):
|
||||
pos = cycle % 40 - 1
|
||||
|
||||
# p1
|
||||
if ((cycle+20) % 40) == 0:
|
||||
strenth = cycle*X
|
||||
strs.append(strenth)
|
||||
|
||||
# p2
|
||||
if cycle % 40 == 0:
|
||||
line += 1
|
||||
if (pos == X-1 or pos == X or pos == X+1):
|
||||
screen[line-1][pos] = True
|
||||
|
||||
cycle += 1
|
||||
|
||||
if cmd[0] == 'addx':
|
||||
X += int(cmd[1])
|
||||
|
||||
# p1
|
||||
print(sum(strs))
|
||||
|
||||
# p2
|
||||
for y in range(H):
|
||||
for x in range(W):
|
||||
if (screen[y][x]):
|
||||
print("#", end="")
|
||||
else:
|
||||
print(".", end="")
|
||||
print()
|
||||
101
2022/11/k.py
Normal file
101
2022/11/k.py
Normal file
@@ -0,0 +1,101 @@
|
||||
import os
|
||||
|
||||
|
||||
class Operation:
|
||||
def __init__(self, operand, y: int):
|
||||
self.operand: str = operand
|
||||
self.y: int = y
|
||||
|
||||
def operate(self, input: int) -> int:
|
||||
i = int(input)
|
||||
j = 0
|
||||
if self.y == 'old':
|
||||
j = i
|
||||
else:
|
||||
j = int(self.y)
|
||||
if (self.operand == '+'):
|
||||
return i + j
|
||||
if (self.operand == '*'):
|
||||
return i * j
|
||||
|
||||
|
||||
class Monkey:
|
||||
def __init__(self, monkeyid: int, items, operation, testNumber: int,
|
||||
actionTrue: int, actionFalse: int):
|
||||
self.monkeyid = monkeyid
|
||||
self.items = items
|
||||
op = operation.split()
|
||||
self.operation = Operation(op[1], op[2])
|
||||
self.testNumber = testNumber
|
||||
self.actionTrue = int(actionTrue)
|
||||
self.actionFalse = int(actionFalse)
|
||||
self.inspections = 0
|
||||
|
||||
def inspect(self, worryDivider: int, p2: bool = False):
|
||||
self.inspections += 1
|
||||
item: int = self.items.pop(0)
|
||||
worryScore = self.operation.operate(item)//worryDivider
|
||||
|
||||
if (p2):
|
||||
worryScore %= lcm
|
||||
|
||||
if worryScore % self.testNumber == 0:
|
||||
return (self.actionTrue, worryScore)
|
||||
else:
|
||||
return (self.actionFalse, worryScore)
|
||||
|
||||
def __str__(self):
|
||||
return "Monkey " + str(self.monkeyid) + ": "+str(self.items)+" true: " \
|
||||
+ str(self.actionTrue)+" false: "+str(self.actionFalse) + \
|
||||
" inspect: "+str(self.inspections)
|
||||
|
||||
|
||||
monkeys = []
|
||||
lcm = 1
|
||||
|
||||
|
||||
def readMonkeys():
|
||||
global monkeys
|
||||
monkeys.clear()
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").read().split('\n\n')
|
||||
|
||||
for l in input:
|
||||
data = l.splitlines()
|
||||
monkeyid = data[0].split()[1][0]
|
||||
items = []
|
||||
itemline = data[1].strip().split(': ')
|
||||
for i in itemline[1].strip().split(', '):
|
||||
items.append(int(i))
|
||||
operation = data[2].split('=')[1]
|
||||
test = int(data[3].split()[3])
|
||||
optrue = int(data[4].strip().split()[5])
|
||||
opfalse = int(data[5].strip().split()[5])
|
||||
monkeys.append(
|
||||
Monkey(monkeyid, items, operation, test, optrue, opfalse))
|
||||
global lcm
|
||||
lcm = 1
|
||||
for m in monkeys:
|
||||
lcm = lcm*m.testNumber
|
||||
|
||||
|
||||
def process(rounds: int, div: int, p2: bool = False):
|
||||
readMonkeys()
|
||||
for round in range(1, rounds+1):
|
||||
for m in monkeys:
|
||||
while len(m.items) != 0:
|
||||
(nextmonkey, item) = m.inspect(div, p2)
|
||||
monkeys[nextmonkey].items.append(item)
|
||||
|
||||
tot = []
|
||||
|
||||
for m in monkeys:
|
||||
tot.append(m.inspections)
|
||||
|
||||
tot.sort()
|
||||
return tot[-1]*tot[-2]
|
||||
|
||||
|
||||
print("p1:", process(20, 3))
|
||||
print("p2:", process(10000, 1, True))
|
||||
70
2022/12/l.py
Normal file
70
2022/12/l.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import os
|
||||
import numpy
|
||||
from collections import deque
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").readlines()
|
||||
|
||||
start = ()
|
||||
end = ()
|
||||
|
||||
terrain = []
|
||||
for Y, y in enumerate(input):
|
||||
y = y.strip()
|
||||
cur = []
|
||||
for X, x in enumerate(y):
|
||||
h = ord(x)-ord('a')+1
|
||||
if (x == 'S'):
|
||||
h = 1
|
||||
start = (Y, X)
|
||||
if (x == 'E'):
|
||||
h = 26
|
||||
end = (Y, X)
|
||||
cur.append(h)
|
||||
terrain.append(cur)
|
||||
|
||||
H = len(terrain)
|
||||
W = len(terrain[0])
|
||||
|
||||
neighbours = [(-1, 0), (0, 1), (1, 0), (0, -1)]
|
||||
|
||||
|
||||
def find_path(coords):
|
||||
paths = []
|
||||
for n in neighbours:
|
||||
check = tuple(numpy.add(coords, n))
|
||||
if 0 <= check[0] < H and 0 <= check[1] < W and coords != check:
|
||||
if terrain[check[0]][check[1]] <= terrain[coords[0]][coords[1]]+1:
|
||||
paths.append(check)
|
||||
return paths
|
||||
|
||||
|
||||
def solve(p):
|
||||
Q = deque()
|
||||
|
||||
if (p == 2):
|
||||
for y in range(H):
|
||||
for x in range(W):
|
||||
if terrain[y][x] == 1:
|
||||
Q.append(((y, x), 0))
|
||||
else:
|
||||
Q.append((start, 0))
|
||||
|
||||
V = set()
|
||||
|
||||
while Q:
|
||||
cur_pos, d = Q.popleft()
|
||||
if cur_pos in V:
|
||||
continue
|
||||
V.add(cur_pos)
|
||||
if cur_pos == end:
|
||||
return d
|
||||
|
||||
tocheck = find_path(cur_pos)
|
||||
|
||||
for node in tocheck:
|
||||
Q.append((node, d+1))
|
||||
|
||||
|
||||
print(solve(1))
|
||||
print(solve(2))
|
||||
70
2022/13/m.py
Normal file
70
2022/13/m.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from functools import cmp_to_key
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").read().split('\n\n')
|
||||
|
||||
|
||||
def compare(a, b):
|
||||
typea = type(a)
|
||||
typeb = type(b)
|
||||
|
||||
if typea == int and typeb == int:
|
||||
if a < b:
|
||||
return -1
|
||||
elif a == b:
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
elif typea == list and typeb == list:
|
||||
i = 0
|
||||
while i < len(a) and i < len(b):
|
||||
res = compare(a[i], b[i])
|
||||
if res == 1:
|
||||
return 1
|
||||
if res == -1:
|
||||
return -1
|
||||
i += 1
|
||||
|
||||
if len(a) == i and i < len(b):
|
||||
return -1
|
||||
elif len(b) == i and i < len(a):
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
elif (typea == int and typeb == list):
|
||||
return compare([a], b)
|
||||
|
||||
elif (typeb == int and typea == list):
|
||||
return compare(a, [b])
|
||||
|
||||
|
||||
packets = []
|
||||
p1 = 0
|
||||
for idx, i in enumerate(input):
|
||||
a, b = i.split('\n')
|
||||
a = eval(a)
|
||||
b = eval(b)
|
||||
packets.append(a)
|
||||
packets.append(b)
|
||||
|
||||
if compare(a, b) == -1:
|
||||
p1 += idx+1
|
||||
|
||||
print("p1:", p1)
|
||||
|
||||
packets.append([[2]])
|
||||
packets.append([[6]])
|
||||
|
||||
packets = sorted(packets, key=cmp_to_key(lambda a, b: compare(a, b)))
|
||||
|
||||
p2 = 1
|
||||
|
||||
for idx, p in enumerate(packets):
|
||||
# print(p)
|
||||
if p == [[2]] or p == [[6]]:
|
||||
p2 *= idx+1
|
||||
|
||||
print("p2:", p2)
|
||||
71
2022/14/n.py
Normal file
71
2022/14/n.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").readlines()
|
||||
|
||||
cave = set()
|
||||
|
||||
for l in input:
|
||||
points = l.strip().split(' -> ')
|
||||
prev = (0, 0)
|
||||
for p in points:
|
||||
x, y = p.split(',')
|
||||
x = int(x)
|
||||
y = int(y)
|
||||
if prev == (0, 0):
|
||||
prev = (y, x)
|
||||
cave.add(prev)
|
||||
else:
|
||||
dy = y-prev[0]
|
||||
dx = x-prev[1]
|
||||
tmp = set()
|
||||
dir = 1
|
||||
if dy < 0 or dx < 0:
|
||||
dir = -1
|
||||
if dy != 0:
|
||||
for i in range(prev[0], prev[0]+dy+dir, dir):
|
||||
tmp.add((i, prev[1]))
|
||||
if dx != 0:
|
||||
for i in range(prev[1], prev[1]+dx+dir, dir):
|
||||
tmp.add((prev[0], i))
|
||||
cave.update(tmp)
|
||||
prev = (y, x)
|
||||
|
||||
|
||||
floor = max(r[0] for r in cave)+2
|
||||
for i in range(-20000, 20000):
|
||||
cave.add((floor, i))
|
||||
|
||||
p1 = False
|
||||
for s in range(10000000):
|
||||
X = 500
|
||||
Y = 0
|
||||
current = (Y, X)
|
||||
# print(current)
|
||||
|
||||
while (True):
|
||||
|
||||
down = (current[0]+1, current[1])
|
||||
left = (current[0]+1, current[1]-1)
|
||||
right = (current[0]+1, current[1]+1)
|
||||
|
||||
if (current[0]+1 >= floor and not p1):
|
||||
print("p1:", s)
|
||||
p1 = True
|
||||
|
||||
if down not in cave:
|
||||
current = down
|
||||
if current[0] >= floor:
|
||||
break
|
||||
elif left not in cave:
|
||||
current = left
|
||||
elif right not in cave:
|
||||
current = right
|
||||
else:
|
||||
break
|
||||
|
||||
if current == (0, 500):
|
||||
print("p2:", s+1)
|
||||
break
|
||||
|
||||
cave.add(current)
|
||||
48
2022/15/o.py
Normal file
48
2022/15/o.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").readlines()
|
||||
|
||||
beacons = []
|
||||
sensors = []
|
||||
distance = []
|
||||
|
||||
for line in input:
|
||||
l = line.strip().split()
|
||||
sx = int(l[2].split('=')[1].split(',')[0])
|
||||
sy = int(l[3].split('=')[1].split(':')[0])
|
||||
bx = int(l[8].split('=')[1].split(',')[0])
|
||||
by = int(l[9].split('=')[1])
|
||||
d = abs(bx-sx)+abs(by-sy)
|
||||
distance.append(d)
|
||||
beacons.append((bx, by))
|
||||
sensors.append((sx, sy))
|
||||
print(sx, sy, bx, by, d)
|
||||
|
||||
|
||||
def getcoverage(b, Y):
|
||||
coverage = set()
|
||||
(x, y) = sensors[b]
|
||||
d0 = distance[i]
|
||||
print("check", x, y, d0)
|
||||
for X in range(x-d0, x+d0):
|
||||
if (abs(x-X)+abs(y-Y)) <= d0:
|
||||
coverage.add((X, Y))
|
||||
return coverage
|
||||
|
||||
|
||||
testy = 2000000
|
||||
|
||||
fullcoverage = set()
|
||||
for i in range(len(sensors)):
|
||||
cov = getcoverage(i, testy)
|
||||
fullcoverage.update(cov)
|
||||
|
||||
testset = set()
|
||||
for s in fullcoverage:
|
||||
if s[1] == testy:
|
||||
testset.add(s)
|
||||
|
||||
nonbeacons = testset.difference(set(beacons))
|
||||
|
||||
print(len(nonbeacons))
|
||||
14
2022/16/p.py
Normal file
14
2022/16/p.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input-demo.txt", "r").readlines()
|
||||
|
||||
V = {}
|
||||
|
||||
for l in input:
|
||||
d = l.strip().split()
|
||||
V[d[1]] = (int(d[4].split('=')[1].split(';')[0]),
|
||||
d[9:])
|
||||
|
||||
|
||||
print(V)
|
||||
0
2023/.gitkeep
Normal file
0
2023/.gitkeep
Normal file
3
2023/01/go.mod
Normal file
3
2023/01/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.21.4
|
||||
44
2023/01/main.go
Normal file
44
2023/01/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
lines := parse()
|
||||
|
||||
var first = 0
|
||||
var last = 0
|
||||
|
||||
for _, line := range lines {
|
||||
fmt.Println(line)
|
||||
for i := 0; i < len(line); i++ {
|
||||
//fmt.Println(int(line[i]))
|
||||
x := strconv.Atoi(line[i])
|
||||
if x {
|
||||
first = x
|
||||
break
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(line); i++ {
|
||||
//fmt.Println(int(line[i]))
|
||||
x, err := strconv.Atoi(line[i])
|
||||
if err == nil {
|
||||
last = x
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func parse() []string {
|
||||
filePath := os.Args[1]
|
||||
data, _ := os.ReadFile(filePath)
|
||||
chunks := strings.Split(string(data), "\n")
|
||||
return chunks
|
||||
}
|
||||
5
2023/go.work
Normal file
5
2023/go.work
Normal file
@@ -0,0 +1,5 @@
|
||||
go 1.23.3
|
||||
|
||||
use (
|
||||
./01
|
||||
)
|
||||
3
2024/01/go.mod
Normal file
3
2024/01/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.23.3
|
||||
69
2024/01/main.go
Normal file
69
2024/01/main.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
lines := parse()
|
||||
|
||||
var left = []int{}
|
||||
var right = []int{}
|
||||
|
||||
for _, line := range lines {
|
||||
nums := strings.Fields(line)
|
||||
|
||||
l, _ := strconv.Atoi(nums[0])
|
||||
r, _ := strconv.Atoi(nums[1])
|
||||
left = append(left, l)
|
||||
right = append(right, r)
|
||||
|
||||
}
|
||||
|
||||
sort.Ints(left)
|
||||
sort.Ints(right)
|
||||
|
||||
var sum = 0
|
||||
|
||||
for i := 0; i < len(left); i++ {
|
||||
var diff = right[i] - left[i]
|
||||
if diff < 0 {
|
||||
diff *= -1
|
||||
}
|
||||
sum += diff
|
||||
}
|
||||
|
||||
var similarity = 0
|
||||
|
||||
for i := 0; i < len(left); i++ {
|
||||
var count = 0
|
||||
for j := 0; j < len(right); j++ {
|
||||
if left[i] == right[j] {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
similarity += count * left[i]
|
||||
}
|
||||
fmt.Println(sum)
|
||||
fmt.Println(similarity)
|
||||
|
||||
}
|
||||
|
||||
func parse() []string {
|
||||
filePath := os.Args[1]
|
||||
data, _ := os.ReadFile(filePath)
|
||||
chunks := strings.Split(string(data), "\n")
|
||||
var nonEmpthyChunks = []string{}
|
||||
|
||||
for _, chunk := range chunks {
|
||||
if chunk != "" {
|
||||
nonEmpthyChunks = append(nonEmpthyChunks, chunk)
|
||||
}
|
||||
}
|
||||
return nonEmpthyChunks
|
||||
}
|
||||
3
2024/02/go.mod
Normal file
3
2024/02/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.23.3
|
||||
115
2024/02/main.go
Normal file
115
2024/02/main.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
Increasing = 1
|
||||
Decreasing = -1
|
||||
)
|
||||
|
||||
func absInt(x int) int {
|
||||
return absDiffInt(x, 0)
|
||||
}
|
||||
|
||||
func absDiffInt(x, y int) int {
|
||||
if x < y {
|
||||
return y - x
|
||||
}
|
||||
return x - y
|
||||
}
|
||||
|
||||
func checkDirection(x int) int {
|
||||
switch {
|
||||
case x < 0:
|
||||
return Increasing
|
||||
case x > 0:
|
||||
return Decreasing
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func checklevel(report []int) bool {
|
||||
dir := checkDirection(report[0] - report[1])
|
||||
for i := 0; i < len(report)-1; i++ {
|
||||
zero := report[i]
|
||||
one := report[i+1]
|
||||
diff := zero - one
|
||||
if newDir := checkDirection(diff); newDir != dir {
|
||||
return false
|
||||
}
|
||||
absDiff := absInt(diff)
|
||||
if absDiff < 1 || absDiff > 3 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
lines, err := parse()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
|
||||
var safes = 0
|
||||
var safes2 = 0
|
||||
|
||||
for _, line := range lines {
|
||||
numbers := strings.Fields(line)
|
||||
report := make([]int, len(numbers))
|
||||
for i, num := range numbers {
|
||||
x, err := strconv.Atoi(num)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
report[i] = x
|
||||
}
|
||||
if checklevel(report) {
|
||||
safes++
|
||||
safes2++
|
||||
} else {
|
||||
var checks = 0
|
||||
for j := 0; j < len(report); j++ {
|
||||
copyArray := make([]int, len(report))
|
||||
copy(copyArray, report)
|
||||
report2 := append(copyArray[:j], copyArray[j+1:]...)
|
||||
if checklevel(report2) {
|
||||
checks++
|
||||
}
|
||||
}
|
||||
if checks >= 1 {
|
||||
safes2++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("p1: ", safes)
|
||||
fmt.Println("p2: ", safes2)
|
||||
}
|
||||
|
||||
func parse() ([]string, error) {
|
||||
if len(os.Args) < 2 {
|
||||
return nil, fmt.Errorf("no file provided")
|
||||
}
|
||||
filePath := os.Args[1]
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening file: %v", err)
|
||||
}
|
||||
chunks := strings.Split(string(data), "\n")
|
||||
var nonEmpthyChunks = []string{}
|
||||
|
||||
for _, chunk := range chunks {
|
||||
if chunk != "" {
|
||||
nonEmpthyChunks = append(nonEmpthyChunks, chunk)
|
||||
}
|
||||
}
|
||||
return nonEmpthyChunks, nil
|
||||
}
|
||||
3
2024/03/go.mod
Normal file
3
2024/03/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.23.3
|
||||
75
2024/03/main.go
Normal file
75
2024/03/main.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func calculate(x string) int {
|
||||
|
||||
clean1 := strings.Replace(x, "mul(", "", -1)
|
||||
clean2 := strings.Replace(clean1, ")", "", -1)
|
||||
splat := strings.Split(clean2, ",")
|
||||
|
||||
l, err := strconv.Atoi(splat[0])
|
||||
if err != nil {
|
||||
}
|
||||
r, err := strconv.Atoi(splat[1])
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
return l * r
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
lines, err := parse()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
oneline := strings.Join(lines, "")
|
||||
|
||||
var p1 int = 0
|
||||
var p2 int = 0
|
||||
|
||||
re := regexp.MustCompile(`mul\(\d+,\d+\)`)
|
||||
matches := re.FindAllString(oneline, -1)
|
||||
for i := 0; i < len(matches); i++ {
|
||||
p1 += calculate(matches[i])
|
||||
}
|
||||
|
||||
onecleanre := regexp.MustCompile(`(?s)don\'t\(\).*?do\(\)|$`)
|
||||
oneclean := onecleanre.ReplaceAllString(oneline, "")
|
||||
|
||||
re2 := regexp.MustCompile(`mul\(\d+,\d+\)`)
|
||||
matches2 := re2.FindAllString(oneclean, -1)
|
||||
for i := 0; i < len(matches2); i++ {
|
||||
p2 += calculate(matches2[i])
|
||||
}
|
||||
|
||||
fmt.Println("p1: ", p1)
|
||||
fmt.Println("p2: ", p2)
|
||||
}
|
||||
|
||||
func parse() ([]string, error) {
|
||||
if len(os.Args) < 2 {
|
||||
return nil, fmt.Errorf("no file provided")
|
||||
}
|
||||
filePath := os.Args[1]
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening file: %v", err)
|
||||
}
|
||||
chunks := strings.Split(string(data), "\n")
|
||||
var nonEmpthyChunks = []string{}
|
||||
|
||||
for _, chunk := range chunks {
|
||||
if chunk != "" {
|
||||
nonEmpthyChunks = append(nonEmpthyChunks, chunk)
|
||||
}
|
||||
}
|
||||
return nonEmpthyChunks, nil
|
||||
}
|
||||
3
2024/04/go.mod
Normal file
3
2024/04/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.23.3
|
||||
126
2024/04/main.go
Normal file
126
2024/04/main.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
lines, err := parse()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
|
||||
var p1 int = 0
|
||||
var p2 int = 0
|
||||
|
||||
h := len(lines)
|
||||
w := len(lines[0])
|
||||
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
|
||||
if lines[y][x] == 'X' {
|
||||
// check right
|
||||
if x <= len(lines[y])-4 {
|
||||
r := string([]byte{lines[y][x+1], lines[y][x+2], lines[y][x+3]})
|
||||
if r == "MAS" {
|
||||
p1++
|
||||
}
|
||||
}
|
||||
|
||||
// check left
|
||||
if x >= 3 {
|
||||
l := string([]byte{lines[y][x-1], lines[y][x-2], lines[y][x-3]})
|
||||
if l == "MAS" {
|
||||
p1++
|
||||
}
|
||||
}
|
||||
|
||||
// check if up possible
|
||||
if y >= 3 {
|
||||
u := string([]byte{lines[y-1][x], lines[y-2][x], lines[y-3][x]})
|
||||
if u == "MAS" {
|
||||
p1++
|
||||
}
|
||||
if x >= 3 {
|
||||
ul := string([]byte{lines[y-1][x-1], lines[y-2][x-2], lines[y-3][x-3]})
|
||||
if ul == "MAS" {
|
||||
p1++
|
||||
}
|
||||
}
|
||||
if x < w-3 {
|
||||
ur := string([]byte{lines[y-1][x+1], lines[y-2][x+2], lines[y-3][x+3]})
|
||||
if ur == "MAS" {
|
||||
p1++
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// check down
|
||||
if y <= h-4 {
|
||||
d := string([]byte{lines[y+1][x], lines[y+2][x], lines[y+3][x]})
|
||||
if d == "MAS" {
|
||||
p1++
|
||||
}
|
||||
if x >= 3 {
|
||||
dl := string([]byte{lines[y+1][x-1], lines[y+2][x-2], lines[y+3][x-3]})
|
||||
if dl == "MAS" {
|
||||
p1++
|
||||
}
|
||||
}
|
||||
if x < w-3 {
|
||||
dr := string([]byte{lines[y+1][x+1], lines[y+2][x+2], lines[y+3][x+3]})
|
||||
if dr == "MAS" {
|
||||
p1++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if lines[y][x] == 'A' {
|
||||
if y >= 1 && y < h-1 && x >= 1 && x < w-1 {
|
||||
check := string([]byte{lines[y-1][x-1], lines[y-1][x+1], lines[y+1][x-1], lines[y+1][x+1]})
|
||||
if check == "MSMS" {
|
||||
p2++
|
||||
}
|
||||
if check == "SMSM" {
|
||||
p2++
|
||||
}
|
||||
if check == "MMSS" {
|
||||
p2++
|
||||
}
|
||||
if check == "SSMM" {
|
||||
p2++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("p1: ", p1)
|
||||
fmt.Println("p2: ", p2)
|
||||
}
|
||||
|
||||
func parse() ([]string, error) {
|
||||
if len(os.Args) < 2 {
|
||||
return nil, fmt.Errorf("no file provided")
|
||||
}
|
||||
filePath := os.Args[1]
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening file: %v", err)
|
||||
}
|
||||
chunks := strings.Split(string(data), "\n")
|
||||
var nonEmpthyChunks = []string{}
|
||||
|
||||
for _, chunk := range chunks {
|
||||
if chunk != "" {
|
||||
nonEmpthyChunks = append(nonEmpthyChunks, chunk)
|
||||
}
|
||||
}
|
||||
return nonEmpthyChunks, nil
|
||||
}
|
||||
3
2024/05/go.mod
Normal file
3
2024/05/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.23.3
|
||||
102
2024/05/main.go
Normal file
102
2024/05/main.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func contains(s []int, e int) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func check(nums []int, maps map[int][]int) bool {
|
||||
correct := true
|
||||
for i := 0; i < len(nums); i++ {
|
||||
var checking = nums[i]
|
||||
for j := 0; j < len(nums); j++ {
|
||||
|
||||
if !contains(maps[checking], nums[j]) {
|
||||
if i < j {
|
||||
correct = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return correct
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
lines, err := parse()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
|
||||
var p1 int = 0
|
||||
var p2 int = 0
|
||||
|
||||
maps := make(map[int][]int)
|
||||
|
||||
for y := 0; y < len(lines); y++ {
|
||||
if lines[y] != "" && lines[y][2] == '|' {
|
||||
split := strings.Split(lines[y], "|")
|
||||
l, err := strconv.Atoi(split[0])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r, err := strconv.Atoi(split[1])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
maps[l] = append(maps[l], r)
|
||||
}
|
||||
|
||||
if lines[y] != "" && lines[y][2] == ',' {
|
||||
numsstr := strings.Split(lines[y], ",")
|
||||
var nums []int
|
||||
|
||||
for i := 0; i < len(numsstr); i++ {
|
||||
x, err := strconv.Atoi(numsstr[i])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
nums = append(nums, x)
|
||||
}
|
||||
|
||||
if check(nums, maps) {
|
||||
p1 += nums[len(nums)/2]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("p1: ", p1)
|
||||
fmt.Println("p2: ", p2)
|
||||
}
|
||||
|
||||
func parse() ([]string, error) {
|
||||
if len(os.Args) < 2 {
|
||||
return nil, fmt.Errorf("no file provided")
|
||||
}
|
||||
filePath := os.Args[1]
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening file: %v", err)
|
||||
}
|
||||
chunks := strings.Split(string(data), "\n")
|
||||
var nonEmpthyChunks = []string{}
|
||||
|
||||
for _, chunk := range chunks {
|
||||
if chunk != "" {
|
||||
nonEmpthyChunks = append(nonEmpthyChunks, chunk)
|
||||
}
|
||||
}
|
||||
return nonEmpthyChunks, nil
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
# Advent of Code 2022
|
||||
# Advent of Code
|
||||
|
||||
https://adventofcode.com/
|
||||
|
||||
24
testing/test.go
Normal file
24
testing/test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
lines := parse()
|
||||
|
||||
for _, line := range lines {
|
||||
fmt.Println(line)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func parse() []string {
|
||||
filePath := os.Args[1]
|
||||
data, _ := os.ReadFile(filePath)
|
||||
chunks := strings.Split(string(data), "\n")
|
||||
return chunks
|
||||
}
|
||||
4
testing/test.txt
Normal file
4
testing/test.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
3425 423
|
||||
9
utils/getpuzzle.sh
Executable file
9
utils/getpuzzle.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/../.env"
|
||||
|
||||
year=$1
|
||||
day=$2
|
||||
|
||||
echo "Getting input for $year day $day"
|
||||
|
||||
curl -s -S --cookie "$COOKIE" --output ./input.txt https://adventofcode.com/$year/day/$day/input
|
||||
Reference in New Issue
Block a user