From 7e4e536908c9b84d02ecbe0c61ad0f80732ca480 Mon Sep 17 00:00:00 2001 From: Rongkun Wang Date: Mon, 11 Dec 2023 16:25:22 +0100 Subject: [PATCH 1/2] day 11 py --- inputs/11_example.txt | 10 ++++++ solutions/11_example.txt | 2 ++ src/python/11_rk.py | 77 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 inputs/11_example.txt create mode 100644 solutions/11_example.txt create mode 100644 src/python/11_rk.py diff --git a/inputs/11_example.txt b/inputs/11_example.txt new file mode 100644 index 0000000..986aad4 --- /dev/null +++ b/inputs/11_example.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... diff --git a/solutions/11_example.txt b/solutions/11_example.txt new file mode 100644 index 0000000..d9f66ce --- /dev/null +++ b/solutions/11_example.txt @@ -0,0 +1,2 @@ +374 +82000210 diff --git a/src/python/11_rk.py b/src/python/11_rk.py new file mode 100644 index 0000000..45ce3fc --- /dev/null +++ b/src/python/11_rk.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import sys +import collections + +def parse(infile): + ''' + build the map + ''' + fullMap = [[c for c in line.strip()] for line in infile] + return fullMap + + +def dis(pos1, pos2, empty_row, empty_col, sol2 = False): + # empty col inside + dist = len([i for i in empty_row if (i - pos1[0]) * (i - pos2[0]) < 0]) + dist += len([i for i in empty_col if (i - pos1[1]) * (i - pos2[1]) < 0]) + + dist *= (1000000-1) if sol2 else 1 + # test case + # dist *= (10-1) if sol2 else 1 + # dist *= (100-1) if sol2 else 1 + + dist += abs(pos1[0] - pos2[0]) + dist += abs(pos1[1] - pos2[1]) + + print(pos1, pos2, dist) + return dist + + +def sol1(m): + height = len(m) + width = len(m[0]) + l_galaxy = [] + l_empty_row = [] + l_empty_col = [] + + for r, row_content in enumerate(m): + all_dot = True + for c, char in enumerate(row_content): + if char == "#": + l_galaxy.append((r, c)) + all_dot = False + if all_dot: + l_empty_row.append(r) + + for c in range(width): + all_dot = True + for r in range(height): + if m[r][c] == "#": + all_dot = False + if all_dot: + l_empty_col.append(c) + + + print(l_empty_row) + print(l_empty_col) + s = 0 + s2 = 0 + for i in range(len(l_galaxy)): + for j in range(i+1, len(l_galaxy)): + s += dis(l_galaxy[i], l_galaxy[j], l_empty_row, l_empty_col) + s2 += dis(l_galaxy[i], l_galaxy[j], l_empty_row, l_empty_col, True) + pass + pass + return s, s2 + + +def main(): + with open(sys.argv[1], 'r') as infile: + m = parse(infile) + # print(m) + sol = sol1(m) + print(sol) + # print(sol[1]) + pass + +main() From 2e5d204fdd8056dbb27e65e8be5b9d79159dae74 Mon Sep 17 00:00:00 2001 From: Rongkun Wang Date: Mon, 11 Dec 2023 16:26:04 +0100 Subject: [PATCH 2/2] remove/format print --- src/python/11_rk.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/python/11_rk.py b/src/python/11_rk.py index 45ce3fc..fa375bc 100644 --- a/src/python/11_rk.py +++ b/src/python/11_rk.py @@ -23,7 +23,7 @@ def dis(pos1, pos2, empty_row, empty_col, sol2 = False): dist += abs(pos1[0] - pos2[0]) dist += abs(pos1[1] - pos2[1]) - print(pos1, pos2, dist) + # print(pos1, pos2, dist) return dist @@ -52,8 +52,8 @@ def sol1(m): l_empty_col.append(c) - print(l_empty_row) - print(l_empty_col) + # print(l_empty_row) + # print(l_empty_col) s = 0 s2 = 0 for i in range(len(l_galaxy)): @@ -70,8 +70,8 @@ def main(): m = parse(infile) # print(m) sol = sol1(m) - print(sol) - # print(sol[1]) + print(sol[0]) + print(sol[1]) pass main()