๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป CS

[Advent of Code] Day 1 Historian Hysteria

by dev.py 2024. 12. 1.

 

https://adventofcode.com/

 

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)