Skip to content

Commit 7a9a3f2

Browse files
author
Reza Shiri
committed
Add day-4
1 parent aeab7d6 commit 7a9a3f2

File tree

4 files changed

+2096
-0
lines changed

4 files changed

+2096
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type Period struct {
11+
start int
12+
end int
13+
}
14+
15+
func NewPeriod(start, end int) Period {
16+
return Period{start: start, end: end}
17+
}
18+
19+
func (p Period) Contains(other Period) bool {
20+
return p.start >= other.start && p.end <= other.end
21+
}
22+
23+
func main() {
24+
bytesRead, _ := ioutil.ReadFile("input.txt")
25+
fileContent := string(bytesRead)
26+
lines := strings.Split(fileContent, "\n")
27+
28+
var result int
29+
30+
for _, line := range lines {
31+
elves := strings.Split(line, ",")
32+
var periods []Period
33+
for _, elf := range elves {
34+
points := strings.Split(elf, "-")
35+
start, _ := strconv.Atoi(points[0])
36+
end, _ := strconv.Atoi(points[1])
37+
periods = append(periods, NewPeriod(start, end))
38+
}
39+
40+
if periods[0].Contains(periods[1]) || periods[1].Contains(periods[0]) {
41+
result += 1
42+
}
43+
}
44+
45+
fmt.Println(result)
46+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy