Advent of code

From miki
Revision as of 17:09, 7 December 2024 by Mip (talk | contribs) (Created page with "Some tips to solve https://adventofcode.com/. == I/O == === Read a list of numbers === {| class=wikitable |- ! Code !! Output |- | <source lang="python"> print("Input:") for line in open(INPUT): print(line.strip()) </source> | <source lang="text"> Input: 3 4 4 3 2 5 1 3 3 9 3 3 </source> |- | <source lang="python"> F=[] # Flat for line in open(INPUT): numbers = list(map(int,line.split())) F.extend(numbers) print(f"{F}") </source> | <source lang...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Some tips to solve https://adventofcode.com/.

I/O

Read a list of numbers

Code Output
print("Input:")
for line in open(INPUT):
    print(line.strip())
Input:
3   4
4   3
2   5
1   3
3   9
3   3
F=[] # Flat
for line in open(INPUT):
    numbers = list(map(int,line.split()))
    F.extend(numbers)
print(f"{F}")
[3, 4, 4, 3, 2, 5, 1, 3, 3, 9, 3, 3]
L=[] # List
for line in open(INPUT):
    numbers = list(map(int,line.split()))
    L.append(numbers)
print(f"{L}")
[[3, 4], 
 [4, 3], 
 [2, 5], 
 [1, 3], 
 [3, 9],
 [3, 3]]

Read a list of miscellaneous data

See also:

Code Output
print("Input:")
for line in open(INPUT):
    print(line.strip())
Input:
ABC  3  FGH  4 
IZP  4  XYZ  3 
KIJ  2  FGH  5 
ABC  1  TYE  3 
FIS  3  FGH  9 
ABC  3  FGH  3
INPUT2='small2.txt'
L = []
for line in open(INPUT2):
    matches = re.findall(r'([A-Z]+|\d+)',line)
    L.append(matches)
print(f"{L}")
['ABC', '3', 'FGH', '4'], 
 ['IZP', '4', 'XYZ', '3'], 
 ['KIJ', '2', 'FGH', '5'], 
 ['ABC', '1', 'TYE', '3'], 
 ['FIS', '3', 'FGH', '9'], 
 ['ABC', '3', 'FGH', '3']]