상세 컨텐츠

본문 제목

[Codility] ParkingBill (Go)

Development/Algorithm

by thisisnew 2023. 1. 3. 01:01

본문

반응형

https://app.codility.com/programmers/trainings/5/parking_bill/start/

 

Codility

Your browser is not supported Please, update your browser or switch to a different one. Learn more about what browsers are supported

app.codility.com

 

Task description

You parked your car in a parking lot and want to compute the total cost of the ticket. The billing rules are as follows:

  • The entrance fee of the car parking lot is 2;
  • The first full or partial hour costs 3;
  • Each successive full or partial hour (after the first) costs 4.

You entered the car parking lot at time E and left at time L. In this task, times are represented as strings in the format "HH:MM" (where "HH" is a two-digit number between 0 and 23, which stands for hours, and "MM" is a two-digit number between 0 and 59, which stands for minutes).

Write a function:

func Solution(E string, L string) int

that, given strings E and L specifying points in time in the format "HH:MM", returns the total cost of the parking bill from your entry at time E to your exit at time L. You can assume that E describes a time before L on the same day.

For example, given "10:00" and "13:21" your function should return 17, because the entrance fee equals 2, the first hour costs 3 and there are two more full hours and part of a further hour, so the total cost is 2 + 3 + (3 * 4) = 17. Given "09:42" and "11:42" your function should return 9, because the entrance fee equals 2, the first hour costs 3 and the second hour costs 4, so the total cost is 2 + 3 + 4 = 9.

Assume that:

  • strings E and L follow the format "HH:MM" strictly;
  • string E describes a time before L on the same day.

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.


package solution

import (
	"time"
)

const parkingTimeLayout = "15:04"

func Solution(E string, L string) int {
    var result = 2

	eTime, _ := time.Parse(parkingTimeLayout, E)
	lTime, _ := time.Parse(parkingTimeLayout, L)
	difMin := lTime.Sub(eTime).Minutes()

	var isFirstTime = false
	var total int

	for difMin > 0 {
		if !isFirstTime {
			result += 3
			difMin -= 60
			isFirstTime = true
			continue
		}

		total++
		difMin -= 60
	}

	result += total * 4

	return result
}

반응형

'Development > Algorithm' 카테고리의 다른 글

[Codility] StrSymmetryPoint (Go)  (0) 2023.01.05
[Codility] ParityDegree (Go)  (0) 2023.01.04
[Codility] BinaryGap (Go)  (0) 2023.01.02
[백준] 2154번 수 이어 쓰기 3 (Go)  (2) 2022.12.30
[백준] 2804번 크로스워드 만들기 (Go)  (0) 2022.12.28

관련글 더보기

댓글 영역