Last active
June 11, 2021 06:32
-
-
Save jeromewu/eb47751d7a1616c9030ca0170290e23a to your computer and use it in GitHub Desktop.
week01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns w01) | |
(def key-pad-1 [[\1 \2 \3] [\4 \5 \6] [\7 \8 \9]]) | |
(def key-pad-2 [[\0 \0 \1 \0 \0] [\0 \2 \3 \4 \0] [\5 \6 \7 \8 \9] [\0 \A \B \C \0] [\0 \0 \D \0 \0]]) | |
(def delta-map {\U [-1 0] \D [1 0] \L [0 -1] \R [0 1]}) | |
(defn read-file [filename] | |
(->> | |
(slurp filename) | |
(re-seq #"\w+"))) | |
(defn update-pos [ctx line key-pad] | |
(reduce | |
(fn [c op] | |
(let [delta (delta-map op) | |
nr (+ (:r c) (first delta)) | |
nc (+ (:c c) (last delta))] | |
(if (and (>= nr 0) (< nr (count key-pad)) (>= nc 0) (< nc (count (first key-pad))) (not= \0 (-> key-pad (nth nr) (nth nc)))) | |
(assoc c :r nr :c nc) | |
c))) | |
ctx | |
line)) | |
(defn solve [filename key-pad r c] | |
(->> | |
(read-file filename) | |
(reduce | |
(fn [ctx line] | |
(->> | |
(update-pos ctx line key-pad) | |
(#(let [codes (:codes %) | |
code (-> key-pad (nth (:r %)) (nth (:c %)))] | |
(assoc % :codes (conj codes code)))))) | |
{:codes [] :r r :c c}) | |
(:codes) | |
(reduce str))) | |
(solve "data/input01" key-pad-1 1 1) | |
(solve "data/input02" key-pad-1 1 1) | |
(solve "data/input01" key-pad-2 2 0) | |
(solve "data/input02" key-pad-2 2 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
) | |
var KEY_PAD_1 := [][]rune{ | |
{'1', '2', '3'}, | |
{'4', '5', '6'}, | |
{'7', '8', '9'}, | |
} | |
var KEY_PAD_2 := [][]rune{ | |
{0, 0, '1', 0, 0}, | |
{0, '2', '3', '4', 0}, | |
{'5', '6', '7', '8', '9'}, | |
{0, 'A', 'B', 'C', 0}, | |
{0, 0, 'D', 0, 0}, | |
} | |
var DELTA_MAP = map[rune][]int{ | |
'U': {-1, 0}, | |
'D': {1, 0}, | |
'L': {0, -1}, | |
'R': {0, 1}, | |
} | |
// an utility function for reading file content as string array | |
func readFile(filename string) []string { | |
var lines []string | |
file, err := os.Open(filename) | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
lines = append(lines, scanner.Text()) | |
} | |
return lines | |
} | |
func solve(filename string, keyPad [][]rune, rs, cs int) { | |
lines := readFile(filename) | |
codes := make([]rune, len(lines)) | |
r, c := rs, cs | |
for _, line := range lines { | |
for _, op := range line { | |
delta := DELTA_MAP[op] | |
nr, nc := r+delta[0], c+delta[1] | |
// update row and col index only when it is a valid move | |
if nr >= 0 && nr < len(keyPad) && nc >= 0 && nc < len(keyPad[0]) && keyPad[nr][nc] != 0 { | |
r, c = nr, nc | |
} | |
} | |
fmt.Print(keyPad[r][c]) | |
} | |
fmt.Println() | |
} | |
func main() { | |
inputFilename := os.Args[1] | |
solve(inputFilename, KEY_PAD_1, 1, 1) | |
solve(inputFilename, KEY_PAD_2, 2, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment