====== Advent Of Code ====== * 사이트 링크: [[https://adventofcode.com/]] * 매년 12월 1일부터 25일까지 매일 한 문제씩 문제가 올라오는 사이트. Advent calendar을 모티브로 한다 * 2015년에 처음 시작되어서 지금까지 매년 이어져오고 있다. * 국내에서는 인지도가 똥망이지만, 해외에서는 꽤 인지도가 있는 듯 하다. * 문제마다 선착순 100등 안에 풀이를 제출하면, (100-등수) 만큼의 점수를 얻는다. 문제별 점수를 모은 총 점수로 랭킹이 나온다 ===== 연도별 문서 ===== * [[ps:advent_of_code:2023]] ===== 문제 스타일 ===== * 일반 PS/CP에서 등장하는 문제 스타일과는 조금 다르다. 따로 공부가 필요한 자료구조/알고리즘등을 사용해서 풀기보다는 기본적인 알고리즘 지식과 구현으로 풀어야 하는 문제들. PS하는 사람들보다는 일반 개발자를 위한 문제들에 가깝다 * 인풋 데이터가 제공되고, 로컬에서 데이터에 대한 답을 구해서, 정답을 입력하는 형식이다. 소스코드는 전혀 제출할 필요가 없다. * 인풋 데이터는 유저마다 다르게 주어진다. 따라서 로그인을 해야만 데이터를 받을 수 있다. * 인풋 데이터는 PS/CP에서 흔히 주어지는 '최대한 컴퓨터가 읽기 쉬운 형태로 포매팅된' 방식의 데이터가 아니다. 대부분 문제에서는 PS문제에서는 할 필요가 없었을 파싱 작업이 필요하다 ===== 유틸리티 코드 ===== * 역사가 오래된 이벤트다보니. 인풋파일을 다운받고 기본 템플릿 코드를 생성하는 자동화 코드들이 이미 인터넷에 많이 있다. * [[https://github.com/tomfran/advent-of-code-setup]] 를 기반으로 내가 만들어 쓰고 있는 코드는 아래와 같다. * 세션 쿠키값을 파일에 저장해놓고 읽어서 쓴다. 원본 프로젝트의 설명을 참고. import os import requests import sys FOLDER = './AdventOfCode/' YEAR = 2023 USER_AGENT = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' HEADERS = {'User-Agent': USER_AGENT} SESSION_ID_FILE = FOLDER + 'session.cookie' TEMPLATE = '''"""Solution code for "Advent of Code {0} Day {1}". - Problem link: https://adventofcode.com/{0}/day/{1} Tags: [XXX] """ def solve_part1(lines): pass def solve_part2(lines): pass def main(): with open('./AdventOfCode/{0}/day{1}.txt', encoding='utf-8') as file: lines = file.read().split('\\n') print('[PART 1]') solve_part1(lines) print('[PART 2]') solve_part2(lines) if __name__ == '__main__': main() ''' def get_session_id(filename): with open(filename, encoding='utf-8') as f: return f.read().strip() def get_url(year, day): return f'https://adventofcode.com/{year}/day/{day}/input' def get_cookies(): session = get_session_id(SESSION_ID_FILE) return {'session': session} def create_input_file(year, day): path = f'{FOLDER}{year}/day{day}.txt' if os.path.exists(path): print(path + ' already exists.') return url = get_url(year, day) cookies = get_cookies() response = requests.get(url, headers=HEADERS, cookies=cookies) if not response.ok: raise RuntimeError( f'Request failed\n\tstatus code: {response.status_code}\n\tmessage: {response.content}' ) with open(path, 'w', encoding='utf-8') as f: f.write(response.text[:-1]) def create_source_file(year, day): path = f'{FOLDER}{year}/day{day}.py' if os.path.exists(path): print(path + ' already exists.') return with open(path, 'w', encoding='utf-8') as f: f.write(TEMPLATE.format(year, day)) def main(): if len(sys.argv) != 2: year_and_day = input(f'Input day (or year/day, if year != {YEAR}): ') else: year_and_day = sys.argv[1] if '/' in year_and_day: year, day = year_and_day.split('/') else: year, day = YEAR, int(year_and_day) create_input_file(year, day) create_source_file(year, day) if __name__ == '__main__': main()