Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenge 2019: Problem statement 1 solution #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)

func main() {
// open input.csv partnersFile
inputFile, err := os.Open("input.csv")
if err != nil {
log.Fatal(err)
}
// close inputFile at the end of main
defer inputFile.Close()

// create output.csv file
outputFile, err := os.Create("output.csv")
if err != nil {
log.Fatal(err)
}
// close output file at the end of main
defer outputFile.Close()

// create a csv writer for output file
outputFileWriter := csv.NewWriter(outputFile)
// flush writer at the end
defer outputFileWriter.Flush()

// read csv input.csv using a csv.Reader
inputFileCsvReader := csv.NewReader(inputFile)

// iterate over input and partners to minimum cost of delivery
for {
inputRecord, err := inputFileCsvReader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}

inputTheaterId := inputRecord[2]
deliverySize, err := strconv.Atoi(inputRecord[1])
if err != nil {
log.Fatal("Error while converting deliverySize string to int: ", err)
}

// open partners.csv partnersFile
partnersFile, err := os.Open("partners.csv")
if err != nil {
log.Fatal(err)
}
// read csv partner.csv using a csv.Reader
partnerFileCsvReader := csv.NewReader(partnersFile)

//skip first record
if _, err := partnerFileCsvReader.Read(); err != nil {
fmt.Println("Error reading first line of partner.csv: ", err)
}

minDeliveryCost := 0
isDeliveryPossible := false
deliveryPartner := ""

var outputRecord []string

// write deliveryId to output
outputRecord = append(outputRecord, inputRecord[0])

for {
partnerRecord, err := partnerFileCsvReader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}

sizeSlab := strings.Split(partnerRecord[1], "-")
sizeSlabMin, err := strconv.Atoi(strings.TrimSpace(sizeSlab[0]))
if err != nil {
log.Fatal(err)
}
sizeSlabMax, err := strconv.Atoi(strings.TrimSpace(sizeSlab[1]))
if err != nil {
log.Fatal(err)
}

if inputTheaterId == strings.TrimSpace(partnerRecord[0]) && (deliverySize >= sizeSlabMin && deliverySize <= sizeSlabMax) {
isDeliveryPossible = true
rate, err := strconv.Atoi(strings.TrimSpace(partnerRecord[3]))
if err != nil {
log.Fatal(err)
}
deliveryCost := deliverySize * rate
minCost, err := strconv.Atoi(strings.TrimSpace(partnerRecord[2]))
if err != nil {
log.Fatal(err)
}
if deliveryCost < minCost {
deliveryCost = minCost
}
if minDeliveryCost == 0 || deliveryCost < minDeliveryCost {
minDeliveryCost = deliveryCost
deliveryPartner = partnerRecord[4]
}
}
}
if !isDeliveryPossible {
outputRecord = append(outputRecord, fmt.Sprintf("%t", isDeliveryPossible))
outputRecord = append(outputRecord, "")
outputRecord = append(outputRecord, "")
} else {
outputRecord = append(outputRecord, fmt.Sprintf("%t", isDeliveryPossible), deliveryPartner, fmt.Sprintf("%d", minDeliveryCost))
}
partnersFile.Close()
outputFileWriter.Write(outputRecord)
}
}
4 changes: 4 additions & 0 deletions output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
D1,true,P1,2000
D2,true,P1,3250
D3,true,P3,15300
D4,false,,
4 changes: 0 additions & 4 deletions output1.csv

This file was deleted.

4 changes: 0 additions & 4 deletions output2.csv

This file was deleted.