GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — AdventOfCode/languages/py/2023/06_WaitForIt.py at main · wolflu05/AdventOfCode

Expand file tree

/

06_WaitForIt.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
36 lines (27 loc) · 858 Bytes
/

06_WaitForIt.py

Copy path

File metadata and controls

36 lines (27 loc) · 858 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import math
from lib import *
inp = [l.split()[1:] for l in get_input()]
# x -> button hold time, T -> Time, D -> Distance record, nD -> Distance raced
# x*(T-x)=nD
# -x^2+Tx>D
# -x^2+Tx-D>0
# x=(T±sqrt(T^2-4*D))/(2)
def calculate_win_options(races):
res = 1
for t, r in races:
a = (t+math.sqrt(t**2-4*r)) / 2
b = (t-math.sqrt(t**2-4*r)) / 2
# if b is a whole number, we need to increase it by one so that it
# calculates if we have won, not also a draw
if int(b) == b:
b += 1
res *= math.ceil(a) - math.ceil(b)
return res
# --- Part 1
time = [int(a) for a in inp[0]]
distance = [int(a) for a in inp[1]]
print(calculate_win_options(zip(time, distance)))
# --- Part 2
time = int("".join(inp[0]))
distance = int("".join(inp[1]))
print(calculate_win_options([[time, distance]]))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.