Skip to content

Commit

Permalink
Day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
aastrand committed Dec 11, 2023
1 parent f37c2f5 commit 5ce9a5f
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
89 changes: 89 additions & 0 deletions day11/day11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env python3

import sys

from utils import io
from utils.grid import Grid
from utils.math import manhattan_dist


def rot_90(lst):
return ["".join(list(reversed(x))) for x in zip(*lst)]


def parse(lines, padding=2):
verticals = []
for i in range(len(lines) - 1):
verticals.append(lines[i])
if set(list(lines[i])) == {"."}:
for _ in range(0, padding - 1):
verticals.append(lines[i])
verticals.append(lines[-1])

lines = rot_90(verticals)
horizontals = []
for i in range(len(lines) - 1):
horizontals.append(lines[i])
if set(list(lines[i])) == {"."}:
for _ in range(0, padding - 1):
horizontals.append(lines[i])
horizontals.append(lines[-1])
lines = rot_90(rot_90(rot_90(horizontals)))

grid = Grid.from_lines(lines)

galaxies = set()

def visitor(_, pos, val):
if val == "#":
galaxies.add(pos)

grid.walk(visitor)

return grid, galaxies


def solve(filename, padding=2):
_, galaxies = parse(io.get_lines(filename), padding)

sum = 0
galaxies = list(galaxies)
for i in range(0, len(galaxies)):
for j in range(i + 1, len(galaxies)):
sum += manhattan_dist(galaxies[i][0], galaxies[i][1], galaxies[j][0], galaxies[j][1])

return sum


def main():
assert solve("example.txt") == 374
assert solve("example.txt") == 374
assert solve("example.txt", 10) == 1030
print(solve("../input/2023/day11.txt")) # = 9565386

# part 2
# solve("example.txt", 20) # = 1850 => 1850 - 1030 = 820
# solve("example.txt", 30) # = 2670 => 2670 - 1850 = 820
# solve("example.txt", 40) # = 3490 => 3490 - 2670 = 820
# solve("example.txt", 50) # = 4310 => 4310 - 3490 = 820
# so example scales with 820 per 10
# example2 with 100:
# 90 / 10 = 9
# 9 * 820 = 7380
# 7380 + 1030 = 8410

# lets just do the math
# print(solve("../input/2023/day11.txt")) # = 9565386
# print(solve("../input/2023/day11.txt", 3)) # = 10423365
# print(solve("../input/2023/day11.txt", 10)) # = 16429218
# print(solve("../input/2023/day11.txt", 15)) # = 20719113
# increases linearly after a while, let's try 20
g1 = 20
g2 = 30
s1 = solve("../input/2023/day11.txt", g1) # = 25009008
s2 = solve("../input/2023/day11.txt", g2) # = 33588798
print(((1000000 - g2) // (g2 - g1)) * (s2 - s1) + s2)


if __name__ == "__main__":
sys.exit(main())
10 changes: 10 additions & 0 deletions day11/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
2 changes: 1 addition & 1 deletion input
Submodule input updated from b7b19f to ade222
17 changes: 17 additions & 0 deletions test/math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest

from utils.math import manhattan_dist, sign


class TestMath(unittest.TestCase):
def test_sign(self):
self.assertEqual(-1, sign(-4))
self.assertEqual(1, sign(8))
self.assertEqual(0, sign(0))

def test_manhattan_dist(self):
self.assertEqual(1 + 10, manhattan_dist(0, 4, -10, 3))


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions utils/math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def manhattan_dist(x1, y1, x2, y2):
return abs(x2 - x1) + abs(y2 - y1)


def sign(a):
return (a > 0) - (a < 0)

0 comments on commit 5ce9a5f

Please sign in to comment.