Roderic Day vor 1 Jahr
Ursprung
Commit
5f2e215140
3 geänderte Dateien mit 45 neuen und 2 gelöschten Zeilen
  1. +7
    -2
      makefile
  2. +19
    -0
      y2023/p06.py
  3. +19
    -0
      y2023/p07.py

+ 7
- 2
makefile Datei anzeigen

@@ -7,7 +7,7 @@ CODE := $(shell find . -path "./y????/p??.*" -type f | xargs ls -rt | tail -n 1)
YEAR := $(shell echo ${CODE} | sed 's/[^0-9]/ /g' | cut -d' ' -f4)
DAY0 := $(shell echo ${CODE} | sed 's/[^0-9]/ /g' | cut -d' ' -f6)
DATA := ./y${YEAR}/p${DAY0}.dat
URL := https://adventofcode.com/${YEAR}/day/`echo ${DAY0} | bc`/input
URL := https://adventofcode.com/${YEAR}/day/`echo ${DAY0} | bc`

pyrun: ${DATA}
cat ${DATA} | docker run -v `pwd`:/app/ -w /app/ -i --rm python:latest python -u ./y${YEAR}/p${DAY0}.py
@@ -16,4 +16,9 @@ ${DATA}:
# avoid spam in the lead up to the event
test ${YEAR}${DAY0} -le `date +%Y%d`
# only poll if data isn't yet stored locally
test -f ${DATA} || curl -s -b "session=${SESSION}" ${URL} > ${DATA}
test -f ${DATA} || curl -s -b "session=${SESSION}" ${URL}/input > ${DATA}

# # avoid spam in the lead up to the event
# test ${YEAR}${DAY0} -le `date +%Y%d`
# # only poll if data isn't yet stored locally
# test -f ${DATA} || curl -s $(URL) | tr '\n' '@' | sed -E "s/.+<pre><code>//" | sed -E "s/<\/code><\/pre>.+//" | tr '@' '\n' > $(DATA)

+ 19
- 0
y2023/p06.py Datei anzeigen

@@ -0,0 +1,19 @@
import math


def combos(record, dist):
a, b, c = 1, -record, dist
x1, x2 = sorted([
(-b + (b ** 2 - 4 * a * c) ** 0.5) / (2 * a),
(-b - (b ** 2 - 4 * a * c) ** 0.5) / (2 * a),
])
return int(x2) - int(x1)


text = open(0).read()

records, distances = [[int(n) for n in line.split()[1:]] for line in text.splitlines()]
print(math.prod(combos(a, b)for a, b in zip(*[records, distances])))

records, distances = [[int(''.join(line.split()[1:]))] for line in text.splitlines()]
print(math.prod(combos(a, b)for a, b in zip(*[records, distances])))

+ 19
- 0
y2023/p07.py Datei anzeigen

@@ -0,0 +1,19 @@
import collections


def strength(hand, order, J_swap='J'):
counter = collections.Counter(hand.replace('J', J_swap))
count = collections.Counter(counter.values())
return [count[n] for n in [5, 4, 3, 2, 1]] + [order.index(k) for k in hand]


text = open(0).read()
data = [ln.split() for ln in text.splitlines()]

order = '123456789TJQKA'
hands = [(strength(hand, order), hand, score) for hand, score in data]
print(sum(rank * int(score) for rank, (_, hand, score) in enumerate(sorted(hands), 1)))

order = 'J123456789TQKA'
hands = [(max(strength(hand, order, J_swap) for J_swap in order[1:]), hand, score) for hand, score in data]
print(sum(rank * int(score) for rank, (_, hand, score) in enumerate(sorted(hands), 1)))

Laden…
Abbrechen
Speichern