Advent of Code 2024
adventofcode.com
์์ด ๋ ํด๋ ๋๋ฆฌ๊ณ , ์ฝ๋ฉ ์ค๋ ฅ๋ ๋๋ฆฌ๊ณ ๋ญํค๋นํค ์์ ๐
1๋ฒ ๋ฌธ์ ๋ ๋ ๊ฐ์ ๋ฆฌ์คํธ๋ฅผ ๊ฐ๊ฐ ์ ๋ ฌํ์ฌ, ๊ฐ ์ฐจ์ด๋ฅผ ํฉํ๋ฉด ๋๋ค.
result = 0
left = []
right = []
with open('input/01.txt','r') as file:
for line in file:
left_number, right_number = map(int, line.split())
left.append(left_number)
right.append(right_number)
left.sort()
right.sort()
for i in range(len(left)):
result += abs(left[i] - right[i])
print(result)
1๋ฒ ๋ฌธ์ ๋ฅผ ํ๋ฉด ๋ณ ํ๋๋ฅผ ํ๋ํ๊ณ , 2๋ฒ ๋ฌธ์ ์ ์ ๊ทผ์ด ๊ฐ๋ฅํด์ง๋ค.
์ญ์ฌ ์์ฅ๋์ ํ์ฒด๋ฅผ ์์๋ณด์ง ๋ชปํ๋ ํ์๋ค... ํ ์ ์์ด ์ ์ฌ ์ ์๋ฅผ ์๊ตฌํ๋ค.
์ด๋์ ๋ํ(Accompany) ํ์๊ณ ํ๊ตฌ๋..
๋ฐ๋ผ์จ ์ด์ ๊ทธ๋ค์ ์๊ตฌ๋ฅผ ๋ค์ด์ฃผ์
from collections import defaultdict
result = 0
left = []
right = defaultdict(int)
with open('input/02.txt','r') as file:
for line in file:
left_number, right_number = map(int, line.split())
left.append(left_number)
right[right_number] += 1
for number in left:
result += number * right[number]
print(result)