Es wird vmtl. wieder schnell genug komplizierter

Es wird vmtl. wieder schnell genug komplizierter
Code: Alles auswählen
seatID = lambda s : int(s.replace('F','0').replace('B','1').replace('L','0').replace('R','1'), 2)
ids = set(map(seatID, open('input.txt').read().splitlines()))
print(max(ids), (set(range(min(ids), max(ids))) - ids).pop())
Code: Alles auswählen
with open('input.txt') as file:
data = [line.strip() for line in file]
def partition(cmds, letter, end, start=0):
for char in cmds:
mid = (end - start) // 2
if char == letter:
end = mid + start
else:
start = start + mid + 1
return end
def seatid(line):
row = partition(line[:-3], 'F', 127)
col = partition(line[-3:], 'L', 7)
idd = row * 8 + col
return idd
# Part 1
maxid = max(seatid(line) for line in data)
print(maxid)
# Part 2
ids = {seatid(line) for line in data}
for i in range(maxid):
if not i in ids and (i + 1) in ids and (i - 1) in ids:
print(i)
Code: Alles auswählen
n=`sed 'y/FLBR/0011/;s/^/ibase=2;/'<f|sort|bc`;tail -n1<<<$n;comm -23 <(seq `sed -n '1p;$p'<<<$n`) <(<<<$n)
Code: Alles auswählen
n=`sed 'y/FLBR/0011/;s/^/ibase=2;/'<f|sort|bc`;tail -n1<<<$n;comm -23 <(seq `sed -n '1p;$p'<<<$n`) <(cat<<<$n)
Code: Alles auswählen
S=set(int(s.translate({70:48,66:49,76:48,82:49}),2)for s in open("input.txt"))
print(max(S),(set(range(min(S),max(S)))-S).pop())
Code: Alles auswählen
S=set(int(s.translate({70:48,66:49,76:48,82:49}),2)for s in open("input.txt"))
m=max(S);print(m,(set(range(min(S),m))-S).pop())
Code: Alles auswählen
# Tag 5
# https://adventofcode.com/2020/day/5
def main():
def decode_seat(seatcode):
row = int(seatcode[:7].replace('F', '0').replace('B', '1'), 2)
column = int(seatcode[-3:].replace('L', '0').replace('R', '1'), 2)
seat_id = row * 8 + column
return (row, column, seat_id)
# Tests
seats = [
'FBFBBFFRLR', # row 44, column 5, seat ID 357.
'BFFFBBFRRR', # row 70, column 7, seat ID 567.
'FFFBBBFRRR', # row 14, column 7, seat ID 119.
'BBFFBBFRLL' # row 102, column 4, seat ID 820.
]
print('tests: ', [decode_seat(seat) for seat in seats])
# Part 1
with open('input_day5.txt', 'r') as lines:
seat_codes = [x.strip('\n') for x in lines]
seat_ids = set(decode_seat(x)[2] for x in seat_codes)
print('Solution Part 1: ', max(seat_ids))
# Part 2
print('Solution Part 1: ', set(range(min(seat_ids), max(seat_ids)))-seat_ids)
if __name__ == "__main__":
main()
Code: Alles auswählen
# Tag 5
# https://adventofcode.com/2020/day/5
def decode_seat(seatcode):
seatcode = (
seatcode.replace("F", "0").replace("B", "1").replace("L", "0").replace("R", "1")
)
return (int(seatcode[:7], 2), int(seatcode[-3:], 2), int(seatcode, 2))
def main():
# Tests
seats = [
"FBFBBFFRLR", # row 44, column 5, seat ID 357.
"BFFFBBFRRR", # row 70, column 7, seat ID 567.
"FFFBBBFRRR", # row 14, column 7, seat ID 119.
"BBFFBBFRLL", # row 102, column 4, seat ID 820.
]
print("tests: ", [decode_seat(seat) for seat in seats])
# Part 1
with open("input_day5.txt", "r") as lines:
seat_codes = [x.strip("\n") for x in lines]
seat_ids = set(decode_seat(x)[2] for x in seat_codes)
print("Solution Part 1: ", max(seat_ids))
# Part 2
print("Solution Part 2: ", set(range(min(seat_ids), max(seat_ids))) - seat_ids)
if __name__ == "__main__":
main()
Code: Alles auswählen
10 TI$="000000":DIM A(25):R1=0:R2=0:GN=1:OPEN 2,8,2,"INPUT06,S,R"
20 PRINT"ASK GROUPS..."
100 IF ST<>0 THEN CLOSE 2:GOTO 9999
110 PRINT GN:PRINT"{UP}";:GN=GN+1:FOR I=0 TO 25:A(I)=0:NEXT:PC=0
200 REM ASK PERSON
210 INPUT#2,A$:IF ST<>0 OR A$="" THEN 300
220 PC=PC+1:FOR I=1 TO LEN(A$):J=ASC(MID$(A$,I,1))-65:A(J)=A(J)+1:NEXT:GOTO 200
300 REM EVALUATE GROUPS ANSWERS
310 FOR I=0 TO 25:N=A(I):IF N>0 THEN R1=R1+1
320 IF N=PC THEN R2=R2+1
330 NEXT:GOTO 100
9999 PRINT"PART 1:"R1:PRINT"PART 2:"R2:PRINT"TIME: "TI$
Code: Alles auswählen
n(){sed 'y/FLBR/0011/;s/^/ibase=2;/'<f|sort|bc};n|tail -1;n|comm -23 <(seq `n|sed -n '1p;$p'`) -
Code: Alles auswählen
import re
parse_bag = re.compile("[a-z]+ [a-z]+").match
parse_contents = re.compile("(\d+) ([a-z]+ [a-z]+)").findall
with open("day7/input.txt") as stream:
rules = {
parse_bag(line).group(): {
name: int(amount) for amount, name in parse_contents(line)
} for line in stream
}
def count_super_bags(needle):
return needle == "shiny gold" or any(map(count_super_bags, rules[needle]))
def count(needle):
return 1 + sum(amount * count(bag) for bag, amount in rules[needle].items())
print(sum(map(count_super_bags, rules)) - 1)
print(count("shiny gold") - 1)