| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | // For more tutorials: https://blog.learngoprogramming.com | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Copyright © 2018 Inanc Gumus | 
					
						
							|  |  |  | // Learn Go Programming Course | 
					
						
							|  |  |  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bufio" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 10:10:52 +03:00
										 |  |  | // result stores the parsed result for a domain | 
					
						
							|  |  |  | type result struct { | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 	domain string | 
					
						
							| 
									
										
										
										
											2019-04-26 10:10:52 +03:00
										 |  |  | 	visits int | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 	// add more metrics if needed | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | // parser keep tracks of the parsing | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | type parser struct { | 
					
						
							| 
									
										
										
										
											2019-04-26 10:10:52 +03:00
										 |  |  | 	sum     map[string]result // metrics per domain | 
					
						
							|  |  |  | 	domains []string          // unique domain names | 
					
						
							|  |  |  | 	total   int               // total visits for all domains | 
					
						
							|  |  |  | 	lines   int               // number of parsed lines (for the error messages) | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							| 
									
										
										
										
											2019-04-26 10:10:52 +03:00
										 |  |  | 	p := parser{sum: make(map[string]result)} | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Scan the standard-in line by line | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 	in := bufio.NewScanner(os.Stdin) | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 	for in.Scan() { | 
					
						
							|  |  |  | 		p.lines++ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Parse the fields | 
					
						
							|  |  |  | 		fields := strings.Fields(in.Text()) | 
					
						
							|  |  |  | 		if len(fields) != 2 { | 
					
						
							|  |  |  | 			fmt.Printf("wrong input: %v (line #%d)\n", fields, p.lines) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 10:10:52 +03:00
										 |  |  | 		domain := fields[0] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 		// Sum the total visits per domain | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 		visits, err := strconv.Atoi(fields[1]) | 
					
						
							|  |  |  | 		if visits < 0 || err != nil { | 
					
						
							|  |  |  | 			fmt.Printf("wrong input: %q (line #%d)\n", fields[1], p.lines) | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Collect the unique domains | 
					
						
							| 
									
										
										
										
											2019-04-26 01:20:41 +03:00
										 |  |  | 		if _, ok := p.sum[domain]; !ok { | 
					
						
							|  |  |  | 			p.domains = append(p.domains, domain) | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 		// Keep track of total and per domain visits | 
					
						
							|  |  |  | 		p.total += visits | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// You cannot assign to composite values | 
					
						
							| 
									
										
										
										
											2019-05-08 14:02:32 +03:00
										 |  |  | 		// p.sum[domain].visits += visits | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// create and assign a new copy of `visit` | 
					
						
							| 
									
										
										
										
											2019-04-26 10:10:52 +03:00
										 |  |  | 		p.sum[domain] = result{ | 
					
						
							| 
									
										
										
										
											2019-04-26 01:20:41 +03:00
										 |  |  | 			domain: domain, | 
					
						
							| 
									
										
										
										
											2019-04-26 10:10:52 +03:00
										 |  |  | 			visits: visits + p.sum[domain].visits, | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Print the visits per domain | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 	sort.Strings(p.domains) | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 	fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS") | 
					
						
							|  |  |  | 	fmt.Println(strings.Repeat("-", 45)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 01:20:41 +03:00
										 |  |  | 	for _, domain := range p.domains { | 
					
						
							| 
									
										
										
										
											2019-04-26 10:10:52 +03:00
										 |  |  | 		parsed := p.sum[domain] | 
					
						
							|  |  |  | 		fmt.Printf("%-30s %10d\n", domain, parsed.visits) | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Print the total visits for all domains | 
					
						
							| 
									
										
										
										
											2019-04-26 00:27:54 +03:00
										 |  |  | 	fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total) | 
					
						
							| 
									
										
										
										
											2019-04-27 10:45:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Let's handle the error | 
					
						
							|  |  |  | 	if err := in.Err(); err != nil { | 
					
						
							|  |  |  | 		fmt.Println("> Err:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-23 00:32:25 +03:00
										 |  |  | } |