From baf854ce39df267121064ff97ef4ce6e257f3772 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 3 Dec 2021 18:17:34 +0100 Subject: [PATCH] 2021 - Day 3 --- 2021/3/go.mod | 9 +++ 2021/3/go.sum | 2 + 2021/3/main.go | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 2021/3/go.mod create mode 100644 2021/3/go.sum create mode 100644 2021/3/main.go diff --git a/2021/3/go.mod b/2021/3/go.mod new file mode 100644 index 0000000..4c58b8d --- /dev/null +++ b/2021/3/go.mod @@ -0,0 +1,9 @@ +module AoC/2021/3 + +require AoC/2021/common v0.0.0 + +require github.com/joho/godotenv v1.4.0 // indirect + +replace AoC/2021/common v0.0.0 => ../common + +go 1.17 diff --git a/2021/3/go.sum b/2021/3/go.sum new file mode 100644 index 0000000..8c9f290 --- /dev/null +++ b/2021/3/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/2021/3/main.go b/2021/3/main.go new file mode 100644 index 0000000..32a7c00 --- /dev/null +++ b/2021/3/main.go @@ -0,0 +1,150 @@ +package main + +import ( + "AoC/2021/common" + "bufio" + "strconv" +) + +func main() { + part1() + part2() +} + +func part1() { + aoc := aoc.New(2021, 3) + + aoc.Test(` +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 + `, 198) + + aoc.Solution(func (input *bufio.Scanner) int { + length := 0 + + var entries []int + + for input.Scan() { + line := input.Text() + if len(line) > length { + length = len(line) + } + + num, err := strconv.ParseInt(line, 2, 64) + if err != nil { + panic(err) + } + + entries = append(entries, int(num)) + } + + gamma := 0 + epsilon := 0 + for j := 0; j < length; j++ { + var count int + for _, num := range entries { + count += num >> j & 1 + } + + if float64(count)/float64(len(entries)) >= 0.5 { + gamma += 1 << j + } else { + epsilon += 1 << j + } + } + + return gamma*epsilon + }) +} + +func filter(entries []int, length int, findCommon bool) int { + e := make([]int, len(entries)) + copy(e, entries) + for j := length-1; j >= 0; j-- { + var count int + for _, num := range e { + count += num >> j & 1 + } + + var common int + if !findCommon { + common = 1 + } + if float64(count)/float64(len(e)) >= 0.5 { + if findCommon { + common = 1 + } else { + common = 0 + } + } + + n := 0 + for _, num := range e { + if num >> j & 1 == common { + e[n] = num + n++ + } + } + e = e[:n] + + if len(e) == 1 { + break + } + } + + return e[0] +} + +func part2() { + aoc := aoc.New(2021, 3) + + aoc.Test(` +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 + `, 230) + + aoc.Solution(func (input *bufio.Scanner) int { + length := 0 + + var entries []int + + for input.Scan() { + line := input.Text() + if len(line) > length { + length = len(line) + } + + num, err := strconv.ParseInt(line, 2, 64) + if err != nil { + panic(err) + } + + entries = append(entries, int(num)) + } + + oxygen := filter(entries, length, true) + co2 := filter(entries, length, false) + + return int(oxygen * co2) + }) +}