globmont

AoC 21-02

Dec 2nd, 2021 (edited)
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. data = pd.read_csv(
  4.     "inputs/2021/02/data.csv",
  5.     sep=" ",
  6.     header=None,
  7.     names=["direction", "n"],
  8.     dtype=dict(direction=str, n=int)
  9. )
  10.  
  11. # Part 1
  12. direction_counts = data.groupby("direction").n.sum()
  13. part_1 = (direction_counts["down"] - direction_counts["up"]) * direction_counts["forward"]
  14. print(f"Part 1: {part_1:,d}")
  15.  
  16. # Part 2
  17. data = data.pivot(columns="direction", values="n").fillna(0)
  18. data["aim"] = (data.down - data.up).cumsum()
  19. depth = (data.aim * data.forward).sum()
  20. horizontal = data.forward.sum()
  21.  
  22. part_2 = depth * horizontal
  23. print(f"Part 2: {part_2:,.0f}")
Add Comment
Please, Sign In to add comment