From f20f86f72720683d00b7ccafc9e5aa2c151ecb1d Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 12 Oct 2022 16:58:46 +0200 Subject: problem 79 in go --- go/079-passcode_derivation.go | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 go/079-passcode_derivation.go diff --git a/go/079-passcode_derivation.go b/go/079-passcode_derivation.go new file mode 100644 index 0000000..001d2f8 --- /dev/null +++ b/go/079-passcode_derivation.go @@ -0,0 +1,98 @@ +/* +* Passcode derivation +* Problem 79 +* +* A common security method used for online banking is to ask the user for three random +* characters from a passcode. For example, if the passcode was 531278, they may ask for the +* 2nd, 3rd, and 5th characters; the expected reply would be: 317. +* The text file, keylog.txt, contains fifty successful login attempts. +* Given that the three characters are always asked for in order, analyse the file so as to +* determine the shortest possible secret passcode of unknown length. + */ + +package main + +import "fmt" + +var PASSCODE_PARTS = [...][3]int{ + {3, 1, 9}, + {6, 8, 0}, + {1, 8, 0}, + {6, 9, 0}, + {1, 2, 9}, + {6, 2, 0}, + {7, 6, 2}, + {6, 8, 9}, + {7, 6, 2}, + {3, 1, 8}, + {3, 6, 8}, + {7, 1, 0}, + {7, 2, 0}, + {7, 1, 0}, + {6, 2, 9}, + {1, 6, 8}, + {1, 6, 0}, + {6, 8, 9}, + {7, 1, 6}, + {7, 3, 1}, + {7, 3, 6}, + {7, 2, 9}, + {3, 1, 6}, + {7, 2, 9}, + {7, 2, 9}, + {7, 1, 0}, + {7, 6, 9}, + {2, 9, 0}, + {7, 1, 9}, + {6, 8, 0}, + {3, 1, 8}, + {3, 8, 9}, + {1, 6, 2}, + {2, 8, 9}, + {1, 6, 2}, + {7, 1, 8}, + {7, 2, 9}, + {3, 1, 9}, + {7, 9, 0}, + {6, 8, 0}, + {8, 9, 0}, + {3, 6, 2}, + {3, 1, 9}, + {7, 6, 0}, + {3, 1, 6}, + {7, 2, 9}, + {3, 8, 0}, + {3, 1, 9}, + {7, 2, 8}, + {7, 1, 6}, +} + + +// dumb brute force approch (takes about 1min to solve) +func main() { + mainLoop: + for passcode := 100; ; passcode++ { + s := fmt.Sprintf("%d", passcode) + digits := make([]int, 0, len(s)) + for _, c := range s { + digits = append(digits, int(c - '0')) + } + for _, part := range PASSCODE_PARTS { + validCount := 0 + for _, d := range digits { + if d == part[validCount] { + validCount++ + } + if validCount == 3 { + break + } + } + if validCount != 3 { + continue mainLoop + } + } + fmt.Println(passcode) + break + } + +} -- cgit