aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit 3f89885d8fad7f0b12490802a7e9ce3d32b62633 (patch)
parent 1738bec38d6ac146389f1926b6ef835f6f0662bd
Author: Alex Karle <alex@alexkarle.com>
Date:   Sun,  8 Dec 2024 13:58:22 +0100

2024: Add day 8

Diffstat:
A2024/08/1.py | 43+++++++++++++++++++++++++++++++++++++++++++
A2024/08/2.py | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/2024/08/1.py b/2024/08/1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +import sys +from collections import defaultdict + +def parse(): + ants = defaultdict(list) + i = 0; + for l in sys.stdin: + for j in range(len(l.strip())): + c = l[j] + if c != '.': + ants[c].append((i, j)) + i += 1 + return ants, i + +def onmap(i, j, n): + return i >= 0 and i < n and j >=0 and j < n + +def get_anodes(a, b, n): + dx = b[1] - a[1] + dy = b[0] - a[0] + cands = [ + (b[0] + dy, b[1] + dx), + (a[0] - dy, a[1] - dx) + ] + return filter(lambda x: onmap(x[0], x[1], n), cands) + +def count(ants, n): + locs = set() + for a in ants.keys(): + nodes = ants[a] + # pairwise step through and find new locs + for i in range(len(nodes)): + for j in range(i + 1, len(nodes)): + for l in get_anodes(nodes[i], nodes[j], n): + locs.add(l) + + return len(locs) + +if __name__ == '__main__': + # input is square nxn + ants, n = parse() + print(count(ants, n)) diff --git a/2024/08/2.py b/2024/08/2.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +import sys +from collections import defaultdict + +def parse(): + ants = defaultdict(list) + i = 0; + for l in sys.stdin: + for j in range(len(l.strip())): + c = l[j] + if c != '.': + ants[c].append((i, j)) + i += 1 + return ants, i + +def onmap(i, j, n): + return i >= 0 and i < n and j >=0 and j < n + +def get_anodes(a, b, n): + dx = b[1] - a[1] + dy = b[0] - a[0] + anodes = set() + for loc in [a, b]: + i, j = loc + while onmap(i, j, n): + anodes.add((i, j)) + i += dy + j += dx + i, j = loc + while onmap(i, j, n): + anodes.add((i, j)) + i -= dy + j -= dx + return anodes + +def count(ants, n): + locs = set() + for a in ants.keys(): + nodes = ants[a] + # pairwise step through and find new locs + for i in range(len(nodes)): + for j in range(i + 1, len(nodes)): + for l in get_anodes(nodes[i], nodes[j], n): + locs.add(l) + + return len(locs) + +if __name__ == '__main__': + # input is square nxn + ants, n = parse() + print(count(ants, n))