File tree Expand file tree Collapse file tree 2 files changed +55
-1
lines changed
Expand file tree Collapse file tree 2 files changed +55
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import (
1010 "testing"
1111)
1212
13- func TestDay1 (t * testing.T ) {
13+ func TestPart1 (t * testing.T ) {
1414 file , err := os .Open ("input" )
1515 if err != nil {
1616 t .Fatal ("can't open input file" )
@@ -61,3 +61,51 @@ func TestDay1(t *testing.T) {
6161
6262 fmt .Println (sum )
6363}
64+
65+ func TestPart2 (t * testing.T ) {
66+ file , err := os .Open ("input" )
67+ if err != nil {
68+ t .Fatal ("can't open input file" )
69+ }
70+ defer file .Close ()
71+
72+ var column1 []int
73+ var column2 []int
74+
75+ scanner := bufio .NewScanner (file )
76+ for scanner .Scan () {
77+ // ex: 31594 93577
78+ line := scanner .Text ()
79+
80+ lineStrSlice := strings .Split (line , " " )
81+ lineIntVar1 , err := strconv .Atoi (lineStrSlice [0 ])
82+ if err != nil {
83+ t .Fatal (err )
84+ }
85+
86+ lineIntVar2 , err := strconv .Atoi (lineStrSlice [1 ])
87+ if err != nil {
88+ t .Fatal (err )
89+ }
90+
91+ column1 = append (column1 , lineIntVar1 )
92+ column2 = append (column2 , lineIntVar2 )
93+ }
94+
95+ if scanner .Err () != nil {
96+ t .Fatal ("scanner err" )
97+ }
98+
99+ var score int
100+ for _ , v1 := range column1 {
101+ var sum int
102+ for _ , v2 := range column2 {
103+ if v1 == v2 {
104+ sum += v1
105+ }
106+ }
107+ score += sum
108+ }
109+
110+ fmt .Println (score )
111+ }
Original file line number Diff line number Diff line change 1+ 3 4
2+ 4 3
3+ 2 5
4+ 1 3
5+ 3 9
6+ 3 3
You can’t perform that action at this time.
0 commit comments