\[\Large\texttt{Advent Of Code 2024}
\]
\(\texttt{Intro}\)
这是一个从每年 12 月 1 日开始的为期 25 天的编程挑战,每天中午 1 pm 会公布一道题。
每道题会有两个部分,你需要用编程解决每一个部分。
本文会使用 C++
,Javascript
,Python
进行编程。
网址:https://adventofcode.com/2024
\(\texttt{Day 1}\)
\(\texttt{Part 1}\)
import rea=list(map(lambda it:list(map(int,re.findall("\\d+",it))),open(0).readlines()))
x,y=[_[0] for _ in a],[_[1] for _ in a]
x.sort()
y.sort()
print(sum(abs(x[i]-y[i]) for i in range(len(x))))
\(\texttt{Part 2}\)
import rea=list(map(lambda it:list(map(int,re.findall("\\d+",it))),open(0).readlines()))
x,y=[_[0] for _ in a],[_[1] for _ in a]
print(sum(i*y.count(i) for i in x))
\(\texttt{Day 2}\)
\(\texttt{Part 1}\)
a=list(map(lambda it:list(map(int,it.split())),open(0).readlines()))
tar=0
for it in a:if sorted(it)==it or sorted(it,reverse=True)==it :fl=1for i in range(1,len(it)):if abs(it[i]-it[i-1])<1 or abs(it[i]-it[i-1])>3 :fl=0tar+=fl
print(tar)
\(\texttt{Part 2}\)
def check(it):fl=Trueif sorted(it)==it or sorted(it,reverse=True)==it :for i in range(1,len(it)):if abs(it[i]-it[i-1])<1 or abs(it[i]-it[i-1])>3 :fl=Falseelse : fl=Falsereturn fla=list(map(lambda it:list(map(int,it.split())),open(0).readlines()))
tar=0
for it in a:fl=Falsefor i in range(len(it)):fl|=check(it[:i]+it[i+1:])tar+=int(fl)
print(tar)
\(\texttt{Day 3}\)
\(\texttt{Part 1}\)
import re
reg=r"mul\((\d+),(\d+)\)"
a=re.findall(reg,''.join(list(map(lambda it:it.strip(),open(0).readlines()))))
a=list(map(lambda it:re.match(reg,it),a))
a=list(map(lambda it:[int(it.group(1)),int(it.group(2))],a))
print(sum(i[0]*i[1] for i in a))
\(\texttt{Part 2}\)
import re
reg1=r"mul\(\d+,\d+\)|do\(\)|don't\(\)"
reg2=r"mul\((\d+),(\d+)\)|do\(\)|don't\(\)"
str=''.join(list(map(lambda it:it.strip(),open(0).readlines())))
a=re.findall(reg1,str)
enable,tar=True,0
# print(a)
for i in a:if i=="do()" : enable=Trueelif i=="don't()" : enable=Falseelse:if enable :it=re.match(reg2,i)tar+=int(it.group(1))*int(it.group(2))
print(tar)
\(\texttt{Day 4}\)
\(\texttt{Part 1}\)
const string base="XMAS";
const int dx[9]={0,-1,-1,-1,0,0,1,1,1};
const int dy[9]={0,-1,0,1,-1,1,-1,0,1};vector<string> a;
int n,m;bool check(int x,int y) { return x>=0&&x<n&&y>=0&&y<m; }void work()
{/* Code */string str; int tar=0;while(cin>>str) a.pb(str),n++,m=str.size();for(int i=0; i<n; i++)for(int j=0; j<m; j++){if(a[i][j]!='X') continue;for(int k=1; k<=8; k++){int xx=i+dx[k],yy=j+dy[k],fl=1;for(int l=1; l<4; l++,xx+=dx[k],yy+=dy[k])if(!check(xx,yy)||a[xx][yy]!=base[l])fl=0;tar+=fl;}}cout<<tar<<'\n';return;
}
\(\texttt{Part 2}\)
const string base="MAS";
const int dx[9]={0,-1,-1,1,1};
const int dy[9]={0,-1,1,-1,1};vector<string> a;
int n,m;bool check(int x,int y) { return x>=0&&x<n&&y>=0&&y<m; }void work()
{/* Code */string str; int tar=0;while(cin>>str) a.pb(str),n++,m=str.size();for(int i=0; i<n; i++)for(int j=0; j<m; j++){// if(a[i][j]!='A') continue;int sum=0;for(int k=1; k<=4; k++){int xx=i-dx[k],yy=j-dy[k],fl=1;for(int l=0; l<3; l++,xx+=dx[k],yy+=dy[k])if(!check(xx,yy)||a[xx][yy]!=base[l])fl=0;sum+=fl;}tar+=(sum>=2);}cout<<tar<<'\n';return;
}