Merge pull request #318 from microsoft/advent-of-code-2022

Episode 1 - problems 1-5
This commit is contained in:
Renee Noble 2022-12-06 16:20:50 +11:00 коммит произвёл GitHub
Родитель 1b96c08a8d 9918372cdb
Коммит 08eb3cfd2f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 273 добавлений и 0 удалений

Просмотреть файл

@ -1,5 +1,6 @@
# Welcome to *Advent of Code* Garden 2022
Over 3 episodes we'll be taking a look at the first half of this year's [Advent of Code](https://adventofcode.com/2022/).
We'll talk you through out tips and tricks, live code and demo different problems, and of course share our code with you here!
@ -8,6 +9,7 @@ We'll talk you through out tips and tricks, live code and demo different problem
If you want to join us on the leaderborad, we'd love to have you!
</br>
Use this code: 545432-5ac33587

Просмотреть файл

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

Просмотреть файл

@ -0,0 +1,16 @@
elves = [x.strip() for x in open("day1-data.txt").read().split("\n\n")]
elfInts = [sum([int(x) for x in e.split("\n")]) for e in elves]
elfInts.sort()
print(sum(elfInts[-3:]))
# text = open("day1-data.txt").read()
# elves = text.split("\n\n")
# cals = []
# for elf in elves:
# items = [int(i) for i in elf.split("\n")]
# cal_sum = sum(items)
# cals.append(cal_sum)
# print(max(cals))
# print(sum(sorted(cals)[-3:]))

Просмотреть файл

@ -0,0 +1,3 @@
A Y
B X
C Z

Просмотреть файл

@ -0,0 +1,77 @@
guide = [x.strip().split() for x in open("day2-data.txt").readlines()]
moveMapping = {
"A": "R",
"B": "P",
"C": "S",
"X": "R",
"Y": "P",
"Z": "S",
}
moveScores = {
"R": 1,
"P": 2,
"S": 3,
}
resultMapping = {
"RR": "D",
"PP": "D",
"SS": "D",
"RP": "W",
"PS": "W",
"SR": "W",
"RS": "L",
"PR": "L",
"SP": "L",
}
resultScores = {
"L": 0,
"D": 3,
"W": 6,
}
score = 0
for theirMove, myMove in guide:
game = moveMapping[theirMove] + moveMapping[myMove]
score += moveScores[game[-1]]
score += resultScores[resultMapping[game]]
print(score)
# Part 2
score = 0
for theirMove, myMove in guide:
game = moveMapping[theirMove] + moveMapping[myMove]
score += moveScores[game[-1]]
score += resultScores[resultMapping[game]]
print(score)
outcomeMapping = {
"X": "L",
"Y": "D",
"Z": "W",
}
playMapping = {
"RL": "S",
"RD": "R",
"RW": "P",
"PL": "R",
"PD": "P",
"PW": "S",
"SL": "P",
"SD": "S",
"SW": "R",
}
score = 0
for theirMove, outcome in guide:
game = moveMapping[theirMove] + outcomeMapping[outcome]
score += resultScores[game[-1]]
score += moveScores[playMapping[game]]
print(score)

Просмотреть файл

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

Просмотреть файл

@ -0,0 +1,68 @@
# Jacks answer
rucksacks = [x.strip() for x in open("day3-data.txt").readlines()]
def calcPriority(item):
if item.isupper():
return ord(item) - ord('A') + 27
else:
return ord(item) - ord('a') + 1
prioritySum = 0
for sack in rucksacks:
c1 = set(sack[:len(sack)//2])
c2 = set(sack[len(sack)//2:])
shared = list(c1.intersection(c2))[0]
prioritySum += calcPriority(shared)
print(prioritySum) # part 1
badgePrioritySum = 0
for i in range(0, len(rucksacks), 3):
group = (rucksacks[i], rucksacks[i+1], rucksacks[i+2])
badge = list(set(group[0]).intersection(set(group[1])).intersection(set(group[2])))[0]
badgePrioritySum += calcPriority(badge)
print(badgePrioritySum)
# from string import ascii_lowercase, ascii_uppercase
# def get_priority(letter):
# all_letters = ascii_lowercase + ascii_uppercase
# return all_letters.index(letter) + 1
# def part1():
# with open("day3.txt") as f:
# for line in f:
# line = line.strip()
# h1 = line[0:len(line)//2]
# h2 = line[len(line)//2:]
# for letter in h1:
# if letter in h2:
# print(get_priority(letter))
# total += get_priority(letter)
# break
# print(total)
# def part2():
# total = 0
# sacks = []
# with open("day3.txt") as f:
# for line in f:
# sack = line.strip()
# sacks.append(sack)
# for i in range(0, len(sacks), 3):
# for letter in sacks[i]:
# if letter in sacks[i + 1] and letter in sacks[i + 2]:
# total += get_priority(letter)
# break
# print(total)

Просмотреть файл

@ -0,0 +1,42 @@
pairRanges = [x.strip().split(',') for x in open("data.txt").readlines()]
# Jacks Answer
totalOverlaps = 0
anyOverlaps = 0
for a, b in pairRanges:
aStart, aStop = a.split('-')
bStart, bStop = b.split('-')
aRange = set([x for x in range(int(aStart), int(aStop)+1)])
bRange = set([x for x in range(int(bStart), int(bStop)+1)])
if aRange.issubset(bRange) or bRange.issubset(aRange):
totalOverlaps += 1
if len(aRange.intersection(bRange)) != 0:
anyOverlaps += 1
print(totalOverlaps)
print(anyOverlaps)
# Renee's answer
total = 0
with open("day4-data.txt") as f:
for line in f:
parts = line.split(",")
part_ranges = [tuple([int(i) for i in part.split("-")]) for part in parts]
print(part_ranges)
sorted_ranges = sorted(part_ranges, key=lambda x: (x[0], -x[1]))
if sorted_ranges[1][1] <= sorted_ranges[0][1]:
total += 1
#part
sorted_ranges = sorted(part_ranges, key=lambda x: (x[0], -x[1]))
if sorted_ranges[1][0] <= sorted_ranges[0][1]:
total += 1

Просмотреть файл

@ -0,0 +1,45 @@
import re
with open("day5-data.txt") as f:
text = f.read()
initial , moves = text.split("\n\n")
moves = moves.split("\n")
move_tuples = []
for move in moves:
x = re.search("move (\d+) from (\d+) to (\d+)", move)
move_tuples.append((int(x[1]), int(x[2]), int(x[3])))
print(initial)
stack_indexes = {i: 4 * (i - 1) + 1 for i in range(1,10)}
stacks = {i: [] for i in stack_indexes}
for row in initial.split("\n"):
if "1" not in row:
for l, i in stack_indexes.items():
if row[i] != " ":
stacks[l].append(row[i])
print(stacks)
for num, start, fin in move_tuples:
# for i in range(num):
slice1 = stacks[start][:num]
print(slice1)
remaining = stacks[start][num:]
stacks[start] = remaining
stacks[fin] = slice1 + stacks[fin]
print(stacks)
# print("".join([v[0] for v in stacks.values() if len(v)>0]))
print()
print()
print(stacks.values())
print()
for v in stacks.values():
# if len(v) > 0:
print(v[0], end="")