File tree Expand file tree Collapse file tree 5 files changed +2327
-0
lines changed Expand file tree Collapse file tree 5 files changed +2327
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "bufio"
5
+ "fmt"
6
+ "os"
7
+ "sort"
8
+ "strconv"
9
+ )
10
+
11
+ func main () {
12
+ elfs := readElfs ("../../input/day1.txt" )
13
+
14
+ sort .Sort (sort .Reverse (sort .IntSlice (elfs )))
15
+
16
+ fmt .Printf ("part 1: %v\n " , elfs [0 ])
17
+
18
+ sum := 0
19
+ for _ , c := range elfs [0 :3 ] {
20
+ sum += c
21
+ }
22
+
23
+ fmt .Printf ("part 2: %v\n " , sum )
24
+ }
25
+
26
+ func readElfs (name string ) []int {
27
+ elfs := []int {0 }
28
+ currentElf := & elfs [0 ]
29
+
30
+ fileByLine (name , func (line string ) {
31
+ if line == "" {
32
+ elfs = append (elfs , 0 )
33
+ currentElf = & elfs [len (elfs )- 1 ]
34
+ } else {
35
+ cals , _ := strconv .Atoi (line )
36
+ * currentElf += cals
37
+ }
38
+ })
39
+ return elfs
40
+ }
41
+
42
+ func fileByLine (name string , handler func (line string )) {
43
+ file , err := os .Open (name )
44
+ if err != nil {
45
+ panic (err )
46
+ }
47
+ defer file .Close ()
48
+
49
+ scanner := bufio .NewScanner (file )
50
+ for scanner .Scan () {
51
+ handler (scanner .Text ())
52
+ }
53
+ }
Original file line number Diff line number Diff line change
1
+ module aoc2022
2
+
3
+ go 1.19
You can’t perform that action at this time.
0 commit comments