From 6ed18289c89eaff3557907df63cf58bd2009b3ee Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 7 Dec 2021 14:26:36 +0100 Subject: Added the first days --- 2021/03/first.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2021/03/first.go (limited to '2021/03/first.go') diff --git a/2021/03/first.go b/2021/03/first.go new file mode 100644 index 0000000..5343505 --- /dev/null +++ b/2021/03/first.go @@ -0,0 +1,42 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" +) + +func main() { + f, err := os.Open("input") + if err != nil { + log.Fatalf("%+v", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + bits := make([]int, 12) + for scanner.Scan() { + num := scanner.Text() + for n := 0; n < 12; n++ { + if num[n] == '1' { + bits[n]++ + } + } + } + + gamma := 0 + epsilon := 0 + for n := 0; n < 12; n++ { + gamma *= 2 + epsilon *= 2 + if bits[n] > 500 { + gamma++ + } else { + epsilon++ + } + } + fmt.Println(gamma * epsilon) +} -- cgit v1.2.3