generated from public/aoc_template
Day 7
This commit is contained in:
parent
00534e682a
commit
a9c4dcad67
41
day05.py
41
day05.py
@ -3,9 +3,9 @@ from typing import Any
|
||||
|
||||
|
||||
def lookup(the_map: dict, source: int) -> int:
|
||||
for k, v in the_map.items():
|
||||
if k <= source < k + v['length']:
|
||||
return source - k + v['dest']
|
||||
for (s, l), d in the_map.items():
|
||||
if s <= source < s + l:
|
||||
return source - s + d
|
||||
return source
|
||||
|
||||
|
||||
@ -18,7 +18,7 @@ class Day(AOCDay):
|
||||
[
|
||||
(46, "input5_test"),
|
||||
(60294664, "input5"),
|
||||
]
|
||||
],
|
||||
]
|
||||
|
||||
def __init__(self, year: int, day: int):
|
||||
@ -48,30 +48,34 @@ class Day(AOCDay):
|
||||
for line in self.getInput():
|
||||
if not line:
|
||||
continue
|
||||
elif line.startswith('seeds:'):
|
||||
elif line.startswith("seeds:"):
|
||||
_, s = line.split(": ")
|
||||
seeds = list(map(int, s.split()))
|
||||
elif line.startswith('seed-to-soil'):
|
||||
elif line.startswith("seed-to-soil"):
|
||||
the_map = self.seed2soil
|
||||
elif line.startswith('soil-to-fertilizer'):
|
||||
elif line.startswith("soil-to-fertilizer"):
|
||||
the_map = self.soil2fertilizer
|
||||
elif line.startswith('fertilizer-to-water'):
|
||||
elif line.startswith("fertilizer-to-water"):
|
||||
the_map = self.fertilizer2water
|
||||
elif line.startswith('water-to-light'):
|
||||
elif line.startswith("water-to-light"):
|
||||
the_map = self.water2light
|
||||
elif line.startswith('light-to-temperature'):
|
||||
elif line.startswith("light-to-temperature"):
|
||||
the_map = self.light2temperature
|
||||
elif line.startswith('temperature-to-humidity'):
|
||||
elif line.startswith("temperature-to-humidity"):
|
||||
the_map = self.temperature2humidity
|
||||
elif line.startswith('humidity-to-location'):
|
||||
elif line.startswith("humidity-to-location"):
|
||||
the_map = self.humidity2location
|
||||
else:
|
||||
dest, source, length = map(int, line.split())
|
||||
the_map[source] = {'dest': dest, 'length': length}
|
||||
|
||||
if part2:
|
||||
self.seed_locations = {}
|
||||
the_map[(source, length)] = dest
|
||||
|
||||
self.seed_locations = {}
|
||||
last_min = 0
|
||||
for (source, length), dest in sorted(self.seed2soil.items()):
|
||||
if last_min < source:
|
||||
self.seed2soil[(last_min, source - last_min)] = 0
|
||||
last_min = source + length
|
||||
self.seed2soil[(last_min, 2**32)] = 0
|
||||
|
||||
return seeds
|
||||
|
||||
@ -80,10 +84,11 @@ class Day(AOCDay):
|
||||
return min(self.get_seed_location(s) for s in seeds)
|
||||
|
||||
def part2(self) -> Any:
|
||||
return
|
||||
seeds = self.parse_input()
|
||||
min_loc = 2**32
|
||||
for i in range(0, len(seeds), 2):
|
||||
for s in range(seeds[i], seeds[i] + seeds[i+1]):
|
||||
for s in range(seeds[i], seeds[i] + seeds[i + 1]):
|
||||
x = self.get_seed_location(s)
|
||||
if x < min_loc:
|
||||
min_loc = x
|
||||
@ -92,6 +97,6 @@ class Day(AOCDay):
|
||||
return min_loc
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
day = Day(2023, 5)
|
||||
day.run(verbose=True)
|
||||
|
||||
90
day07.py
Normal file
90
day07.py
Normal file
@ -0,0 +1,90 @@
|
||||
from collections import Counter
|
||||
from tools.aoc import AOCDay
|
||||
from typing import Any
|
||||
|
||||
|
||||
F_ORDER = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]
|
||||
|
||||
|
||||
class CardSet:
|
||||
def __init__(self, card_string: str, bid: str, part2: bool = False):
|
||||
self.bid = int(bid)
|
||||
self.cards = [F_ORDER.index(x) + 1 for x in card_string]
|
||||
if part2:
|
||||
for i, c in enumerate(self.cards):
|
||||
if c == 11:
|
||||
self.cards[i] = 1
|
||||
if not part2:
|
||||
self.strength = self.type_rank()
|
||||
else:
|
||||
self.strength = self.joker_type_rank()
|
||||
|
||||
def type_rank(self) -> int:
|
||||
card_set = set(self.cards)
|
||||
if len(card_set) == 5: # high card
|
||||
return 0
|
||||
elif len(card_set) == 4: # one pair
|
||||
return 1
|
||||
elif len(card_set) == 3: # two pair or three of a kind
|
||||
for c in self.cards:
|
||||
if self.cards.count(c) == 3:
|
||||
return 3
|
||||
return 2
|
||||
elif len(card_set) == 2: # full house or four of a kind
|
||||
for c in self.cards:
|
||||
if self.cards.count(c) == 4:
|
||||
return 5
|
||||
return 4
|
||||
else:
|
||||
return 6
|
||||
|
||||
def joker_type_rank(self) -> int:
|
||||
if 1 not in self.cards:
|
||||
return self.type_rank()
|
||||
|
||||
orig_cards = self.cards.copy()
|
||||
max_strength = 0
|
||||
for i in range(2, 15):
|
||||
self.cards = [x if x != 1 else i for x in self.cards]
|
||||
max_strength = max(max_strength, self.type_rank())
|
||||
self.cards = orig_cards.copy()
|
||||
return max_strength
|
||||
|
||||
def __lt__(self, other):
|
||||
if self.strength < other.strength:
|
||||
return True
|
||||
elif self.strength > other.strength:
|
||||
return False
|
||||
else:
|
||||
for i, c in enumerate(self.cards):
|
||||
if c < other.cards[i]:
|
||||
return True
|
||||
elif c > other.cards[i]:
|
||||
return False
|
||||
|
||||
|
||||
class Day(AOCDay):
|
||||
inputs = [
|
||||
[
|
||||
(6440, "input7_test"),
|
||||
(247815719, "input7"),
|
||||
],
|
||||
[
|
||||
(5905, "input7_test"),
|
||||
(248747492, "input7"),
|
||||
],
|
||||
]
|
||||
|
||||
def parse_input(self, part2: bool = False) -> list:
|
||||
return [CardSet(*line.split(), part2) for line in self.getInput()]
|
||||
|
||||
def part1(self) -> Any:
|
||||
return sum(c.bid * (i + 1) for i, c in enumerate(sorted(self.parse_input())))
|
||||
|
||||
def part2(self) -> Any:
|
||||
return sum(c.bid * (i + 1) for i, c in enumerate(sorted(self.parse_input(True))))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
day = Day(2023, 7)
|
||||
day.run(verbose=True)
|
||||
1000
inputs/input7
Normal file
1000
inputs/input7
Normal file
File diff suppressed because it is too large
Load Diff
5
inputs/input7_test
Normal file
5
inputs/input7_test
Normal file
@ -0,0 +1,5 @@
|
||||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
||||
Loading…
Reference in New Issue
Block a user