diff --git a/2024/14/14-1-brute.py b/2024/14/14-1-brute.py new file mode 100644 index 0000000..f06b969 --- /dev/null +++ b/2024/14/14-1-brute.py @@ -0,0 +1,67 @@ +import re + +testing = False # change this when doing the actual input +seconds = 100 + +tiles = [] + +def quadrantSafetyScore(startRow, endRow, startCol, endCol): + sum = 0 + for i in range(startRow, endRow): + for j in range(startCol, endCol): + sum += tiles[i][j] + return sum + +def move(position, velocity, secondsToGo): + if secondsToGo == 0: + return + + pX, pY = position + vX, vY = velocity + + newPosX = (pX + vX) % width + newPosY = (pY + vY) % height + + tiles[pY][pX] -= 1 + tiles[newPosY][newPosX] += 1 + + move((newPosX,newPosY), velocity, secondsToGo - 1) + +match testing: + case True: + inputPath = "2024/14/testinput.txt" + width = 11 + height = 7 + case False: + inputPath = "2024/14/input.txt" + width = 101 + height = 103 + +for i in range(height): + tiles.append([]) + for j in range(width): + tiles[i].append(0); + + +with open(inputPath) as f: + for line in f: + line = line.strip() + + regex = re.findall(r"(-*\d+)", line) + + startPosition = (int(regex[0]), int(regex[1])) + velocity = (int(regex[2]), int(regex[3])) + + pX, pY = startPosition + tiles[pY][pX] += 1 + move(startPosition, velocity, seconds) + +middleRow = int(height / 2) +middleCol = int(width / 2) + +q1 = quadrantSafetyScore(0, middleRow, 0, middleCol) +q2 = quadrantSafetyScore(0, middleRow, middleCol + 1, width) +q3 = quadrantSafetyScore(middleRow + 1, height, 0, middleCol) +q4 = quadrantSafetyScore(middleRow + 1, height, middleCol + 1, width) + +print(q1 * q2 * q3 * q4) \ No newline at end of file diff --git a/2024/14/14-1.py b/2024/14/14-1.py new file mode 100644 index 0000000..087390f --- /dev/null +++ b/2024/14/14-1.py @@ -0,0 +1,50 @@ +import re + +testing = False # change this when doing the actual input +seconds = 100 + +tiles = [] + +def quadrantSafetyScore(startRow, endRow, startCol, endCol): + sum = 0 + for i in range(startRow, endRow): + for j in range(startCol, endCol): + sum += tiles[i][j] + return sum + +match testing: + case True: + inputPath = "2024/14/testinput.txt" + width = 11 + height = 7 + case False: + inputPath = "2024/14/input.txt" + width = 101 + height = 103 + +for i in range(height): + tiles.append([]) + for j in range(width): + tiles[i].append(0); + + +with open(inputPath) as f: + for line in f: + line = line.strip() + + pX, pY, vX, vY = map(int, re.findall(r"(-*\d+)", line)) + + newPosX = (pX + vX * seconds) % width + newPosY = (pY + vY * seconds) % height + + tiles[newPosY][newPosX] += 1 + +middleRow = int(height / 2) +middleCol = int(width / 2) + +q1 = quadrantSafetyScore(0, middleRow, 0, middleCol) +q2 = quadrantSafetyScore(0, middleRow, middleCol + 1, width) +q3 = quadrantSafetyScore(middleRow + 1, height, 0, middleCol) +q4 = quadrantSafetyScore(middleRow + 1, height, middleCol + 1, width) + +print(q1 * q2 * q3 * q4) \ No newline at end of file diff --git a/2024/14/testinput.txt b/2024/14/testinput.txt new file mode 100644 index 0000000..72a324a --- /dev/null +++ b/2024/14/testinput.txt @@ -0,0 +1,12 @@ +p=0,4 v=3,-3 +p=6,3 v=-1,-3 +p=10,3 v=-1,2 +p=2,0 v=2,-1 +p=0,0 v=1,3 +p=3,0 v=-2,-2 +p=7,6 v=-1,-3 +p=3,0 v=-1,-2 +p=9,3 v=2,3 +p=7,3 v=-1,2 +p=2,4 v=2,-3 +p=9,5 v=-3,-3 \ No newline at end of file