From 1859239c3671f8bfa3c6e6c18e8ddb8fae38e388 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 17 Jan 2021 08:23:03 +0100 Subject: [PATCH] day06 --- day05/day.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ inputs/5 | 1 + inputs/5_test | 1 + main.go | 7 ++++++- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 day05/day.go create mode 100644 inputs/5 create mode 100644 inputs/5_test diff --git a/day05/day.go b/day05/day.go new file mode 100644 index 0000000..229442e --- /dev/null +++ b/day05/day.go @@ -0,0 +1,52 @@ +package day05 + +import ( + "crypto/md5" + "fmt" + "strconv" + "tools" +) + +func Part1(puzzle tools.AoCPuzzle) interface{} { + doorId := puzzle.GetInputArray()[0] + password := "" + i := 0 + + for { + chk := doorId + strconv.Itoa(i) + chksum := fmt.Sprintf("%x", md5.Sum([]byte(chk))) + if chksum[0:5] == "00000" { + password += string(chksum[5]) + } + if len(password) == 8 { + break + } + i++ + } + + return password +} + +func Part2(puzzle tools.AoCPuzzle) interface{} { + doorId := puzzle.GetInputArray()[0] + password := make([]byte, 8) + i := 0 + cCount := 0 + + for { + chk := doorId + strconv.Itoa(i) + chksum := fmt.Sprintf("%x", md5.Sum([]byte(chk))) + if chksum[0:5] == "00000" { + if chksum[5] >= '0' && chksum[5] <= '7' && password[chksum[5]-48] == 0 { + password[chksum[5]-48] = chksum[6] + cCount++ + } + } + if cCount == 8 { + break + } + i++ + } + + return string(password) +} diff --git a/inputs/5 b/inputs/5 new file mode 100644 index 0000000..d152446 --- /dev/null +++ b/inputs/5 @@ -0,0 +1 @@ +wtnhxymk \ No newline at end of file diff --git a/inputs/5_test b/inputs/5_test new file mode 100644 index 0000000..f2ba8f8 --- /dev/null +++ b/inputs/5_test @@ -0,0 +1 @@ +abc \ No newline at end of file diff --git a/main.go b/main.go index 2373deb..b3b06bc 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "aoc2016/day02" "aoc2016/day03" "aoc2016/day04" + "aoc2016/day05" "flag" "fmt" "os" @@ -26,7 +27,7 @@ func initDayFunctions() { 2: {1: day02.Part1, 2: day02.Part2}, 3: {1: day03.Part1, 2: day03.Part2}, 4: {1: day04.Part1, 2: day04.Part2}, - // 5: {1: day05.Part1, 2: day05.Part2}, + 5: {1: day05.Part1, 2: day05.Part2}, // 6: {1: day06.Part1, 2: day06.Part2}, // 7: {1: day07.Part1, 2: day07.Part2}, // 8: {1: day08.Part1, 2: day08.Part2}, @@ -43,6 +44,10 @@ func initDayFunctions() { // 19: {1: day19.Part1, 2: day19.Part2}, // 20: {1: day20.Part1, 2: day20.Part2}, // 21: {1: day21.Part1, 2: day21.Part2}, + // 22: {1: day22.Part1, 2: day22.Part2}, + // 23: {1: day23.Part1, 2: day23.Part2}, + // 24: {1: day24.Part1, 2: day24.Part2}, + // 25: {1: day25.Part1, 2: day25.Part2}, } }