Advent of code: Difference between revisions

From miki
Jump to navigation Jump to search
(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...")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Some tips to solve https://adventofcode.com/.
Some tips to solve https://adventofcode.com/.

== Get input.txt ==

A Makefile rule:

<source lang="make">
YEAR=2024

input.txt: ../cookie.txt
DAY=$$(basename $$(pwd)); wget -O $@ --header="Cookie: $$(cat ../cookie.txt)" https://adventofcode.com/$(YEAR)/day/$${DAY##day}/input
</source>

Get content of {{file|../cookie.txt}} (collect using Firefox developers / storage panel):
<source lang="text">
session=aaaaaaaaaaaaaabbbbbbbbbbbbbbcccccccccccccddddddddddde1111111222222222333333333444444444555555555555
</source>


== I/O ==
== I/O ==
Line 69: Line 85:
|
|
<source lang="python">
<source lang="python">
print("Input:")
# Input:
# ------
for line in open(INPUT):
# ABC 3 FGH 4
print(line.strip())
# IZP 4 XYZ 3
</source>
# KIJ 2 FGH 5
|
# ABC 1 TYE 3
<source lang="text">
# FIS 3 FGH 9
Input:
ABC 3 FGH 4
# ABC 3 FGH 3

IZP 4 XYZ 3
KIJ 2 FGH 5
ABC 1 TYE 3
FIS 3 FGH 9
ABC 3 FGH 3
</source>
|-
|
<source lang="python">
INPUT2='small2.txt'
INPUT2='small2.txt'
L = []
L = []
Line 95: Line 103:
|
|
<source lang="text">
<source lang="text">
['ABC', '3', 'FGH', '4'],
[['ABC', '3', 'FGH', '4'],
['IZP', '4', 'XYZ', '3'],
['IZP', '4', 'XYZ', '3'],
['KIJ', '2', 'FGH', '5'],
['KIJ', '2', 'FGH', '5'],
Line 101: Line 109:
['FIS', '3', 'FGH', '9'],
['FIS', '3', 'FGH', '9'],
['ABC', '3', 'FGH', '3']]
['ABC', '3', 'FGH', '3']]
</source>
|-
||
<source lang="python">
# Input:
# Button A: X+27, Y+18
# Button B: X+17, Y+44
# Prize: X=8105, Y=9698

with open(INPUT) as f:
while True:
x_a,y_a=list(map(int,re.findall(r'Button A: X\+(\d+), Y\+(\d+)',f.readline())[0]))
x_b,y_b=list(map(int,re.findall(r'Button B: X\+(\d+), Y\+(\d+)',f.readline())[0]))
X,Y=list(map(int,re.findall(r'Prize: X=(\d+), Y=(\d+)',f.readline())[0]))
print(x_a,y_a)
print(x_b,y_b)
print(X,Y)
if not f.readline():
break
</source>
|
<source lang="text">
27 18
17 44
8105 9698
</source>
|-
|
<source lang="python">
# Input:
# 5,4
# 4,2
# 4,5
# 3,0
# 2,1

# Read the whole thing and extract only numbers, then reorg...
bytes = list(map(int,re.findall(r'\d+',open(INPUT).read())))
bytes = [(bytes[i],bytes[i+1]) for i in range(0,len(bytes),2)]
print(bytes)
</source>
|
<source lang="text">
[(5, 4), (4, 2), (4, 5), (3, 0), (2, 1)]
</source>
</source>
|}
|}

Latest revision as of 13:43, 18 December 2024

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

Get input.txt

A Makefile rule:

YEAR=2024

input.txt: ../cookie.txt
	DAY=$$(basename $$(pwd)); wget -O $@ --header="Cookie: $$(cat ../cookie.txt)" https://adventofcode.com/$(YEAR)/day/$${DAY##day}/input

Get content of ../cookie.txt (collect using Firefox developers / storage panel):

session=aaaaaaaaaaaaaabbbbbbbbbbbbbbcccccccccccccddddddddddde1111111222222222333333333444444444555555555555

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
# 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']]
# Input:
#   Button A: X+27, Y+18
#   Button B: X+17, Y+44
#   Prize: X=8105, Y=9698

with open(INPUT) as f:
    while True:
        x_a,y_a=list(map(int,re.findall(r'Button A: X\+(\d+), Y\+(\d+)',f.readline())[0]))
        x_b,y_b=list(map(int,re.findall(r'Button B: X\+(\d+), Y\+(\d+)',f.readline())[0]))
        X,Y=list(map(int,re.findall(r'Prize: X=(\d+), Y=(\d+)',f.readline())[0]))
        print(x_a,y_a)
        print(x_b,y_b)
        print(X,Y)
        if not f.readline():
            break
27 18
17 44
8105 9698
# Input:
#   5,4
#   4,2
#   4,5
#   3,0
#   2,1

# Read the whole thing and extract only numbers, then reorg...
bytes = list(map(int,re.findall(r'\d+',open(INPUT).read())))
bytes = [(bytes[i],bytes[i+1]) for i in range(0,len(bytes),2)]
print(bytes)
[(5, 4), (4, 2), (4, 5), (3, 0), (2, 1)]