├── 2015 ├── 01.go ├── 01.k ├── 01.py ├── 01.q ├── 01.r ├── 02.go ├── 02.k ├── 02.py ├── 02.q ├── 03.go ├── 03.k ├── 03.q ├── 04.go ├── 04.k ├── 04.q ├── 05.go ├── 05.k ├── 05.py ├── 05.q ├── 06.go ├── 06.k ├── 06.py ├── 06.q ├── 07.go ├── 07.k ├── 07.py ├── 07.q ├── 08.go ├── 08.k ├── 08.py ├── 08.q ├── 09.go ├── 09.k ├── 09.py ├── 09.q ├── 10.go ├── 10.k ├── 10.py ├── 10.q ├── 11.go ├── 11.k ├── 11.py ├── 11.q ├── 12.go ├── 12.k ├── 12.py ├── 12.q ├── 13.go ├── 13.k ├── 13.py ├── 13.q ├── 14.go ├── 14.k ├── 14.py ├── 14.q ├── 15.go ├── 15.k ├── 15.q ├── 16.go ├── 16.k ├── 16.q ├── 17.go ├── 17.k ├── 17.py ├── 17.q ├── 18.go ├── 18.k ├── 18.q ├── 19.go ├── 19.k ├── 19.q ├── 20.go ├── 20.k ├── 20.q ├── 21.go ├── 21.k ├── 21.q ├── 22.go ├── 22.k ├── 22.q ├── 23.go ├── 23.k ├── 23.py ├── 23.q ├── 24.go ├── 24.k ├── 24.q ├── 25.go ├── 25.k ├── 25.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 15.txt │ ├── 16.txt │ ├── 17.txt │ ├── 18.txt │ ├── 19.txt │ ├── 20.txt │ ├── 21.txt │ ├── 22.txt │ ├── 23.txt │ ├── 24.txt │ └── 25.txt ├── 2016 ├── 01.py ├── 01.q ├── 02.py ├── 02.q ├── 03.py ├── 03.q ├── 04.py ├── 04.q ├── 05.py ├── 05.q ├── 06.py ├── 06.q ├── 07.py ├── 07.q ├── 08.py ├── 08.q ├── 09.py ├── 09.q ├── 10.py ├── 10.q ├── 11.q ├── 12.py ├── 12.q ├── 13.py ├── 13.q ├── 14.py ├── 14.q ├── 15.py ├── 15.q ├── 16.py ├── 16.q ├── 17.q ├── 18.py ├── 18.q ├── 19.py ├── 20.py ├── 21.py ├── 21.q ├── 22.py ├── 22.q ├── 23.py ├── 24.q ├── 25.py └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 15.txt │ ├── 16.txt │ ├── 17.txt │ ├── 18.txt │ ├── 19.txt │ ├── 20.txt │ ├── 21.txt │ ├── 22.txt │ ├── 23.txt │ ├── 24.txt │ └── 25.txt ├── 2017 ├── 01.py ├── 01.q ├── 02.py ├── 02.q ├── 03.py ├── 03.q ├── 04.py ├── 04.q ├── 05.py ├── 05.q ├── 06.q ├── 07.q ├── 08.q ├── 09.q ├── 10.q ├── 11.q ├── 12.q ├── 13.q ├── 14.q ├── 15.q ├── 16.q ├── 17.q ├── 18.q ├── 19.q ├── 20.q ├── 21.q ├── 22.q ├── 23.q ├── 24.q ├── 25.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 15.txt │ ├── 16.txt │ ├── 17.txt │ ├── 18.txt │ ├── 19.txt │ ├── 20.txt │ ├── 21.txt │ ├── 22.txt │ ├── 23.txt │ ├── 24.txt │ └── 25.txt ├── 2018 ├── 01.py ├── 01.q ├── 02.py ├── 02.q ├── 03.q ├── 04.q ├── 05.q ├── 06.q ├── 07.q ├── 08.q ├── 09.q ├── 10.q ├── 11.q ├── 12.q ├── 13.q ├── 14.q ├── 15.q ├── 16.q ├── 18.q ├── 19.q ├── 21.q ├── 22.q ├── 23.q ├── 25.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 15.txt │ ├── 16.txt │ ├── 18.txt │ ├── 19.txt │ ├── 21.txt │ ├── 22.txt │ ├── 23.txt │ └── 25.txt ├── 2019 ├── 01.js ├── 01.q ├── 02.js ├── 02.py ├── 02.q ├── 03.q ├── 04.q ├── 05.q ├── 06.q ├── 07.q ├── 08.q ├── 09.q ├── 10.q ├── 11.q ├── 12.q ├── 13.q ├── 14.q ├── 16.q ├── 22.q ├── 24.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 16.txt │ ├── 22.txt │ └── 24.txt ├── 2020 ├── 01.q ├── 02.q ├── 03.q ├── 04.q ├── 05.q ├── 06.q ├── 07.q ├── 08.q ├── 09.q ├── 10.q ├── 11.q ├── 12.q ├── 13.q ├── 14.q ├── 15.q ├── 17.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 15.txt │ └── 17.txt ├── 2021 ├── 01.q ├── 02.q ├── 03.q ├── 04.q ├── 05.q ├── 06.q ├── 07.q ├── 08.q ├── 09.q ├── 10.q ├── 11.q ├── 12.q ├── 13.q ├── 14.q ├── 15.q ├── 16.q ├── 17.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ ├── 14.txt │ ├── 15.txt │ ├── 16.txt │ └── 17.txt ├── 2022 ├── 01.q ├── 02.q ├── 03.q ├── 04.q ├── 05.q ├── 06.q ├── 07.q ├── 08.q ├── 09.q ├── 10.q ├── 11.q ├── 12.q ├── 13.py ├── 13.q ├── 14.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ ├── 10.txt │ ├── 11.txt │ ├── 12.txt │ ├── 13.txt │ └── 14.txt ├── 2023 ├── 01.q ├── 02.q ├── 03.q ├── 04.q ├── 06.q ├── 07.q ├── 08.q ├── 09.q ├── 15.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 06.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ └── 15.txt ├── 2024 ├── 01.q ├── 02.q ├── 03.py ├── 03.q ├── 04.q ├── 05.q ├── 07.q ├── 08.q ├── 09.q ├── 11.q └── input │ ├── 01.txt │ ├── 02.txt │ ├── 03.txt │ ├── 04.txt │ ├── 05.txt │ ├── 07.txt │ ├── 08.txt │ ├── 09.txt │ └── 11.txt ├── .gitignore ├── README.md ├── c ├── Makefile ├── README.md ├── aoc.c └── k.h └── rs ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md └── src └── aoc.rs /.gitignore: -------------------------------------------------------------------------------- 1 | *[A-z].txt 2 | *.gch 3 | *.so 4 | -------------------------------------------------------------------------------- /2015/01.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "log" 7 | ) 8 | 9 | func main() { 10 | data, err := ioutil.ReadFile("input/01.txt") 11 | if err != nil { 12 | log.Fatal(err) 13 | } 14 | 15 | floor, basement := 0, 0 16 | up := byte('(') 17 | 18 | for i, v := range data { 19 | if v == up { 20 | floor++ 21 | } else { 22 | floor-- 23 | } 24 | if floor < 0 && basement == 0 { 25 | basement = i 26 | } 27 | } 28 | 29 | fmt.Println(floor) 30 | fmt.Println(basement + 1) // answer is 1 indexed 31 | } 32 | -------------------------------------------------------------------------------- /2015/01.k: -------------------------------------------------------------------------------- 1 | /--- Day 1: Not Quite Lisp --- 2 | 3 | last s:+\1 -1")"=*0:"input/01.txt" 4 | 1+s?-1 5 | -------------------------------------------------------------------------------- /2015/01.py: -------------------------------------------------------------------------------- 1 | """--- Day 1: Not Quite Lisp ---""" 2 | 3 | floor = reached_basement = 0 4 | 5 | with open("input/01.txt") as f: 6 | # ( is up 7 | # ) is down 8 | instructions = f.read() 9 | reached_basement = 0 10 | 11 | for i, v in enumerate(instructions): 12 | if v == '(': 13 | floor += 1 14 | else: 15 | floor += -1 16 | if floor < 0 and reached_basement == 0: 17 | reached_basement = i + 1 18 | 19 | print(floor) 20 | print(reached_basement) 21 | -------------------------------------------------------------------------------- /2015/01.q: -------------------------------------------------------------------------------- 1 | /--- Day 1: Not Quite Lisp --- 2 | 3 | last s:sums -1 1"("=first read0 `:input/01.txt 4 | /232 5 | 1+s?-1 6 | /1783 7 | -------------------------------------------------------------------------------- /2015/01.r: -------------------------------------------------------------------------------- 1 | # --- Day 1: Not Quite Lisp --- 2 | 3 | input = "input/01.txt" 4 | 5 | a = cumsum( c( -1, 1 )[ 1 + as.integer( "(" == strsplit( readChar( input, file.info( input )$size ), "" )[[ 1 ]] ) ] ) 6 | tail(a, n = 1 ) 7 | #[1] 232 8 | 9 | min( which( a == -1 ) ) 10 | #[1] 1783 11 | -------------------------------------------------------------------------------- /2015/02.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "log" 7 | "os" 8 | "sort" 9 | "strconv" 10 | "strings" 11 | ) 12 | 13 | func getDimensions(entry string) []int { 14 | var dimensions []int 15 | for _, v := range strings.Split(entry, "x") { 16 | dimension, err := strconv.Atoi(v) 17 | if err != nil { 18 | log.Fatal(err) 19 | } 20 | dimensions = append(dimensions, dimension) 21 | } 22 | sort.Ints(dimensions) 23 | return dimensions 24 | } 25 | 26 | func calculateWrappingPaper(dimensions []int) int { 27 | return (2 * dimensions[2] * dimensions[0]) + // 2 * l * w 28 | (2 * dimensions[0] * dimensions[1]) + // 2 * w * h 29 | (2 * dimensions[1] * dimensions[2]) + // 2 * h * l 30 | dimensions[0]*dimensions[1] // slack 31 | } 32 | 33 | func calculateRibbon(dimensions []int) int { 34 | return (2*dimensions[0] + 2*dimensions[1]) + // smallest perimeter 35 | (dimensions[0] * dimensions[1] * dimensions[2]) // volume 36 | } 37 | 38 | func main() { 39 | file, err := os.Open("input/02.txt") 40 | if err != nil { 41 | log.Fatal(err) 42 | } 43 | defer file.Close() 44 | 45 | wrap, ribbon := 0, 0 46 | 47 | scanner := bufio.NewScanner(file) 48 | for scanner.Scan() { 49 | dimensions := getDimensions(scanner.Text()) 50 | wrap += calculateWrappingPaper(dimensions) 51 | ribbon += calculateRibbon(dimensions) 52 | } 53 | 54 | if err := scanner.Err(); err != nil { 55 | log.Fatal(err) 56 | } 57 | 58 | fmt.Println(wrap) 59 | fmt.Println(ribbon) 60 | } 61 | -------------------------------------------------------------------------------- /2015/02.k: -------------------------------------------------------------------------------- 1 | /--- Day 2: I Was Told There Would Be No Math --- 2 | 3 | t:+`w`h`l!+^'"i"$'"x"\'0:"input/02.txt" 4 | 5 | first select +/(2*w*l)+(3*w*h)+2*h*l from t 6 | first select +/(w*h*l)+(2*w+h) from t 7 | -------------------------------------------------------------------------------- /2015/02.py: -------------------------------------------------------------------------------- 1 | """--- Day 2: I Was Told There Would Be No Math ---""" 2 | 3 | paper = ribbon = 0 4 | 5 | with open("input/02.txt") as f: 6 | for line in f: 7 | dimensions = list(map(int, line.split("x"))) # "8x5x3" -> [ 8, 5, 3] 8 | dimensions.sort() # [ 8, 5, 3 ] -> [ 3, 5, 8 ] 9 | l, w, h = dimensions # l = 3, w = 5, h = 8 10 | paper += (2*l*w + 2*w*h + 2*h*l) + l*w # (surface) + slack 11 | ribbon += (2*l+2*w) + l*w*h # (wrap) + bow 12 | 13 | print(paper) 14 | print(ribbon) 15 | -------------------------------------------------------------------------------- /2015/02.q: -------------------------------------------------------------------------------- 1 | /--- Day 2: I Was Told There Would Be No Math --- 2 | 3 | t:flip `w`h`l!flip asc each "J"$ "x" vs'read0 `:input/02.txt 4 | exec sum (2*w*l) + (3*w*h) + 2*h*l from t 5 | /1598415 6 | exec sum (w*h*l) + (2*w+h) from t 7 | /3812909 8 | -------------------------------------------------------------------------------- /2015/03.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "log" 7 | ) 8 | 9 | type key struct { 10 | x int 11 | y int 12 | } 13 | 14 | func solve(data []byte, roboSanta bool) int { 15 | x, y, rx, ry := 0, 0, 0, 0 16 | 17 | visited := make(map[key]int) 18 | visited[key{x: x, y: y}] = 2 19 | 20 | north, east, south, west := byte('^'), byte('>'), byte('v'), byte('<') 21 | 22 | for i, v := range data { 23 | santa := i%2 == 0 && roboSanta 24 | 25 | if v == north { 26 | if santa { 27 | x++ 28 | } else { 29 | rx++ 30 | } 31 | } else if v == east { 32 | if santa { 33 | y++ 34 | } else { 35 | ry++ 36 | } 37 | } else if v == south { 38 | if santa { 39 | x-- 40 | } else { 41 | rx-- 42 | } 43 | } else if v == west { 44 | if santa { 45 | y-- 46 | } else { 47 | ry-- 48 | } 49 | } 50 | 51 | if santa { 52 | visited[key{x, y}]++ 53 | } else { 54 | visited[key{rx, ry}]++ 55 | } 56 | } 57 | 58 | return len(visited) 59 | } 60 | 61 | func main() { 62 | data, err := ioutil.ReadFile("input/03.txt") 63 | 64 | if err != nil { 65 | log.Fatal(err) 66 | } 67 | 68 | fmt.Println(solve(data, false)) 69 | fmt.Println(solve(data, true)) 70 | } 71 | -------------------------------------------------------------------------------- /2015/03.k: -------------------------------------------------------------------------------- 1 | /--- Day 3: Perfectly Spherical Houses in a Vacuum --- 2 | 3 | #?+\d:(0 1;0 -1;-1 0;1 0)"^v<>"?*0:"input/03.txt" 4 | #?,/+\2^d 5 | -------------------------------------------------------------------------------- /2015/03.q: -------------------------------------------------------------------------------- 1 | /--- Day 3: Perfectly Spherical Houses in a Vacuum --- 2 | 3 | count distinct sums d:(0 1;0 -1;-1 0;1 0)"^v<>"?first read0 `:input/03.txt 4 | /2572 5 | count distinct raze sums 0N 2#d 6 | /2631 7 | -------------------------------------------------------------------------------- /2015/04.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "crypto/md5" 5 | "encoding/hex" 6 | "fmt" 7 | "io/ioutil" 8 | "log" 9 | "strings" 10 | ) 11 | 12 | func solve(secret string, prefix string) int { 13 | key := 0 14 | for { 15 | text := fmt.Sprintf("%s%d", secret, key) 16 | hash := md5.New() 17 | hash.Write([]byte(text)) 18 | result := hex.EncodeToString(hash.Sum(nil)) 19 | if strings.HasPrefix(result, prefix) { 20 | return key 21 | } 22 | key++ 23 | } 24 | } 25 | 26 | func main() { 27 | data, err := ioutil.ReadFile("input/04.txt") 28 | if err != nil { 29 | log.Fatal(err) 30 | } 31 | 32 | secret := string(data[:len(data)-1]) // drop \n 33 | fmt.Println(solve(secret, "00000")) 34 | fmt.Println(solve(secret, "000000")) 35 | } 36 | -------------------------------------------------------------------------------- /2015/04.k: -------------------------------------------------------------------------------- 1 | /--- Day 4: The Ideal Stocking Stuffer --- 2 | 3 | /md5:*"../rs/target/release/libaoc.so"2:`md5!1 4 | md5:*"../c/aoc.so"2:`md5!1 5 | 6 | f:{[x;y;z] y<256/3#md5 (x,$z),0x0}[*0:"input/04.txt";;] 7 | 8 | (f[16];1+)/:0 9 | (f[0];1+)/:0 10 | -------------------------------------------------------------------------------- /2015/04.q: -------------------------------------------------------------------------------- 1 | /--- Day 4: The Ideal Stocking Stuffer --- 2 | 3 | f:{not (16>a 2)&0 0~2#a:"j"$md5 x,string y}[r:first read0 `:input/04.txt;] 4 | g:{sum 3#md5 x,string y}[r;] 5 | (1+)/[f;0] 6 | /254575 7 | (1+)/[g;0] 8 | /1038736 9 | -------------------------------------------------------------------------------- /2015/05.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "log" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func isNice(word string) int { 12 | vowels := 0 13 | for _, v := range "aeiou" { 14 | vowels += strings.Count(word, string(v)) 15 | } 16 | if vowels < 3 { 17 | return 0 18 | } 19 | 20 | doubles := 0 21 | for i := 0; i < len(word)-1; i++ { 22 | if word[i] == word[i+1] { 23 | doubles++ 24 | } 25 | } 26 | if doubles == 0 { 27 | return 0 28 | } 29 | 30 | for _, v := range []string{"ab", "cd", "pq", "xy"} { 31 | if strings.Contains(word, v) { 32 | return 0 33 | } 34 | } 35 | return 1 36 | } 37 | 38 | func isReallyNice(word string) int { 39 | repeats, duplicates := 0, 0 40 | 41 | for i := 0; i < len(word)-2; i++ { 42 | if word[i] == word[i+2] { 43 | repeats++ 44 | } 45 | if strings.Index(word[i+2:len(word)], word[i:i+2]) > -1 { 46 | duplicates++ 47 | } 48 | } 49 | if repeats > 0 && duplicates > 0 { 50 | return 1 51 | } 52 | return 0 53 | } 54 | 55 | func main() { 56 | file, err := os.Open("input/05.txt") 57 | if err != nil { 58 | log.Fatal(err) 59 | } 60 | defer file.Close() 61 | 62 | nice, reallyNice := 0, 0 63 | 64 | scanner := bufio.NewScanner(file) 65 | for scanner.Scan() { 66 | text := scanner.Text() 67 | nice += isNice(text) 68 | reallyNice += isReallyNice(text) 69 | } 70 | 71 | if err := scanner.Err(); err != nil { 72 | log.Fatal(err) 73 | } 74 | 75 | fmt.Println(nice) 76 | fmt.Println(reallyNice) 77 | } 78 | -------------------------------------------------------------------------------- /2015/05.k: -------------------------------------------------------------------------------- 1 | /--- Day 5: Doesn't He Have Intern-Elves For This? --- 2 | 3 | sw:{[x;y] (y-1)_(-y)#',\x} / sliding window 4 | 5 | v:{[x] 2<#"aeiou"#x} / 3 vowels 6 | d:{[x] |/=':x} / doubles 7 | b:{[x] ~#(2^"abcdpqxy")#sw[;2] x} / no bad chars 8 | 9 | p:{[x] $[2=c:#w:&1<+/s~\/s:sw[;2] x;~1=-/|w;2 -1: 20 | bad = True 21 | if v_cnt > 2 and double and not bad: 22 | part1 += 1 23 | 24 | # part 2 25 | pairs = repeats = False 26 | for i in range(0, len(line)-2): 27 | a, b, c = line[i:i+3] 28 | if a == c: 29 | repeats = True 30 | if line.count(a+b) > 1: 31 | pairs = True 32 | if repeats and pairs: 33 | part2 += 1 34 | 35 | print(part1) 36 | print(part2) 37 | -------------------------------------------------------------------------------- /2015/05.q: -------------------------------------------------------------------------------- 1 | /--- Day 5: Doesn't He Have Intern-Elves For This? --- 2 | 3 | sw:{neg[x-1]_flip next\[x-1;y]} / sliding window 4 | 5 | sum {(any x=prev x) and (0=count raze x ss/:("ab";"cd";"pq";"xy")) and 2 0: 25 | grid[x][y][1] -= 1 # decrease brightness 26 | else: 27 | grid[x][y][0] = 1 - grid[x][y][0] # toggle 28 | grid[x][y][1] += 2 # increase brightness by 2 29 | 30 | # calculate brightness 31 | for x in range(size): 32 | for y in range(size): 33 | brightness += grid[x][y][1] 34 | if grid[x][y][0] == 1: 35 | lit += 1 36 | 37 | print(lit) 38 | print(brightness) 39 | -------------------------------------------------------------------------------- /2015/06.q: -------------------------------------------------------------------------------- 1 | /--- Day 6: Probably a Fire Hazard --- 2 | 3 | sanitize:{ "J"$flip ","vs'(" "vs x)$["u"=x 1;2 4;1 3] }; 4 | range:{ first[x]+til 1+last[x]-first x }; 5 | 6 | on:{ x[range y;range z]:1b;x }; 7 | off:{ x[range y;range z]:0b;x }; 8 | tog:{ x[range y;range z]:not x[range y;range z];x }; 9 | 10 | light:{[g;i] 11 | $["n"=a:i 6; 12 | on[g;]; 13 | "f"=a; 14 | off[g;]; 15 | tog[g;]] . sanitize i 16 | }; 17 | 18 | sum sum light/[1000 1000#0b;r:read0 `:input/06.txt] 19 | /377891 20 | 21 | on:{ x[range y;range z]+:1;x }; 22 | off:{ x[range y;range z]:0|x[range y;range z]-1;x }; 23 | tog:{ x[range y;range z]+:2;x }; 24 | 25 | sum sum light/[1000 1000#0;r] 26 | /14110788 27 | -------------------------------------------------------------------------------- /2015/07.k: -------------------------------------------------------------------------------- 1 | /--- Day 7: Some Assembly Required --- 2 | 3 | g:{[x] $["0123456789"'*x;. x;(!W)'`$x;W`$x;()]} / get 4 | b:{[x] ((32-#b)#0),b:2\x} / binary 5 | 6 | w:{[x;y] $[#(),v:g@x;W[`$y]:v;()]} / wire 7 | n:{[x;y] $[#(),v:g@x;W[`$y]:2/16 _1 0 b v;()]} / not 8 | a:{[x;y;z] $[2=#,/v:g'(x;y);W[`$z]:2/&/b'v;()]} / and 9 | o:{[x;y;z] $[2=#,/v:g'(x;y);W[`$z]:2/|/b'v;()]} / or 10 | l:{[x;y;z] $[2=#,/v:g'(x;y);W[`$z]:{[x;y] 2/16 _ b[x],y#0}. v;()]} / lshift 11 | r:{[x;y;z] $[2=#,/v:g'(x;y);W[`$z]:{[x;y] 2/(-y) _ b x}. v;()]} / rshift 12 | 13 | f:{[x] 14 | $[(!W)'`$last x / already solved 15 | () 16 | 3=#x / wire 17 | w . x 0 2 18 | 4=#x / not 19 | n . x 1 3 20 | ((a;o;l;r)"AOLR"?x[1;0]). x 0 2 4]} 21 | 22 | {[x;y] f'x;W}[i:" "\'0:"input/07.txt"]/:W:`!0N; 23 | W`a 24 | 25 | i:@[i;&(,"b")~/last'i;:;,($W`a;"->";,"b")] 26 | {[x;y] f'x;W}[i]/:W:`!0N; 27 | W`a 28 | -------------------------------------------------------------------------------- /2015/07.q: -------------------------------------------------------------------------------- 1 | /--- Day 7: Some Assembly Required --- 2 | 3 | .a.wires:(`u#enlist `)!enlist 0N 4 | 5 | .a.get:{ $[all x in .Q.n;"J"$x;.a.wires `$x] } 6 | .a.not:{ $[null r:.a.get x;0N;0b sv (48#0b),not -16#0b vs r] } 7 | .a.and:{ $[any null r:.a.get each (x;y);0N;0b sv all 0b vs'r] } 8 | .a.or:{ $[any null r:.a.get each (x;y);0N;0b sv any 0b vs'r] } 9 | .a.ls:{ "j"$(2 xexp .a.get y) * .a.get x } 10 | .a.rs:{ .a.get[x] div (2 xexp .a.get y) } 11 | 12 | f:{ 13 | if[not null .a.wires y;:()]; 14 | .a.wires[y]:$[1=count x; 15 | .a.get x 0; 16 | "NOT"~x 0; 17 | .a.not x 1; 18 | "AND"~x 1; 19 | .a.and . x 0 2; 20 | "OR"~x 1; 21 | .a.or . x 0 2; 22 | "LSHIFT"~x 1; 23 | .a.ls . x 0 2; 24 | .a.rs . x 0 2 25 | ] 26 | }; 27 | 28 | i:{(" "vs x;`$y)}.'" -> "vs/:read0 `:input/07.txt 29 | 30 | while[null .a.wires`a;f .'i];a:.a.wires`a;a 31 | /16076 32 | 33 | delete from `.a.wires; 34 | i[where i[;1]=`b;0]:enlist enlist string a 35 | while[null .a.wires`a;f .'i];a:.a.wires`a;a 36 | /2797 37 | -------------------------------------------------------------------------------- /2015/08.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "log" 7 | "os" 8 | ) 9 | 10 | func decode(text string) int { 11 | memory := 0 12 | 13 | for i := 1; i < len(text)-1; { 14 | if text[i] == byte('\\') { 15 | if text[i+1] == byte('x') { 16 | i += 4 17 | } else { 18 | i += 2 19 | } 20 | } else { 21 | i++ 22 | } 23 | memory++ 24 | } 25 | return len(text) - memory 26 | } 27 | 28 | func encode(text string) int { 29 | memory := 6 // "" => "\"\"" 30 | for i := 1; i < len(text)-1; i++ { 31 | if text[i] == byte('\\') || text[i] == byte('"') { 32 | memory++ 33 | } 34 | memory++ 35 | } 36 | return memory - len(text) 37 | } 38 | 39 | func main() { 40 | file, err := os.Open("input/08.txt") 41 | if err != nil { 42 | log.Fatal(err) 43 | } 44 | defer file.Close() 45 | 46 | decoded, encoded := 0, 0 47 | 48 | scanner := bufio.NewScanner(file) 49 | for scanner.Scan() { 50 | text := scanner.Text() 51 | decoded += decode(text) 52 | encoded += encode(text) 53 | } 54 | 55 | if err := scanner.Err(); err != nil { 56 | log.Fatal(err) 57 | } 58 | 59 | fmt.Println(decoded) 60 | fmt.Println(encoded) 61 | } 62 | -------------------------------------------------------------------------------- /2015/08.k: -------------------------------------------------------------------------------- 1 | /--- Day 8: Matchsticks --- 2 | 3 | d:{[x] 4 | *(0;" ";0){[x;y] / (count;previous;skip) 5 | c:x 0 6 | $[s:x 2 7 | (c;y;s-1) 8 | "\\"=x 1 / previous was an escape char 9 | (1+x 0;y;$[y="x";3;1]) 10 | (1+x 0;y;0)]}/:x} 11 | 12 | +/{[x] (2+#x)-d x}'i:0:"input/08.txt" 13 | +/{[x] 2++/"\"\\"'x}'i 14 | -------------------------------------------------------------------------------- /2015/08.py: -------------------------------------------------------------------------------- 1 | """--- Day 8: Matchsticks ---""" 2 | 3 | with open("input/08.txt") as f: 4 | total_code = total_mem = total_recode = 0 5 | for line in f: 6 | mem = 0 7 | code = 2 # assume "" in every string 8 | i = 1 9 | recode = 6 # assume "\"\"" in every string 10 | 11 | while i < (len(line) -2): 12 | if line[i] == "\\": 13 | if line[i+1] == "x": # \x00 14 | code += 4 15 | mem += 1 16 | i += 4 17 | recode += 5 18 | elif line[i+1] == "\\" or line[i+1] == "\"": # \\ or \" 19 | code += 2 20 | mem += 1 21 | i += 2 22 | recode += 4 23 | else: 24 | code += 1 25 | i += 1 26 | mem += 1 27 | recode += 1 28 | 29 | total_mem += mem 30 | total_code += code 31 | total_recode += recode 32 | 33 | print(total_code - total_mem) 34 | print(total_recode - total_code) 35 | -------------------------------------------------------------------------------- /2015/08.q: -------------------------------------------------------------------------------- 1 | /--- Day 8: Matchsticks --- 2 | 3 | sum { count[x]-count 2_ ssr[ssr[x;"\\x[0-9a-f][0-9a-f]";"_"];"\\?";"_"] } each r:read0 `:input/08.txt 4 | /1350 5 | sum { count[.Q.s1 x]-count x} each r 6 | /2085 7 | -------------------------------------------------------------------------------- /2015/09.k: -------------------------------------------------------------------------------- 1 | /--- Day 9: All in a Single Night --- 2 | 3 | k:{[x](,/@\)/,\(,<<)'x=/x:!x} / from: https://groups.google.com/d/msg/shaktidb/OTsNFbAa7dI/sRmpNwYPAwAJ 4 | sw:{[x;y] (y-1)_(-y)#',\x} / sliding window 5 | 6 | r:,/{[x] (`$,/'2#''x(0 2;2 0))!2#`i$x 4}'" "\'0:"input/09.txt" / routes 7 | c:{[x] x@k@#x}@`$?2^,/$!r / combos 8 | 9 | min t:{[x] +/r`$,/'$sw[x;2]}'c 10 | max t 11 | -------------------------------------------------------------------------------- /2015/09.py: -------------------------------------------------------------------------------- 1 | """--- Day 9: All in a Single Night ---""" 2 | 3 | def recurse(current, routes, distance): 4 | if len(routes) > 1: 5 | for route in routes: 6 | # python lists are a bastard 7 | r = list(routes) 8 | # been there, done that 9 | r.remove(route) 10 | # next stop ... 11 | recurse(route, r, distance + destinations[current][route]) 12 | else: 13 | # final destination 14 | final_destinations.append(distance + destinations[current][routes[0]]) 15 | 16 | def day_9(instructions): 17 | # build destinations 18 | for line in instructions: 19 | location, _, destination, _, distance = line.split(" ") 20 | if location not in destinations: 21 | destinations[location] = {} 22 | if destination not in destinations: 23 | destinations[destination] = {} 24 | # double-pack 25 | destinations[location][destination] = destinations[destination][location] = int(distance) 26 | 27 | # bootstrap 28 | for route, _ in destinations.items(): 29 | r = list(destinations.keys()) 30 | r.remove(route) 31 | recurse(route, r, 0) 32 | return final_destinations 33 | 34 | with open("input/09.txt") as f: 35 | final_destinations = [] 36 | destinations = {} 37 | res = day_9(f) 38 | print(min(res)) 39 | print(max(res)) 40 | -------------------------------------------------------------------------------- /2015/09.q: -------------------------------------------------------------------------------- 1 | /--- Day 9: All in a Single Night --- 2 | 3 | /read data 4 | r:flip`f`t`d!("s s i";" ")0:`:input/09.txt; 5 | /combinations 6 | c:{raze y,/:'x except/:y}[ix;]/[count[ix]-1;] ix:distinct (raze/)exec (f;t) from r; 7 | /calculate all routes 8 | min routes:{ sum {[x;y] first exec d from r where f in (x;y), t in (x;y) }.'-2#'prev\[count[x]-2;x] } each c 9 | /251 10 | max routes 11 | /898 -------------------------------------------------------------------------------- /2015/10.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func lookAndSay(input []byte) []byte { 8 | var new []byte 9 | count := byte('0') 10 | compare := input[0] 11 | 12 | for _, v := range input { 13 | if v == compare { 14 | count++ 15 | } else { 16 | new = append(new, count, compare) 17 | // reset 18 | count = byte('1') 19 | compare = v 20 | } 21 | } 22 | return append(new, count, input[len(input)-1]) 23 | } 24 | 25 | func main() { 26 | res := lookAndSay([]byte("1113222113")) 27 | 28 | for i := 1; i < 50; i++ { 29 | if i == 40 { 30 | fmt.Println(len(res)) 31 | } 32 | res = lookAndSay(res) 33 | } 34 | fmt.Println(len(res)) 35 | } 36 | -------------------------------------------------------------------------------- /2015/10.k: -------------------------------------------------------------------------------- 1 | /--- Day 10: Elves Look, Elves Say --- 2 | 3 | #@[;40] r:(50;{[x] ,/(1_-':i,#x),'x@i:&~~':x})\:.'*0:"input/10.txt" 4 | #last r 5 | -------------------------------------------------------------------------------- /2015/10.py: -------------------------------------------------------------------------------- 1 | """--- Day 10: Elves Look, Elves Say ---""" 2 | 3 | def iterate(string): 4 | new = [] 5 | count = 0 6 | compare = string[0] 7 | for s in string: 8 | if s == compare: 9 | count += 1 10 | else: 11 | new.append(str(count) + compare) 12 | # reset 13 | count = 1 14 | compare = s 15 | new.append(str(count) + string[-1]) 16 | return ''.join(new) 17 | 18 | with open("input/10.txt") as instructions: 19 | for line in instructions: 20 | text = str(line).replace("\n", "") 21 | 22 | for i in range(1, 51): 23 | text = iterate(text) 24 | if i in (40, 50): 25 | print(len(text)) 26 | -------------------------------------------------------------------------------- /2015/10.q: -------------------------------------------------------------------------------- 1 | /--- Day 10: Elves Look, Elves Say --- 2 | 3 | res:{raze (1_deltas i,count x),'x i:where differ x}\[50;]10 vs "J"$ first read0 `:input/10.txt 4 | count res 40 5 | /252594 6 | count res 50 7 | /3579328 8 | -------------------------------------------------------------------------------- /2015/11.k: -------------------------------------------------------------------------------- 1 | /--- Day 11: Corporate Policy --- 2 | 3 | a:"abcdefghijklmnopqrstuvwxyz" 4 | 5 | n:{[x] / generate next pass 6 | $[0=#x 7 | "a" 8 | "z">last x 9 | (-1_ x),a@1+a?last x 10 | (n@-1_x),"a"]} 11 | 12 | s:{[x] (1_-':&1=-':a?x)'1} / straight 13 | b:{[x] ~|/"iol"'x} / no bad chars 14 | p:{[x] 1<+/{[x] (1_-':&x)'1}'a=\x} / pairs 15 | 16 | c:{[x] 17 | $[~s x / no straight 18 | n x 19 | ~b x / bad chars 20 | n x 21 | ~p x / no pairs 22 | n x 23 | x]} 24 | 25 | r:c/:*0:"input/11.txt";r 26 | c/:n r 27 | -------------------------------------------------------------------------------- /2015/11.py: -------------------------------------------------------------------------------- 1 | """--- Day 11: Corporate Policy ---""" 2 | 3 | def containsStraight(p): 4 | for i in range(len(p) - 2): 5 | if ord(p[i]) + 2 == ord(p[i+1]) + 1 == ord(p[i+2]): 6 | return True # break early 7 | return False 8 | 9 | def containsIllegals(p): 10 | illegals = ["i", "o", "l"] 11 | 12 | for i in illegals: 13 | if i in p: 14 | return True 15 | return False 16 | 17 | def containsDouble(p): 18 | count = 0 19 | i = 0 20 | while i < len(p) -1: 21 | if p[i] == p[i+1]: 22 | count += 1 23 | i += 2 # skip next 24 | else: 25 | i += 1 26 | return count > 1 27 | 28 | def checkPass(password): 29 | return containsStraight(password) and containsDouble(password) and not containsIllegals(password) 30 | 31 | def incr(s): 32 | if s == '': 33 | return 'a' 34 | if s[-1] < 'z': 35 | return s[0:-1] + chr(ord(s[-1])+1) 36 | return incr(s[:-1]) + 'a' 37 | 38 | def day_11 (instructions): 39 | password = incr(instructions) 40 | 41 | while not checkPass(password): 42 | password = incr(password) 43 | return password 44 | 45 | with open("input/11.txt") as f: 46 | for line in f: 47 | a = day_11(line.replace("\n", "")) 48 | b = day_11(incr(a)) 49 | print(a) 50 | print(b) 51 | -------------------------------------------------------------------------------- /2015/11.q: -------------------------------------------------------------------------------- 1 | /--- Day 11: Corporate Policy --- 2 | 3 | increment:{.Q.a 26 vs 1+26 sv -97+"j"$x} 4 | checkpass:{ 5 | $[not any "iol" in x; 6 | $[1 in 1_deltas where 1=deltas "j"$x; 7 | 1 distance: 18 | distance = r["distance"] 19 | # assign points to leader(s) 20 | for r in reindeers: 21 | if r["distance"] == distance: 22 | r["points"] += 1 23 | 24 | points = 0 25 | for r in reindeers: 26 | if r["points"] > points: 27 | points = r["points"] 28 | 29 | return (distance, points) 30 | 31 | with open("input/14.txt") as instructions: 32 | res = day_14(instructions, 2503) 33 | print(res[0]) 34 | print(res[1]) 35 | -------------------------------------------------------------------------------- /2015/14.q: -------------------------------------------------------------------------------- 1 | /--- Day 14: Reindeer Olympics --- 2 | 3 | t:flip `n`s`t`r!("s j j j";" ")0: `:input/14.txt 4 | 5 | max last f:flip value exec sums 2503#((t#s),r#0) by n from t 6 | /2696 7 | max sum each flip f=max flip f 8 | /1084 9 | -------------------------------------------------------------------------------- /2015/15.k: -------------------------------------------------------------------------------- 1 | /--- Day 15: Science for Hungry People --- 2 | 3 | c:{[x;y;z] $[z=#x;,x,y;,/c[;;z]'[x,/t;y-t:!1+y]]} / combo generator 4 | 5 | i:{[x] "i"$@[;2 4 6 8 10]@" "\",:"_x}'0:"input/15.txt" 6 | s:{[x;y] (t*500=a 4;t:*/0|4#a:+/x*y)}[i;]'c[!0;100;3] 7 | 8 | max s[;1] 9 | max s[;0] 10 | 11 | \ 12 | 13 | i:xkey[;`ing]@+`ing`cap`dur`fla`tex`cal!+{[x] last'" iiiii"$@[;0 2 4 6 8 10]@" "\",:"_x}'0:"input/15.txt" 14 | r:+(`$'*+!i)!+c[();100;3] 15 | 16 | 17 | {[x;y] */0|4#a:sum (. y)* {[x;y] x {ing:y} }[i;]'*'$!y}[i;]'r 18 | 19 | \ 20 | s:{[x;y] (t*500=a`cal;t:*/0|4#a:+/'+(. x)*y) }[;. i]'r 21 | 22 | |/s[;1] 23 | |/s[;0] 24 | 25 | \ 26 | abs:{[x] x*-1 1@0 7)|cats=-1) where samoyeds in -1 2) where (pomeranians <3)|pomeranians=-1) where akitas in -1 0) where vizslas in -1 0) where (goldfish < 5)|goldfish = -1) where (trees>3)|trees=-1) where cars in -1 2) where perfumes in -1 1 8 | -------------------------------------------------------------------------------- /2015/16.q: -------------------------------------------------------------------------------- 1 | /--- Day 16: Aunt Sue --- 2 | 3 | / initialise empty table 4 | sues:`sue xkey flip `sue`children`cats`samoyeds`pomeranians`akitas`vizslas`goldfish`trees`cars`perfumes!"jjjjjjjjjjj"$\:() 5 | / add sues 1-->500, null for unknowns 6 | { `sues upsert x,(count[cols sues]-1)#0N } each 1 + til 500; 7 | / upsert function 8 | upsue:{ ![`sues;enlist(=;`sue;x);0b;enlist[y]!enlist[z]] }; 9 | / upsert information about each sue 10 | {upsue[first s;].' 2 cut 1_s:(" isisisi";" ")0:{x except ":,"} x } each read0 `:input/16.txt; 11 | 12 | 0N!exec first sue from sues 13 | where children in 0N 3, 14 | cats in 0N 7, 15 | samoyeds in 0N 2, 16 | pomeranians in 0N 3, 17 | akitas in 0N 0, 18 | vizslas in 0N 0, 19 | goldfish in 0N 5, 20 | trees in 0N 3, 21 | cars in 0N 2, 22 | perfumes in 0N 1; 23 | /103 24 | 25 | 0N!exec first sue from sues 26 | where children in 0N 3, 27 | (cats > 7) or null cats, 28 | samoyeds in 0N 2, 29 | (pomeranians < 3) or null pomeranians, 30 | akitas in 0N 0, 31 | vizslas in 0N 0, 32 | (goldfish < 5) or null goldfish, 33 | (trees > 3) or null trees, 34 | cars in 0N 2, 35 | perfumes in 0N 1; 36 | /405 37 | -------------------------------------------------------------------------------- /2015/17.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | target := 150 9 | containers := []int{11, 30, 47, 31, 32, 36, 3, 1, 5, 3, 32, 36, 15, 11, 46, 26, 28, 1, 19, 3} 10 | 11 | var ids []int 12 | for i := range containers { 13 | ids = append(ids, i) 14 | } 15 | 16 | queue := [][]int{} 17 | solutions := [][]int{} 18 | // bootstrap queue 19 | for _, id := range ids { 20 | queue = append(queue, []int{id}) 21 | } 22 | 23 | for { 24 | if len(queue) == 0 { 25 | break 26 | } 27 | 28 | var item []int 29 | item, queue = queue[0], queue[1:] 30 | 31 | used := 0 32 | for _, id := range item { 33 | used += containers[id] 34 | } 35 | 36 | if used == target { 37 | solutions = append(solutions, item) 38 | continue 39 | } 40 | 41 | for _, nextContainer := range ids { 42 | if nextContainer < item[len(item)-1] { 43 | if containers[nextContainer]+used <= target { 44 | /* avoid references */ 45 | itemCopy := make([]int, len(item)) 46 | copy(itemCopy, item) 47 | 48 | queue = append(queue, append(itemCopy, nextContainer)) 49 | } 50 | } 51 | } 52 | } 53 | 54 | fmt.Println(len(solutions)) 55 | 56 | minLength := len(containers) 57 | for _, solution := range solutions { 58 | if len(solution) < minLength { 59 | minLength = len(solution) 60 | } 61 | } 62 | minSolutions := 0 63 | for _, solution := range solutions { 64 | if len(solution) == minLength { 65 | minSolutions++ 66 | } 67 | } 68 | fmt.Println(minSolutions) 69 | } 70 | -------------------------------------------------------------------------------- /2015/17.k: -------------------------------------------------------------------------------- 1 | /--- Day 17: No Such Thing as Too Much --- 2 | 3 | a:"abcdefghijklmnopqrstuvwxyz" 4 | 5 | d:{[x;y] x y_#x} / drop 6 | 7 | b:{[x] (`$'a@!#x)!x}@|^`i$0:"input/17.txt" 8 | f:{[x;y;z] $[x=s:+/b@y;,y;,/f[x;;]'[y,/z w;z d/w:&(z>last y)&~x last y) and x >= B[z] + s; 10 | // recurse 11 | raze .z.s'[x;y,/:z w;z _/:w] 12 | } 13 | 14 | count r:f[150;"";key B] 15 | /4372 16 | min count each r 17 | /4 18 | -------------------------------------------------------------------------------- /2015/18.k: -------------------------------------------------------------------------------- 1 | / --- Day 18: Like a GIF For Your Yard --- 2 | 3 | g:"#"=0:"input/18.txt" / grid 4 | c:{[x] ,/x,\/x}@!#g / coords 5 | 6 | f:{[x;y] s:+/(`b=@)#.[x;]'y+/(0 1;1 1;1 0;1 -1;0 -1;-1 -1;-1 0;-1 1) 7 | $[.[x;y] 8 | 2 3's 9 | 3=s]} 10 | s:{[x] .[x;2#,0,-1+#x;:;1b]} / set corners 11 | 12 | +/,/(100;{[x] (#x)^f[x]'c})/:g 13 | +/,/(100;{[x] s(#x)^f[x]'c})/:s g 14 | -------------------------------------------------------------------------------- /2015/18.q: -------------------------------------------------------------------------------- 1 | / --- Day 18: Like a GIF For Your Yard --- 2 | 3 | g:"#"=read0 `:input/18.txt; // build grid 4 | 5 | c:t cross t:til count g; // build coordinates 6 | 7 | f:{ 8 | s:sum .[x;] each (0 1;1 1;1 0;1 -1;0 -1;-1 -1;-1 0;-1 1)+\:y; / sum of neighbours 9 | $[.[x;y]; / check state of current light 10 | s in 2 3; 11 | s = 3] 12 | }; 13 | 14 | sum raze { (2#count x)#f[x;] peach c }/[100;g] 15 | /814 16 | 17 | / set corner lights on 18 | sc:{ 19 | m:-1 + count x; 20 | x[0;0]:1b; 21 | x[0;m]:1b; 22 | x[m;0]:1b; 23 | x[m;m]:1b; 24 | x 25 | } 26 | 27 | sum raze { sc (2#count x)#f[x;] peach c }/[100;sc g] 28 | / 924 29 | -------------------------------------------------------------------------------- /2015/19.k: -------------------------------------------------------------------------------- 1 | /--- Day 19: Medicine for Rudolph --- 2 | 3 | /ssC:*"../c/aoc.so"2:`ss!2 4 | ssC:*"../rs/target/release/libaoc.so"2:`ss!2 5 | ss:{[x;y] (),ssC[x,0x0;y,0x0]} 6 | 7 | sw:{[x;y] (y-1)_(-y)#',\x} 8 | /ss:{[x;y] $[1=#(),y;&x=*y;*{[x;y] $[0=#q:*|x;x;0=#p:*x;(1#q;1_q);(y+*|p)>*q;(p;1_q);(p,*q;1_q)]}[;#y]/:(();&y~/sw[x;#y])]} 9 | ssr:{[x;y;z] ,/@[x;i;:;(#i:1+2*!_.5*#x:(,/0,(0,#y,"")+/ss[x;y])^x)#,z]} 10 | 11 | 12 | r:{[x] (" "\x)0 2}'-2_i:0:"input/19.txt" 13 | s:{[x] `$(&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"'x)^x}@m:*|i 14 | 15 | #?,/'$,/{[x;y;z] @[x;;:;z]'&x=y}[s].'`$/r 16 | 17 | r:|'r@>#'r[;1] / arrange largest transformations first 18 | 19 | f:{[x] / (molecule;transformations) 20 | m:*p:*x 21 | t:*|p 22 | $[m~1#"e" / reached the end! 23 | ,p 24 | (+(ssr[m;;].'r@w;t+c w:&0= max/houses { 16 | res = append(res, v) 17 | } 18 | } 19 | return res 20 | } 21 | 22 | func sum(n []int) int { 23 | res := 0 24 | for _, v := range n { 25 | res += v 26 | } 27 | return res 28 | } 29 | 30 | func solve(n int) []int { 31 | factors := make(map[int]int) 32 | 33 | for i := 1; i <= int(math.Floor(math.Sqrt(float64(n)))); i++ { 34 | if n%i == 0 { 35 | factors[i] = i 36 | factors[n/i] = n / i 37 | } 38 | } 39 | 40 | res := []int{} 41 | for _, factor := range factors { 42 | res = append(res, factor) 43 | } 44 | 45 | return res 46 | } 47 | 48 | func main() { 49 | file, err := os.Open("input/20.txt") 50 | if err != nil { 51 | log.Fatal(err) 52 | } 53 | defer file.Close() 54 | 55 | var line string 56 | scanner := bufio.NewScanner(file) 57 | for scanner.Scan() { 58 | line = scanner.Text() 59 | break 60 | } 61 | 62 | limit, err := strconv.Atoi(line) 63 | if err != nil { 64 | log.Fatal(err) 65 | } 66 | 67 | i := 1 68 | for { 69 | if sum(solve(i)) >= limit/10 { 70 | fmt.Println(i) 71 | break 72 | } 73 | i++ 74 | } 75 | 76 | i = 1 77 | for { 78 | if sum(filter(solve(i), 50, i)) >= limit/11 { 79 | fmt.Println(i) 80 | break 81 | } 82 | i++ 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /2015/20.k: -------------------------------------------------------------------------------- 1 | / --- Day 20: Infinite Elves and Infinite Houses 2 | 3 | d:{[x] ?`i f,x%(f:r@&d=_d:x%r:1+!_sqrt x)} / distinct factors 4 | f:{[x;y] +/r@&~(x%y)>r:d x} 5 | 6 | ({[x;y] x>+/d y}[.1*i:`i$*0:"input/20.txt"];1+)/:0 7 | ({[x;y;z] x>f[z;y]}[i%11;50];1+)/:0 8 | -------------------------------------------------------------------------------- /2015/20.q: -------------------------------------------------------------------------------- 1 | / --- Day 20: Infinite Elves and Infinite Houses 2 | 3 | d:{ distinct f,x div f:r where 0 = x mod r:1 + til floor sqrt x }; 4 | f:{ sum r where (x%y) <= r:d x }; 5 | 6 | (1+)/[{ x > sum d y }[.1*i:"J"$first read0 `:input/20.txt;];1] 7 | /831600 8 | (1+)/[{ x > f[z;y] }[i%11;50;];1] 9 | /884520 10 | -------------------------------------------------------------------------------- /2015/21.k: -------------------------------------------------------------------------------- 1 | / --- Day 21: RPG Simulator 20XX --- 2 | 3 | W:`Dagger`Shortsword`Warhammer`Longsword`Greataxe 4 | A:`Leather`Chainmail`Splintmail`Bandedmail`Platemail`NoArmour 5 | R:`Damage1`Damage2`Damage3`Armor1`Armor2`Armor3`NoRing`NoRing2 6 | I:+`Name`Cost`Damage`Armor!(W,A,R;8 10 25 40 74,13 31 53 75 102 0,25 50 100 20 40 80 0 0;4 5 6 7 8, 0 0 0 0 0 0, 1 2 3 0 0 0 0 0;0 0 0 0 0, 1 2 3 4 5 0, 0 0 0 1 2 3 0 0) 7 | 8 | Rc:R@?^'{[x] ,/{[x;y] y,'((),y)_x}[x;]'x}@!#R / unique ring combinations 9 | 10 | cross:{[x;y] ,/x,\/y} 11 | c:cross[cross[W;A];Rc] / all item combinations 12 | 13 | A:()!() 14 | ct:{[x] 15 | A[`L]:x / global var hack 16 | s:select from I where Name in A[`L] 17 | select sum Cost, sum Armor, sum Damage from s}'c 18 | ct:ct@<*+`Cost#ct / sort by ascending cost 19 | 20 | e:"i"$last'" "\'0:"input/21.txt" / enemy (hp;damage;armor) 21 | 22 | t:{[x] d:last x;$[|/0>*'x;x;(d-(1|a[1]-d 2;0;0);a:*x)]} / take turn 23 | g:{[x;y] ~2 mod#t\:(x;y)}[;e]'100,'.'`Damage`Armor#ct / player has 100 hp 24 | 25 | *`Cost#ct@*&g / cheapest win 26 | *`Cost#ct@last&~g / priciest loss 27 | -------------------------------------------------------------------------------- /2015/23.k: -------------------------------------------------------------------------------- 1 | /--- Day 23: Opening the Turing Lock --- 2 | 3 | f:{[x;y] 4 | a:y 0;b:y 1;i:x pc:y 2; 5 | $["hlf"~i 0 6 | $["a"=*i 1;.5 1 1;1 .5 1]*(a;b;pc+1) 7 | "tpl"~i 0 8 | $["a"=*i 1;3 1 1;1 3 1]*(a;b;pc+1) 9 | "inc"~i 0 10 | $["a"=*i 1;1 0 1;0 1 1]+(a;b;pc) 11 | "jmp"~i 0 12 | (a;b;pc+`i$i 1) 13 | "jie"~i 0 14 | (a;b;pc+$[0=2 mod _y@"ab"?*i 1;`i$i 2;1]) 15 | "jio"~i 0 16 | (a;b;pc+$[1=y@"ab"?*i 1;`i$i 2;1]) 17 | (a;b;pc)]} 18 | 19 | @[;1]@ f[p:{[x] " "\",+"_x}'0:"input/23.txt"]/:0 0 0 20 | @[;1]@ f[p]/:1 0 0 21 | -------------------------------------------------------------------------------- /2015/23.py: -------------------------------------------------------------------------------- 1 | # --- Day 23: Opening the Turing Lock --- 2 | 3 | def day_23(instructions, registers): 4 | 5 | inst = [] 6 | for line in instructions: 7 | inst.append(line.replace("\n", "").split(" ")) 8 | 9 | counter = 0 10 | while counter < len(inst): 11 | x = inst[counter] 12 | if x[0] == "hlf": 13 | registers[x[1]] = registers[x[1]] / 2 14 | counter += 1 15 | elif x[0] == "tpl": 16 | registers[x[1]] = registers[x[1]] * 3 17 | counter += 1 18 | elif x[0] == "inc": 19 | registers[x[1]] = registers[x[1]] + 1 20 | counter += 1 21 | elif x[0] == "jmp": 22 | counter += int(x[1]) 23 | elif x[0] == "jie": 24 | if registers[x[1].replace(",", "")] % 2 == 0: 25 | counter += int(x[2]) 26 | else: 27 | counter += 1 28 | elif x[0] == "jio": 29 | if registers[x[1].replace(",", "")] == 1: 30 | counter += int(x[2]) 31 | else: 32 | counter += 1 33 | 34 | return registers["b"] 35 | 36 | with open("input/23.txt") as instructions: 37 | print(day_23(instructions, {"a": 0, "b": 0})) 38 | with open("input/23.txt") as instructions: 39 | print(day_23(instructions, {"a": 1, "b": 0})) 40 | -------------------------------------------------------------------------------- /2015/23.q: -------------------------------------------------------------------------------- 1 | /--- Day 23: Opening the Turing Lock --- 2 | 3 | f.hlf:{ 4 | r[`$x]:r[`$x] % 2; 5 | 1 6 | }; 7 | f.tpl:{ 8 | r[`$x]*:3; 9 | 1 10 | }; 11 | f.inc:{ 12 | r[`$x]+:1; 13 | 1 14 | }; 15 | f.jmp:{ 16 | -7h$x 17 | }; 18 | f.jie:{ 19 | $[floor[r[`$x]] mod 2;1;-7h$y] 20 | }; 21 | f.jio:{ 22 | $[r[`$x]=1;-7h$y;1] 23 | }; 24 | 25 | run:{[a] 26 | r.a:a; 27 | r.b:0; 28 | pc:0; 29 | while[count[i]>pc+:f . i pc]; 30 | r.b 31 | }; 32 | 33 | i:.[" "vs'except[;","]@'read0 `:input/23.txt;(::;0);`$] 34 | 35 | run 0 36 | /170 37 | run 1 38 | /247 39 | -------------------------------------------------------------------------------- /2015/24.k: -------------------------------------------------------------------------------- 1 | /--- Day 24: It Hangs in the Balance --- 2 | 3 | d:{[x;y] x y_#x} / drop 4 | 5 | f:{[x;y;z] 6 | $[x=s:+/y 7 | ,y 8 | ,/f[x;;]'[y,/z w;z d/w:&(z<*|y)&~x= z + s; 8 | // recurse 9 | raze .z.s'[x;y,/:z w;z _/:w] 10 | }; 11 | 12 | f2:{ f[x;1#y;1_ y] }; 13 | 14 | r:f2[sum[i] div 3;] i:desc "J"$read0 `:input/24.txt; 15 | min prd each r:r where c=min c:count each r 16 | /10439961859 17 | 18 | r:f2[sum[i] div 4;i]; 19 | min prd each r:r where c=min c:count each r 20 | /72050269 21 | -------------------------------------------------------------------------------- /2015/25.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "log" 7 | "os" 8 | "regexp" 9 | "strconv" 10 | ) 11 | 12 | func main() { 13 | 14 | file, err := os.Open("input/25.txt") 15 | if err != nil { 16 | log.Fatal(err) 17 | } 18 | 19 | var text string 20 | scanner := bufio.NewScanner(file) 21 | for scanner.Scan() { 22 | text = scanner.Text() 23 | } 24 | 25 | re := regexp.MustCompile("Enter the code at row ([0-9]+), column ([0-9]+).") 26 | res := re.FindStringSubmatch(text) 27 | if res == nil { 28 | log.Fatal("Regex failed on input", text) 29 | } 30 | row, err := strconv.Atoi(res[1]) 31 | if err != nil { 32 | log.Fatal(err) 33 | } 34 | column, err := strconv.Atoi(res[2]) 35 | if err != nil { 36 | log.Fatal(err) 37 | } 38 | 39 | entry := 0 40 | // find row 41 | for i := 1; i < row; i++ { 42 | entry += i 43 | } 44 | // find column 45 | for i := 1; i < column; i++ { 46 | entry += row + i 47 | } 48 | 49 | i := 0 50 | code := 20151125 51 | for { 52 | if i == entry { 53 | fmt.Println(code) 54 | break 55 | } 56 | code = (code * 252533) % 33554393 57 | i++ 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /2015/25.k: -------------------------------------------------------------------------------- 1 | / --- Day 25: Let It Snow --- 2 | 3 | i:{[x] `i$(" "\",."_x) 16 18}@*0:"input/25.txt" 4 | n:{[x;y;z] x++/y+1+!z-1}[1++/!*i;;] . i 5 | 6 | (n-1;{[x] 33554393 mod 252533*x})/:20151125 7 | -------------------------------------------------------------------------------- /2015/25.q: -------------------------------------------------------------------------------- 1 | / --- Day 25: Let It Snow --- 2 | 3 | i:"J"$","vs 1_inter[;.Q.n,","]first read0 `:input/25.txt; 4 | R:i 0 5 | C:i 1 6 | 7 | / calculate row value 8 | r:1 + sum til R 9 | 10 | / calculate value at RxC 11 | n:{ x + sum y + 1 + til z - 1 }[r;R;C] 12 | 13 | / iterate to n-1 14 | (mod[;33554393]252533*)/[n-1;20151125] 15 | /2650453 16 | -------------------------------------------------------------------------------- /2015/input/04.txt: -------------------------------------------------------------------------------- 1 | bgvyzdsv 2 | -------------------------------------------------------------------------------- /2015/input/09.txt: -------------------------------------------------------------------------------- 1 | Tristram to AlphaCentauri = 34 2 | Tristram to Snowdin = 100 3 | Tristram to Tambi = 63 4 | Tristram to Faerun = 108 5 | Tristram to Norrath = 111 6 | Tristram to Straylight = 89 7 | Tristram to Arbre = 132 8 | AlphaCentauri to Snowdin = 4 9 | AlphaCentauri to Tambi = 79 10 | AlphaCentauri to Faerun = 44 11 | AlphaCentauri to Norrath = 147 12 | AlphaCentauri to Straylight = 133 13 | AlphaCentauri to Arbre = 74 14 | Snowdin to Tambi = 105 15 | Snowdin to Faerun = 95 16 | Snowdin to Norrath = 48 17 | Snowdin to Straylight = 88 18 | Snowdin to Arbre = 7 19 | Tambi to Faerun = 68 20 | Tambi to Norrath = 134 21 | Tambi to Straylight = 107 22 | Tambi to Arbre = 40 23 | Faerun to Norrath = 11 24 | Faerun to Straylight = 66 25 | Faerun to Arbre = 144 26 | Norrath to Straylight = 115 27 | Norrath to Arbre = 135 28 | Straylight to Arbre = 127 29 | -------------------------------------------------------------------------------- /2015/input/10.txt: -------------------------------------------------------------------------------- 1 | 1113222113 2 | -------------------------------------------------------------------------------- /2015/input/11.txt: -------------------------------------------------------------------------------- 1 | vzbxkghb 2 | -------------------------------------------------------------------------------- /2015/input/14.txt: -------------------------------------------------------------------------------- 1 | Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds. 2 | Cupid can fly 8 km/s for 17 seconds, but then must rest for 114 seconds. 3 | Prancer can fly 18 km/s for 6 seconds, but then must rest for 103 seconds. 4 | Donner can fly 25 km/s for 6 seconds, but then must rest for 145 seconds. 5 | Dasher can fly 11 km/s for 12 seconds, but then must rest for 125 seconds. 6 | Comet can fly 21 km/s for 6 seconds, but then must rest for 121 seconds. 7 | Blitzen can fly 18 km/s for 3 seconds, but then must rest for 50 seconds. 8 | Vixen can fly 20 km/s for 4 seconds, but then must rest for 75 seconds. 9 | Dancer can fly 7 km/s for 20 seconds, but then must rest for 119 seconds. 10 | -------------------------------------------------------------------------------- /2015/input/15.txt: -------------------------------------------------------------------------------- 1 | Sugar: capacity 3, durability 0, flavor 0, texture -3, calories 2 2 | Sprinkles: capacity -3, durability 3, flavor 0, texture 0, calories 9 3 | Candy: capacity -1, durability 0, flavor 4, texture 0, calories 1 4 | Chocolate: capacity 0, durability 0, flavor -2, texture 2, calories 8 5 | -------------------------------------------------------------------------------- /2015/input/17.txt: -------------------------------------------------------------------------------- 1 | 11 2 | 30 3 | 47 4 | 31 5 | 32 6 | 36 7 | 3 8 | 1 9 | 5 10 | 3 11 | 32 12 | 36 13 | 15 14 | 11 15 | 46 16 | 26 17 | 28 18 | 1 19 | 19 20 | 3 21 | -------------------------------------------------------------------------------- /2015/input/19.txt: -------------------------------------------------------------------------------- 1 | Al => ThF 2 | Al => ThRnFAr 3 | B => BCa 4 | B => TiB 5 | B => TiRnFAr 6 | Ca => CaCa 7 | Ca => PB 8 | Ca => PRnFAr 9 | Ca => SiRnFYFAr 10 | Ca => SiRnMgAr 11 | Ca => SiTh 12 | F => CaF 13 | F => PMg 14 | F => SiAl 15 | H => CRnAlAr 16 | H => CRnFYFYFAr 17 | H => CRnFYMgAr 18 | H => CRnMgYFAr 19 | H => HCa 20 | H => NRnFYFAr 21 | H => NRnMgAr 22 | H => NTh 23 | H => OB 24 | H => ORnFAr 25 | Mg => BF 26 | Mg => TiMg 27 | N => CRnFAr 28 | N => HSi 29 | O => CRnFYFAr 30 | O => CRnMgAr 31 | O => HP 32 | O => NRnFAr 33 | O => OTi 34 | P => CaP 35 | P => PTi 36 | P => SiRnFAr 37 | Si => CaSi 38 | Th => ThCa 39 | Ti => BP 40 | Ti => TiTi 41 | e => HF 42 | e => NAl 43 | e => OMg 44 | 45 | CRnSiRnCaPTiMgYCaPTiRnFArSiThFArCaSiThSiThPBCaCaSiRnSiRnTiTiMgArPBCaPMgYPTiRnFArFArCaSiRnBPMgArPRnCaPTiRnFArCaSiThCaCaFArPBCaCaPTiTiRnFArCaSiRnSiAlYSiThRnFArArCaSiRnBFArCaCaSiRnSiThCaCaCaFYCaPTiBCaSiThCaSiThPMgArSiRnCaPBFYCaCaFArCaCaCaCaSiThCaSiRnPRnFArPBSiThPRnFArSiRnMgArCaFYFArCaSiRnSiAlArTiTiTiTiTiTiTiRnPMgArPTiTiTiBSiRnSiAlArTiTiRnPMgArCaFYBPBPTiRnSiRnMgArSiThCaFArCaSiThFArPRnFArCaSiRnTiBSiThSiRnSiAlYCaFArPRnFArSiThCaFArCaCaSiThCaCaCaSiRnPRnCaFArFYPMgArCaPBCaPBSiRnFYPBCaFArCaSiAl 46 | -------------------------------------------------------------------------------- /2015/input/20.txt: -------------------------------------------------------------------------------- 1 | 36000000 2 | -------------------------------------------------------------------------------- /2015/input/21.txt: -------------------------------------------------------------------------------- 1 | Hit Points: 100 2 | Damage: 8 3 | Armor: 2 4 | -------------------------------------------------------------------------------- /2015/input/22.txt: -------------------------------------------------------------------------------- 1 | Hit Points: 51 2 | Damage: 9 3 | -------------------------------------------------------------------------------- /2015/input/23.txt: -------------------------------------------------------------------------------- 1 | jio a, +16 2 | inc a 3 | inc a 4 | tpl a 5 | tpl a 6 | tpl a 7 | inc a 8 | inc a 9 | tpl a 10 | inc a 11 | inc a 12 | tpl a 13 | tpl a 14 | tpl a 15 | inc a 16 | jmp +23 17 | tpl a 18 | inc a 19 | inc a 20 | tpl a 21 | inc a 22 | inc a 23 | tpl a 24 | tpl a 25 | inc a 26 | inc a 27 | tpl a 28 | inc a 29 | tpl a 30 | inc a 31 | tpl a 32 | inc a 33 | inc a 34 | tpl a 35 | inc a 36 | tpl a 37 | tpl a 38 | inc a 39 | jio a, +8 40 | inc b 41 | jie a, +4 42 | tpl a 43 | inc a 44 | jmp +2 45 | hlf a 46 | jmp -7 47 | -------------------------------------------------------------------------------- /2015/input/24.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 5 4 | 11 5 | 13 6 | 17 7 | 19 8 | 23 9 | 29 10 | 31 11 | 37 12 | 41 13 | 43 14 | 47 15 | 53 16 | 59 17 | 67 18 | 71 19 | 73 20 | 79 21 | 83 22 | 89 23 | 97 24 | 101 25 | 103 26 | 107 27 | 109 28 | 113 29 | -------------------------------------------------------------------------------- /2015/input/25.txt: -------------------------------------------------------------------------------- 1 | To continue, please consult the code grid in the manual. Enter the code at row 2978, column 3083. 2 | -------------------------------------------------------------------------------- /2016/01.py: -------------------------------------------------------------------------------- 1 | """--- Day 1: No Time for a Taxicab ---""" 2 | 3 | def manhattan(x, y): 4 | return abs(x) + abs(y) 5 | 6 | with open("input/01.txt", "r") as f: 7 | # start at 0,0 facing north 8 | x = y = heading = 0 9 | positions = [(0, 0)] 10 | 11 | for instruction in f.read().split(","): 12 | # strip off the whitespace 13 | instruction = instruction.strip() 14 | # could use regex... 15 | direction, blocks = instruction[0], int(instruction[1:]) 16 | # mod 4 to keep us in the confines of reality 17 | heading = (heading + (1 if direction == "R" else -1)) % 4 18 | 19 | for i in range(blocks): 20 | if heading == 0: 21 | # heading north 22 | y += 1 23 | elif heading == 1: 24 | # heading east 25 | x += 1 26 | elif heading == 2: 27 | # heading south 28 | y -= 1 29 | else: 30 | # heading west 31 | x -= 1 32 | positions.append((x, y)) 33 | 34 | print(manhattan(x, y)) 35 | 36 | hq = [x for x in positions if positions.count(x) > 1][0] 37 | print(manhattan(hq[0], hq[1])) 38 | -------------------------------------------------------------------------------- /2016/01.q: -------------------------------------------------------------------------------- 1 | /--- Day 1: No Time for a Taxicab --- 2 | 3 | f:{ (("J"$1_y)#enlist(0 1;1 0;0 -1;-1 0)"NESW"?d;d:$["R"=first y;"ESWN";"WNES"]"NESW"?last x) }\["N";] 4 | 5 | sum abs last s:sums raze first flip f ", " vs first read0 `:input/01.txt 6 | /181 7 | sum abs s first where 1 0 and (keypad[row-1][col] is not None): 15 | row -= 1 16 | elif instruction == "D": 17 | if row < rc and (keypad[row+1][col] is not None): 18 | row += 1 19 | elif instruction == "L": 20 | if col > 0 and (keypad[row][col-1] is not None): 21 | col -= 1 22 | elif instruction == "R": 23 | if col < rc and (keypad[row][col+1] is not None): 24 | col += 1 25 | elif instruction == "\n": 26 | code.append(keypad[row][col]) 27 | 28 | return "".join(map(str, code)) 29 | 30 | with open("input/02.txt", "r") as f: 31 | instructions = f.read() 32 | 33 | keypad = {} 34 | 35 | # part 1 36 | keypad[0] = [None, None, None, None, None] 37 | keypad[1] = [None, 1, 2, 3 , None] 38 | keypad[2] = [None, 4, 5, 6 , None] 39 | keypad[3] = [None, 7, 8, 9 , None] 40 | keypad[4] = [None, None, None, None, None] 41 | 42 | print(solve(keypad, instructions, 2, 2)) # start on 5 43 | 44 | # part 2 45 | keypad[0] = [None, None, 1, None, None] 46 | keypad[1] = [None, 2, 3, 4, None] 47 | keypad[2] = [5, 6, 7, 8, 9 ] 48 | keypad[3] = [None, "A", "B", "C", None] 49 | keypad[4] = [None, None, "D", None, None] 50 | 51 | print(solve(keypad, instructions, 0, 2)) # start on 5 52 | -------------------------------------------------------------------------------- /2016/02.q: -------------------------------------------------------------------------------- 1 | /--- Day 2: Bathroom Security --- 2 | 3 | move:{ $[null x . n:y + (-1 0;1 0;0 -1;0 1)"UDLR"?z;y;n] } 4 | 5 | k:(" "; 6 | " 123 "; 7 | " 456 "; 8 | " 789 "; 9 | " ") 10 | 11 | k2:(" "; 12 | " 1 "; 13 | " 234 "; 14 | " 56789 "; 15 | " ABC "; 16 | " D "; 17 | " ") 18 | 19 | k ./:{ move[x]/[y;z] }[k]\[2 2;] i:read0 `:input/02.txt 20 | /35749 21 | k2 ./:{ move[x]/[y;z] }[k2]\[3 3;i] 22 | /9365C 23 | -------------------------------------------------------------------------------- /2016/03.py: -------------------------------------------------------------------------------- 1 | """--- Day 3: Squares With Three Sides ---""" 2 | 3 | import re 4 | 5 | with open("input/03.txt", "r") as f: 6 | valid_1 = valid_2 = 0 7 | 8 | a, b, c = [], [], [] 9 | 10 | for instruction in f: 11 | sides = [int(x) for x in re.findall(" [0-9]+", instruction)] 12 | 13 | # this is for part 2 14 | a.append(sides[0]) 15 | b.append(sides[1]) 16 | c.append(sides[2]) 17 | 18 | # solve part 1 19 | sides.sort() 20 | if sides[0] + sides[1] > sides[2]: 21 | valid_1 += 1 22 | 23 | print(valid_1) 24 | 25 | for i in range(0, len(a), 3): 26 | # row a 27 | sides = a[i:i+3] 28 | sides.sort() 29 | if sides[0] + sides[1] > sides[2]: 30 | valid_2 += 1 31 | 32 | # row b 33 | sides = b[i:i+3] 34 | sides.sort() 35 | if sides[0] + sides[1] > sides[2]: 36 | valid_2 += 1 37 | 38 | # row c 39 | sides = c[i:i+3] 40 | sides.sort() 41 | if sides[0] + sides[1] > sides[2]: 42 | valid_2 += 1 43 | 44 | print(valid_2) 45 | -------------------------------------------------------------------------------- /2016/03.q: -------------------------------------------------------------------------------- 1 | /--- Day 3: Squares With Three Sides --- 2 | 3 | f:{ (sum x 0 1)>last x:asc x } 4 | 5 | sum f each r:{ "J"$ 0 5 10 _ x } each read0 `:input/03.txt 6 | /982 7 | sum f each 3 cut raze flip r 8 | /1826 9 | -------------------------------------------------------------------------------- /2016/04.py: -------------------------------------------------------------------------------- 1 | # --- Day 4: Security Through Obscurity --- 2 | 3 | import re 4 | 5 | with open("input/04.txt", "r") as rooms: 6 | sectors = northpole_objects = 0 7 | alphabet = "abcdefghijklmnopqrstuvwxyz" 8 | 9 | for room in rooms: 10 | regex = re.search(r"([a-z\-]*)-([0-9]+)\[([a-z]*)\]", room) 11 | 12 | roomname = regex.group(1) 13 | sector = int(regex.group(2)) 14 | checksum = regex.group(3) 15 | 16 | letters = "".join(roomname.split("-")) 17 | 18 | uniq = {} 19 | # calculated checksum 20 | cs = "" 21 | 22 | for l in letters: 23 | if not l in uniq.keys(): 24 | uniq[l] = letters.count(l) 25 | 26 | i = max(uniq.values()) # work backwards down to 1 27 | 28 | while i > 0: 29 | chars = [k for k, v in uniq.items() if v == i] 30 | chars.sort() 31 | cs += "".join(chars) 32 | i -= 1 33 | 34 | if(cs[:5] == checksum): 35 | sectors += sector 36 | 37 | decrypted = "" 38 | 39 | for r in roomname: 40 | if r == "-": 41 | decrypted += " " 42 | else: 43 | loc = alphabet.find(r) 44 | decrypted += alphabet[(loc + sector) % len(alphabet)] 45 | 46 | if decrypted == "northpole object storage": 47 | northpole_objects = sector 48 | 49 | print(sectors) 50 | print(northpole_objects) 51 | -------------------------------------------------------------------------------- /2016/04.q: -------------------------------------------------------------------------------- 1 | /--- Day 4: Security Through Obscurity --- 2 | 3 | res:{(r;$[(-1_-6#x)~5#.Q.a idesc sum .Q.a=/:r:except[;"-"](first where x in .Q.n)#x;"J"$x inter .Q.n;0])} each read0 `:input/04.txt 4 | sum res[;1] 5 | /245102 6 | last res where ({ "c"$97+mod[y+-97+"j"$x;26] }.' res) like "*pole*" 7 | /324 -------------------------------------------------------------------------------- /2016/05.py: -------------------------------------------------------------------------------- 1 | """--- Day 5: How About a Nice Game of Chess? ---""" 2 | 3 | import hashlib 4 | 5 | password_length = 8 6 | 7 | password_part1 = [] 8 | password_part2 = ["" for x in range(password_length)] 9 | 10 | prefix = "ffykfhsq" 11 | 12 | i = 0 13 | while True: 14 | plaintext = prefix + str(i) 15 | m = hashlib.md5(plaintext.encode("utf-8")) 16 | hd = m.hexdigest() 17 | if hd.startswith("00000"): 18 | # part 1 19 | pos = hd[5] 20 | password_part1.append(pos) 21 | # part 2 22 | pos = int(pos, 16) 23 | if (pos < password_length) and password_part2[pos] == "": 24 | password_part2[pos] = hd[6] 25 | if "" not in password_part2: 26 | # we're done 27 | break 28 | i += 1 29 | 30 | print("".join(password_part1[:password_length])) 31 | print("".join(password_part2)) 32 | -------------------------------------------------------------------------------- /2016/05.q: -------------------------------------------------------------------------------- 1 | /--- Day 5: How About a Nice Game of Chess? --- 2 | 3 | p:first read0 `:input/05.txt 4 | pw:8#" "; 5 | 6 | r:( 7 | (1+)/[{ 16<256 sv 3#"j"$md5 p,string x };]1+ 8 | )\[ 9 | { 10 | m:raze string 4#md5 p,string x; 11 | if[(x>0)&(null pw[i:"J"$m 5])&m[5]in"01234567"; 12 | pw[i]:m 6 13 | ]; 14 | any null pw 15 | };-1] 16 | 17 | (raze each string md5@'p,/:string 1_r)[til 8;5] 18 | /c6697b55 19 | pw 20 | /8c35d1ab 21 | -------------------------------------------------------------------------------- /2016/06.py: -------------------------------------------------------------------------------- 1 | """--- Day 6: Signals and Noise ---""" 2 | 3 | with open("input/06.txt", "r") as f: 4 | transposed = [] 5 | 6 | part_1 = part_2 = "" 7 | 8 | first = True 9 | for instruction in f: 10 | # intialise transposed on first instruction 11 | if first: 12 | transposed = [{} for x in range(len(instruction))] 13 | first = False 14 | for i, c in enumerate(instruction): 15 | if c in transposed[i].keys(): 16 | transposed[i][c] += 1 17 | else: 18 | transposed[i][c] = 1 19 | 20 | for t in transposed: 21 | part_1 += [k for k, v in t.items() if v == max(t.values())][0] 22 | part_2 += [k for k, v in t.items() if v == min(t.values())][0] 23 | 24 | print(part_1[:-1]) 25 | print(part_2[:-1]) 26 | -------------------------------------------------------------------------------- /2016/06.q: -------------------------------------------------------------------------------- 1 | /--- Day 6: Signals and Noise --- 2 | 3 | raze where@'c=max@'c:count@''group@'flip read0 `:input/06.txt 4 | /bjosfbce 5 | raze where@'c=min@'c 6 | /veqfxzfx 7 | -------------------------------------------------------------------------------- /2016/07.q: -------------------------------------------------------------------------------- 1 | /--- Day 7: Internet Protocol Version 7 --- 2 | 3 | sw4:-4_4#'next\ 4 | sw3:-3_3#'next\ 5 | f:{ x where (not x[;0]=x[;1])&x~'reverse each x } 6 | 7 | sum raze { 8 | a:f@'sw4@'x; 9 | (not count raze a[where not e])&count raze a where e:0=mod[;2] til count x 10 | } each r:{ (0,where any "[]"=\:x) cut x } each read0 `:input/07.txt 11 | /118 12 | 13 | sum raze { 14 | a:f@'sw3@'x; 15 | sa:raze a where e:0=mod[;2] til count x; 16 | hb:raze[a where not e][;1 0 1]; 17 | any hb in\:sa 18 | } each r 19 | /260 20 | -------------------------------------------------------------------------------- /2016/08.py: -------------------------------------------------------------------------------- 1 | """--- Day 8: Two-Factor Authentication ---""" 2 | 3 | def rotate(row, n): 4 | i = n % len(row) 5 | return row[-i:] + row[:-i] 6 | 7 | with open("input/08.txt", "r") as instructions: 8 | width, height = 50, 6 9 | 10 | # initialise display 11 | lcd = [[" " for i in range(width)] for i in range(height)] 12 | 13 | for instruction in instructions: 14 | if instruction.startswith("rect"): 15 | # e.g. 'rect 3x2' 16 | x, y = [int(x) for x in instruction.split(" ")[1].split("x")] 17 | # light em up 18 | for i in range(y): 19 | for j in range(x): 20 | lcd[i][j] = "#" 21 | else: 22 | # e.g. 'rotate column x=1 by 1' 23 | _, rc, xy, _, by = instruction.split(" ") 24 | 25 | if rc == "row": 26 | y = int(xy.split("=")[-1]) 27 | lcd[y] = rotate(lcd[y], int(by)) 28 | else: 29 | x = int(xy.split("=")[-1]) 30 | col = rotate([l[x] for l in lcd], int(by)) 31 | for l in lcd: 32 | l[x] = col.pop(0) 33 | 34 | part1 = 0 35 | part2 = "\n" 36 | for row in lcd: 37 | part2 += "".join(row) + "\n" 38 | for led in row: 39 | if led == "#": 40 | part1 += 1 41 | 42 | print(part1) 43 | print(part2) 44 | -------------------------------------------------------------------------------- /2016/08.q: -------------------------------------------------------------------------------- 1 | /--- Day 8: Two-Factor Authentication --- 2 | 3 | l:50 6#0 4 | 5 | { 6 | if[2=count x; 7 | .[`l;;:;1] til each "J"$"x"vs last x 8 | ]; 9 | if[5=count x; 10 | a:"J"$last "=" vs x 2; 11 | b:neg"J"$last x; 12 | $["x"=x[2;0]; 13 | [ 14 | f[;a]:b rotate (f:flip l)[;a]; 15 | l::flip f 16 | ]; 17 | l[;a]:b rotate l[;a] 18 | ]; 19 | ]; 20 | } each " "vs'read0 `:input/08.txt; 21 | 22 | sum raze l 23 | /110 24 | -1" #"flip l; 25 | /#### ## # # ### # # ## ### # # # ## 26 | / # # # # # # # # # # # # # # # # 27 | / # # #### # # ## # # # # # # # 28 | / # # # # ### # # # ### # # # 29 | /# # # # # # # # # # # # # # # # 30 | /#### ## # # # # # # ## # #### # ## 31 | -------------------------------------------------------------------------------- /2016/09.py: -------------------------------------------------------------------------------- 1 | """--- Day 9: Explosives in Cyberspace ---""" 2 | 3 | def recurse(chomp, recurse_level, recurse_once): 4 | 5 | if recurse_once and recurse_level > 0: 6 | return len(chomp) 7 | 8 | i = length = 0 9 | while i < len(chomp): 10 | if chomp[i] == "(": 11 | rb = chomp.find(")", i) 12 | marker = chomp[i+1:rb] 13 | count, multi = [int(x) for x in marker.split("x")] 14 | length += multi * recurse(chomp[rb+1:rb+1+count], recurse_level+1, recurse_once) 15 | i = rb + count + 1 # skip over "(AxB)..." 16 | elif chomp[i] == "\n": 17 | i += 1 18 | else: 19 | length += 1 20 | i += 1 21 | 22 | return length 23 | 24 | with open("input/09.txt", "r") as f: 25 | 26 | part1 = part2 = 0 27 | 28 | for instruction in f: 29 | part1 += recurse(instruction, 0, True) 30 | part2 += recurse(instruction, 0, False) 31 | 32 | print(part1) 33 | print(part2) 34 | -------------------------------------------------------------------------------- /2016/09.q: -------------------------------------------------------------------------------- 1 | /--- Day 9: Explosives in Cyberspace --- 2 | 3 | f:{[x;r] 4 | i:0; 5 | l:0; 6 | while[i < count x; 7 | $["("=x i; 8 | [ 9 | ml:first ss[i _ x;")"]; 10 | m:"J"$"x" vs x i + 1 + til ml - 1; 11 | s:first m; 12 | l+:m[1]*$[r;.z.s[x i + ml + 1 + til m[0];r];m[0]]; 13 | i+:ml + s + 1 14 | ] 15 | ; 16 | [ 17 | l+:1; 18 | i+:1 19 | ] 20 | ]; 21 | ]; 22 | l 23 | }; 24 | 25 | f[;0b] i:first read0 `:input/09.txt 26 | / 98135 27 | f[;1b] i 28 | / 10964557606 29 | -------------------------------------------------------------------------------- /2016/10.q: -------------------------------------------------------------------------------- 1 | /--- Day 10: Balance Bots --- 2 | 3 | i:" "vs'read0 `:input/10.txt 4 | chk:17 61 5 | 6 | / bots 7 | b:{ til[x]!x#enlist () } max "J"$raze (i where 12=count each i)[;1 6 11] 8 | / assign chips 9 | { b[x],:(),y }.'"J"$(i where 6=count each i)[;5 1]; 10 | / create outputs 11 | o:(til count b)!(count b)#enlist () 12 | / rules 13 | r:raze { (enlist"J"$first x)!enlist 2 cut 1_x } each (i where 12=count each i)[;1 5 6 10 11] 14 | 15 | while[sum t:2=count each b; 16 | / chips 17 | c:asc b w:first where t; 18 | / remove from bot 19 | b[w]:enlist (); 20 | / check part 1 21 | if[c~chk; 22 | -1@string w 23 | / 157 24 | ]; 25 | / reassign chips 26 | { $["bot"~r[x;y;0]; 27 | b["J"$r[x;y;1]],:c y; 28 | o["J"$r[x;y;1]],:c y] 29 | }[w;] each 0 1 30 | ]; 31 | 32 | first prd o 0 1 2 33 | / 1085 34 | -------------------------------------------------------------------------------- /2016/12.py: -------------------------------------------------------------------------------- 1 | """--- Day 12: Leonardo's Monorail ---""" 2 | 3 | def solve(instructions, registers): 4 | # intialise program counter to zero 5 | cnt = 0 6 | 7 | while cnt < len(instructions): 8 | inst = instructions[cnt].split(" ") 9 | if inst[0][0] == "c": # cpy 10 | if inst[1] in registers: 11 | registers[inst[2]] = registers[inst[1]] 12 | else: 13 | registers[inst[2]] = int(inst[1]) 14 | elif inst[0][0] == "i": # inc 15 | registers[inst[1]] += 1 16 | elif inst[0][0] == "d": # dec 17 | registers[inst[1]] -= 1 18 | else: # jnz 19 | if inst[1] in registers: 20 | if registers[inst[1]] != 0: 21 | cnt += int(inst[2]) 22 | continue 23 | else: 24 | if int(inst[1]) != 0: 25 | cnt += int(inst[2]) 26 | continue 27 | # increment program counter 28 | cnt += 1 29 | return registers["a"] 30 | 31 | with open("input/12.txt", "r") as f: 32 | instructions = [x[:-1] for x in f] # drop "\n" 33 | 34 | print(solve(instructions, {"a" : 0, "b" : 0, "c" : 0, "d" : 0})) 35 | print(solve(instructions, {"a" : 0, "b" : 0, "c" : 1, "d" : 0})) 36 | -------------------------------------------------------------------------------- /2016/12.q: -------------------------------------------------------------------------------- 1 | /--- Day 12: Leonardo's Monorail --- 2 | 3 | cpy:{ 4 | r[first y]:$[any x in "abcd";r[first x];"J"$x]; 5 | 1 6 | } 7 | inc:{ 8 | r[first x]+:1; 9 | 1 10 | } 11 | dec:{ 12 | r[first x]-:1; 13 | 1 14 | } 15 | jnz:{ 16 | $[r first x;"J"$y;1] 17 | } 18 | 19 | inst:" "vs'read0 `:input/12.txt 20 | 21 | ip:0;r:"abcd"!0 0 0 0 22 | while[count[inst]>ip+:(`$i 0). 1 _ i:inst ip]; 23 | r"a" 24 | /318077 25 | 26 | / first 40 fibonacci numbers 27 | fib40:{ x,sum -2#x }/[38;1 1] 28 | fib40[27] + 14*19 29 | /318077 30 | fib40[27+7] + 14*19 31 | /9227731 32 | -------------------------------------------------------------------------------- /2016/13.py: -------------------------------------------------------------------------------- 1 | """--- Day 13: A Maze of Twisty Little Cubicles ---""" 2 | 3 | def decisions(x, y): 4 | d = [] 5 | # check up 6 | if x > 0: 7 | if office[y][x-1] == ".": d.append([x-1,y]) 8 | # check down 9 | if x < X -1: 10 | if office[y][x+1] == ".": d.append([x+1,y]) 11 | # check left 12 | if y > 0: 13 | if office[y-1][x] == ".": d.append([x,y-1]) 14 | if y < Y -1: 15 | if office[y+1][x] == ".": d.append([x,y+1]) 16 | return d 17 | 18 | def solve(x, y, steps, destination, max_steps): 19 | if 0 < max_steps < len(steps): 20 | return 21 | if [x, y] not in visited and len(steps) <= 50: 22 | visited.append([x,y]) 23 | 24 | if [x,y] == destination: 25 | results.append(steps) 26 | else: 27 | ds = decisions(x, y) 28 | d = [a for a in ds if a not in steps] 29 | 30 | for dd in d: 31 | solve(dd[0], dd[1], steps + [[x,y]], destination, max_steps) 32 | 33 | office = [] 34 | results = [] 35 | visited = [] 36 | 37 | with open("input/13.txt", "r") as instructions: 38 | favourite_number = int(instructions.read()) 39 | X = Y = 100 40 | # pre-populate 100x100 grid 41 | for y in range(Y): 42 | office.append([]) 43 | for x in range(X): 44 | res = x*x + 3*x + 2*x*y + y + y*y + favourite_number 45 | b = str(bin(res))[2:] 46 | ones = [a for a in b if a == "1"] 47 | office[y].append("." if len(ones) % 2 == 0 else "#") 48 | 49 | # optimise so we dont take too long 50 | solve(1, 1, [], [31, 39], X) 51 | 52 | results.sort(key=lambda x: len(x)) 53 | 54 | print(len(results[0])) 55 | print(len(visited)) 56 | -------------------------------------------------------------------------------- /2016/13.q: -------------------------------------------------------------------------------- 1 | /--- Day 13: A Maze of Twisty Little Cubicles --- 2 | 3 | f:{[x;y;z] z+(x*x)+(3*x)+(2*x*y)+y+y*y }[;;"J"$first read0 `:input/13.txt] 4 | 5 | G:flip (2#g)#"#."0=mod[;2]sum each 2 vs'f .' til[g] cross til g:50 6 | 7 | target:39 31 8 | 9 | go:{[start] 10 | 11 | queue:enlist (start;0); 12 | visited:(); 13 | 14 | while[count queue; 15 | / pop first item from queue 16 | current:first queue; 17 | queue:1 _ queue; 18 | / break apart current item 19 | pos:first current; 20 | steps:last current; 21 | / nesw options 22 | o:pos+/:(-1 0;0 1;1 0;0 -1); 23 | / possibilities 24 | p:o where "."=.[G;] each o; 25 | / except done 26 | p:p where not p in queue[;0],visited[;0]; 27 | / add to queue 28 | queue,:(;steps + 1) each p; 29 | / add current to visited 30 | visited,:enlist current 31 | ]; 32 | :visited 33 | } 34 | 35 | res:go[1 1] 36 | first res[;1]where target~/:res[;0] 37 | /96 38 | sum res[;1]<51 39 | /141 40 | -------------------------------------------------------------------------------- /2016/14.py: -------------------------------------------------------------------------------- 1 | """--- Day 14: One-Time Pad ---""" 2 | 3 | import hashlib 4 | 5 | def generate_hashes(count, hash_count, salt, key): 6 | h = [] 7 | for i in range(key, key + count): 8 | m = salt + str(i) 9 | for _ in range(hash_count): 10 | m = hashlib.md5(m.encode('utf-8')).hexdigest() 11 | h.append(m) 12 | return h 13 | 14 | def check_batch(batch, char, count): 15 | for b in batch: 16 | if b.find(count*char) > -1: 17 | return True 18 | return False 19 | 20 | def solve(salt, key_length, hash_count): 21 | 22 | key = [] 23 | hashes = generate_hashes(1000, hash_count, salt, 0) 24 | i = 0 25 | 26 | while len(key) < key_length: 27 | h = hashes[i] 28 | j = 0 29 | while j < len(h) - 2: 30 | if h[j] == h[j+1] and h[j] == h[j+2]: 31 | if i+1001 > len(hashes): 32 | hashes += generate_hashes(1000, hash_count, salt, len(hashes)) 33 | if check_batch(hashes[i+1:i+1001], h[j], 5): 34 | key.append(i) 35 | break # only use first instance 36 | j += 1 37 | i += 1 38 | return key[-1] 39 | 40 | with open("input/14.txt", "r") as f: 41 | 42 | salt = f.read()[:-1] # drop trailing "\n" 43 | 44 | print(solve(salt, 64, 1)) 45 | print(solve(salt, 64, 1+2016)) 46 | -------------------------------------------------------------------------------- /2016/14.q: -------------------------------------------------------------------------------- 1 | /--- Day 14: One-Time Pad --- 2 | 3 | salt:first read0 `:input/14.txt 4 | 5 | f:{[func] 6 | ix:0; 7 | salts:(); 8 | s:(); 9 | 10 | while[64>count s; 11 | if[count[salts]<=ix+1000; 12 | salts,:func each salt,/:string count[salts] + til 1000 13 | ]; 14 | if[count raze m:ss[salts ix;] each 3#'distinct salts ix; 15 | if[any { any x ss y }[;5#salts[ix] min raze m] each salts ix + 1 + til 1000; 16 | s,:ix 17 | ] 18 | ]; 19 | ix+:1 20 | ]; 21 | last s 22 | } 23 | 24 | f[{raze string md5 x}] 25 | /16106 26 | f[{raze string md5 x}/[2017;]] 27 | /22423 28 | -------------------------------------------------------------------------------- /2016/15.py: -------------------------------------------------------------------------------- 1 | """--- Day 15: Timing is Everything ---""" 2 | 3 | def solve(disks): 4 | start_time = 0 5 | while True: 6 | time = start_time 7 | valid = True 8 | for disk in disks: 9 | time += 1 10 | if (disk[0] + time) % disk[1] != 0: 11 | valid = False 12 | break 13 | if valid: 14 | break 15 | start_time += 1 16 | return start_time 17 | 18 | with open("input/15.txt", "r") as instructions: 19 | disks = [] 20 | 21 | for instruction in instructions: 22 | _, _, _, positions, _, _, _, _, _, _, _, position = instruction.split() 23 | disks.append([int(position[:-1]), int(positions)]) 24 | 25 | print(solve(disks)) 26 | disks.append([0, 11]) 27 | print(solve(disks)) 28 | -------------------------------------------------------------------------------- /2016/15.q: -------------------------------------------------------------------------------- 1 | / --- Day 15: Timing is Everything --- 2 | 3 | i:" "vs'read0 `:input/15.txt 4 | 5 | d:(0;1+til count i)+flip "j"$"F"$i[;3 11] 6 | f:{ sum { (x+z) mod y }[x;;]. d } 7 | 8 | (1+)/[f;0] 9 | /121834 10 | 11 | / add disc #7 with 11 positions 12 | d:d,'11 7 13 | (1+)/[f;0] 14 | /3208099 15 | -------------------------------------------------------------------------------- /2016/16.py: -------------------------------------------------------------------------------- 1 | """--- Day 16: Dragon Checksum ---""" 2 | 3 | def dragon_tail(a, length): 4 | while len(a) < length: 5 | a = a + ["0"] + ["0" if a[i] == "1" else "1" for i in reversed(range(len(a)))] 6 | return a[:length] 7 | 8 | def checksum(a): 9 | while len(a) % 2 == 0: 10 | a = ["1" if a[i] == a[i+1] else "0" for i in range(0, len(a)-1, 2)] 11 | return a 12 | 13 | with open("input/16.txt") as f: 14 | text = list(f.read()[:-1]) 15 | print(''.join(checksum(dragon_tail(text, 272)))) 16 | print(''.join(checksum(dragon_tail(text, 35651584)))) 17 | -------------------------------------------------------------------------------- /2016/16.q: -------------------------------------------------------------------------------- 1 | /--- Day 16: Dragon Checksum --- 2 | 3 | l:272 4 | l2:35651584 5 | 6 | c:{ $[mod[count x;2];x;(=/)flip 2 cut x] } 7 | f:{ c/[y#{ x,0b,reverse not x }/[{y>count x}[;y];x]] } 8 | 9 | -1"01"f[;l] i:"1"=first read0 `:input/16.txt; 10 | /11111000111110000 11 | -1"01"f[i;l2]; 12 | /10111100110110100 13 | -------------------------------------------------------------------------------- /2016/17.q: -------------------------------------------------------------------------------- 1 | /--- Day 17: Two Steps Forward --- 2 | 3 | g:("#########"; 4 | "#S| | | #"; 5 | "#-#-#-#-#"; 6 | "# | | | #"; 7 | "#-#-#-#-#"; 8 | "# | | | #"; 9 | "#-#-#-#-#"; 10 | "# | | | "; 11 | "####### V") 12 | 13 | s:(first where any flip w;first where any w:"S"=g) 14 | e:-1+(first where any flip w;first where any w:"V"=g) 15 | 16 | c:"UDLR" 17 | d:(-1 0;1 0;0 -1;0 1) 18 | 19 | h:(any "bcdef"=\:raze string 2#md5@) 20 | 21 | p:first read0 `:input/17.txt 22 | 23 | r:{[x;y] 24 | D:c where any "|-"=\:.[g;] each d+\:x; 25 | L:c where h y; 26 | C:D inter L; 27 | 28 | $[x~e; 29 | enlist y; 30 | raze .z.s'[x+/:2*d c?C;y,/:C] 31 | ] 32 | }[s;p] 33 | 34 | (count p)_first r:r iasc count each r 35 | /DDRRUDLRRD 36 | count (count p)_last r 37 | /488 38 | -------------------------------------------------------------------------------- /2016/18.py: -------------------------------------------------------------------------------- 1 | """--- Day 18: Like a Rogue ---""" 2 | 3 | def solve(line, max_rows): 4 | safes = sum([1 for x in line if x == "."]) 5 | 6 | for _ in range(max_rows - 1): 7 | out = "^" if line[1] == "^" else "." 8 | for i in range(1, len(instruction) - 1): 9 | lcr = line[i-1:i+2] 10 | if lcr in ["^^.", ".^^", "^..", "..^"]: 11 | out += "^" 12 | else: 13 | out += "." 14 | out += "^" if line[-2] == "^" else "." 15 | safes += sum([1 for x in out if x == "."]) 16 | line = out 17 | 18 | return safes 19 | 20 | with open("input/18.txt", "r") as f: 21 | instruction = f.read()[:-1] 22 | print(solve(instruction, 40)) 23 | print(solve(instruction, 400000)) 24 | -------------------------------------------------------------------------------- /2016/18.q: -------------------------------------------------------------------------------- 1 | /--- Day 18: Like a Rogue --- 2 | 3 | sw3:{2_-3#'reverse prev\[1+count x;x,0b]} 4 | traps:`u#(110b;011b;100b;001b) 5 | f:{ sw3[x] in traps } 6 | 7 | sum not raze f\[40-1;] i:"^"=first read0 `:input/18.txt 8 | /1913 9 | sum not raze f\[400000-1;i] 10 | /19993564 11 | -------------------------------------------------------------------------------- /2016/19.py: -------------------------------------------------------------------------------- 1 | """--- Day 19: An Elephant Named Joseph ---""" 2 | 3 | def solve_1(elves): 4 | circle = list(map(lambda x: [1, 1+x], range(elves))) 5 | 6 | while len(circle) > 1: 7 | for i in range(0, len(circle)): 8 | p = circle[i] 9 | if p[0] > 0: 10 | # get second elf 11 | p2 = circle[(i+1) % len(circle)] 12 | # steal presents 13 | p2[0] = 0 14 | # only need to update once we've been around once 15 | circle = [e for e in circle if e[0] > 0] 16 | return circle[0][1] 17 | 18 | def solve_2(elves): 19 | circle = list(map(lambda x: 1+x, range(elves))) 20 | # start at first elf 21 | i = 0 22 | while len(circle) > 1: 23 | # find elf opposite 24 | opposite = (i + int(len(circle) / 2)) % len(circle) 25 | # remove that elf 26 | circle.pop(opposite) 27 | # determine next elf in circle 28 | if i == len(circle): 29 | i = 0 # last elf, next is first 30 | elif i < opposite: 31 | i += 1 # we removed elf ahead of us 32 | else: 33 | continue # we removed an elf before us 34 | return circle[0] 35 | 36 | with open("input/19.txt", "r") as f: 37 | text = int(f.readline()) 38 | print(solve_1(text)) 39 | print(solve_2(text)) 40 | -------------------------------------------------------------------------------- /2016/20.py: -------------------------------------------------------------------------------- 1 | """--- Day 20: Firewall Rules ---""" 2 | 3 | with open("input/20.txt", "r") as f: 4 | instructions = f.read().split("\n")[:-1] 5 | 6 | mn = 0 7 | mx = 4294967295 8 | mxo = 0 9 | gaps = 0 10 | 11 | # sort instructions 12 | instructions.sort(key=lambda x: int(x.split("-")[0])) 13 | 14 | for instruction in instructions: 15 | low, high = [int(x) for x in instruction.split("-")] 16 | # part 1 17 | if low <= mn < high: 18 | mn = high + 1 19 | # part 2 20 | if low > mxo: 21 | gaps += low - mxo 22 | if high >= mxo: 23 | mxo = high + 1 24 | 25 | print(mn) 26 | print(gaps + (mx - mxo) + 1) 27 | -------------------------------------------------------------------------------- /2016/21.q: -------------------------------------------------------------------------------- 1 | /--- Day 21: Scrambled Letters and Hash --- 2 | 3 | swpp:{ x[z,y]:x y,z;x } 4 | swpl:{ wy:where x=y; wz:where x=z; x[wy,wz]:(count[wy]#z),count[wz]#y;x } 5 | rotc:{ ($[y~"left";;neg]@z) rotate x } 6 | rotp:{ i:first where x=y; rotc[x;"right";1 + i + $[i>3;1;0]] } 7 | rvrs:{ x[reverse i]:x i:y + til 1 + z - y;x } 8 | 9 | move:{ 10 | / length of x 11 | c:count x; 12 | / temp var 13 | t:x y; 14 | / remove 15 | x:x _ y; 16 | $[z=0; 17 | t,x; 18 | z=c; 19 | x,t; 20 | (z#x),t,neg[c-z+1]#x 21 | ] 22 | }; 23 | 24 | r:" "vs'read0 `:input/21.txt 25 | 26 | f:{ 27 | if["swap"~y 0; 28 | :$["position"~y 1; 29 | swpp[x;] . "I"$ y 2 5; 30 | swpl[x;] . raze y 2 5] 31 | ]; 32 | if["rotate"~y 0; 33 | :$["based"~y 1; 34 | rotp[x;] first y 6; 35 | rotc[x;y 1;] "I"$ y 2] 36 | ]; 37 | if["reverse"~y 0; 38 | :rvrs[x;] . "I"$ y 2 4 39 | ]; 40 | if["move"~y 0; 41 | :move[x;] . "I"$ y 2 5 42 | ]; 43 | } 44 | 45 | f/["abcdefgh";r] 46 | /aefgbcdh 47 | 48 | g:{ 49 | if["swap"~y 0; 50 | :$["position"~y 1; 51 | swpp[x;] . "I"$ y 5 2; 52 | swpl[x;] . raze y 5 2] 53 | ]; 54 | if["rotate"~y 0; 55 | :$["based"~y 1; 56 | rotc[x;"left";] 1 1 6 2 7 3 0 4 first where x=first y 6; 57 | rotc[x;("left";"right")@"left"~y 1;] "I"$ y 2] 58 | ]; 59 | if["reverse"~y 0; 60 | :rvrs[x;] . "I"$ y 2 4 61 | ]; 62 | if["move"~y 0; 63 | :move[x;] . "I"$ y 5 2 64 | ]; 65 | } 66 | 67 | g/["fbgdceah";reverse r] 68 | /egcdahbf 69 | -------------------------------------------------------------------------------- /2016/22.py: -------------------------------------------------------------------------------- 1 | """--- Day 22: Grid Computing ---""" 2 | 3 | with open("input/22.txt") as f: 4 | 5 | nodes = [] 6 | instructions = f.read() 7 | 8 | for instruction in instructions.split("\n")[2:-1]: 9 | path, size, used, avail, pct = instruction.split() 10 | 11 | _, x, y = path.split("-") 12 | nodes.append({ 13 | "x": x[1:], 14 | "y": y[1:], 15 | "used": int(used[:-1]), 16 | "avail": int(avail[:-1]) 17 | }) 18 | 19 | pairs = 0 20 | for i, _ in enumerate(nodes): 21 | nodeA = nodes[i] 22 | if nodeA["used"] > 0: 23 | for j, _ in enumerate(nodes): 24 | nodeB = nodes[j] 25 | if nodeB["avail"] >= nodeA["used"]: 26 | if nodeA["x"] != nodeB["x"] or nodeA["y"] != nodeB["y"]: 27 | pairs += 1 28 | 29 | print(pairs) 30 | -------------------------------------------------------------------------------- /2016/24.q: -------------------------------------------------------------------------------- 1 | / --- Day 24: Air Duct Spelunking --- 2 | 3 | g:read0 `:input/24.txt; 4 | 5 | bfs:{[S] 6 | // initialise queue as starting node 7 | Q::enlist S; 8 | // initialise visited dictionary 9 | V::(`u#enlist S)!enlist 0; 10 | // iterate until queue is empty 11 | { 12 | // remove first item from queue 13 | v:{ q:first Q;Q::1_Q;q }[]; 14 | // pick out valid neighours 15 | n:enlist each n where {not x in key V} each n:n where not "#"=.[g;] each n:v+/:(-1 0;1 0;0 -1;0 1); 16 | // add neighbours to Q and Visited 17 | { Q,:x; V[x]:y }[;1+V v] each n; 18 | }/[{count Q};(::)] 19 | }; 20 | 21 | route:{ exec sum d from r -2#'prev\[count[x]-2;x] }; 22 | 23 | find:{ (first where any flip w;first where any w:x=y) }; 24 | 25 | res:raze { 26 | // perform bfs starting at x 27 | bfs find[g;] x; 28 | // return levels (ie distance to each point) 29 | V find[g;] each p 30 | } each p:asc (distinct raze g) except "#."; 31 | 32 | // code stolen from 2015.09 33 | r:2!delete from (flip `f`t`d!(`$string raze count[p]#enlist til count p;`$string raze count[p]#'p;res)) where d=0; 34 | // generate all route combinations that begin with 0 35 | c:`0,/:{raze y,/:'x except/:y}[ix;]/[count[ix]-1;] ix:exec distinct f from r where not f=`0; 36 | 37 | min route each c 38 | /448 39 | min route each c,\:`0 40 | /672 41 | -------------------------------------------------------------------------------- /2016/input/01.txt: -------------------------------------------------------------------------------- 1 | L2, L5, L5, R5, L2, L4, R1, R1, L4, R2, R1, L1, L4, R1, L4, L4, R5, R3, R1, L1, R1, L5, L1, R5, L4, R2, L5, L3, L3, R3, L3, R4, R4, L2, L5, R1, R2, L2, L1, R3, R4, L193, R3, L5, R45, L1, R4, R79, L5, L5, R5, R1, L4, R3, R3, L4, R185, L5, L3, L1, R5, L2, R1, R3, R2, L3, L4, L2, R2, L3, L2, L2, L3, L5, R3, R4, L5, R1, R2, L2, R4, R3, L4, L3, L1, R3, R2, R1, R1, L3, R4, L5, R2, R1, R3, L3, L2, L2, R2, R1, R2, R3, L3, L3, R4, L4, R4, R4, R4, L3, L1, L2, R5, R2, R2, R2, L4, L3, L4, R4, L5, L4, R2, L4, L4, R4, R1, R5, L2, L4, L5, L3, L2, L4, L4, R3, L3, L4, R1, L2, R3, L2, R1, R2, R5, L4, L2, L1, L3, R2, R3, L2, L1, L5, L2, L1, R4 2 | -------------------------------------------------------------------------------- /2016/input/05.txt: -------------------------------------------------------------------------------- 1 | ffykfhsq 2 | -------------------------------------------------------------------------------- /2016/input/12.txt: -------------------------------------------------------------------------------- 1 | cpy 1 a 2 | cpy 1 b 3 | cpy 26 d 4 | jnz c 2 5 | jnz 1 5 6 | cpy 7 c 7 | inc d 8 | dec c 9 | jnz c -2 10 | cpy a c 11 | inc a 12 | dec b 13 | jnz b -2 14 | cpy c b 15 | dec d 16 | jnz d -6 17 | cpy 19 c 18 | cpy 14 d 19 | inc a 20 | dec d 21 | jnz d -2 22 | dec c 23 | jnz c -5 24 | -------------------------------------------------------------------------------- /2016/input/13.txt: -------------------------------------------------------------------------------- 1 | 1358 2 | -------------------------------------------------------------------------------- /2016/input/14.txt: -------------------------------------------------------------------------------- 1 | zpqevtbw 2 | -------------------------------------------------------------------------------- /2016/input/15.txt: -------------------------------------------------------------------------------- 1 | Disc #1 has 7 positions; at time=0, it is at position 0. 2 | Disc #2 has 13 positions; at time=0, it is at position 0. 3 | Disc #3 has 3 positions; at time=0, it is at position 2. 4 | Disc #4 has 5 positions; at time=0, it is at position 2. 5 | Disc #5 has 17 positions; at time=0, it is at position 0. 6 | Disc #6 has 19 positions; at time=0, it is at position 7. 7 | -------------------------------------------------------------------------------- /2016/input/16.txt: -------------------------------------------------------------------------------- 1 | 01111001100111011 2 | -------------------------------------------------------------------------------- /2016/input/17.txt: -------------------------------------------------------------------------------- 1 | pslxynzg -------------------------------------------------------------------------------- /2016/input/18.txt: -------------------------------------------------------------------------------- 1 | ^.^^^..^^...^.^..^^^^^.....^...^^^..^^^^.^^.^^^^^^^^.^^.^^^^...^^...^^^^.^.^..^^..^..^.^^.^.^....... 2 | -------------------------------------------------------------------------------- /2016/input/19.txt: -------------------------------------------------------------------------------- 1 | 3005290 2 | -------------------------------------------------------------------------------- /2016/input/23.txt: -------------------------------------------------------------------------------- 1 | cpy a b 2 | dec b 3 | cpy a d 4 | cpy 0 a 5 | cpy b c 6 | inc a 7 | dec c 8 | jnz c -2 9 | dec d 10 | jnz d -5 11 | dec b 12 | cpy b c 13 | cpy c d 14 | dec d 15 | inc c 16 | jnz d -2 17 | tgl c 18 | cpy -16 c 19 | jnz 1 c 20 | cpy 98 c 21 | jnz 86 d 22 | inc a 23 | inc d 24 | jnz d -2 25 | inc c 26 | jnz c -5 27 | -------------------------------------------------------------------------------- /2016/input/25.txt: -------------------------------------------------------------------------------- 1 | cpy a d 2 | cpy 7 c 3 | cpy 365 b 4 | inc d 5 | dec b 6 | jnz b -2 7 | dec c 8 | jnz c -5 9 | cpy d a 10 | jnz 0 0 11 | cpy a b 12 | cpy 0 a 13 | cpy 2 c 14 | jnz b 2 15 | jnz 1 6 16 | dec b 17 | dec c 18 | jnz c -4 19 | inc a 20 | jnz 1 -7 21 | cpy 2 b 22 | jnz c 2 23 | jnz 1 4 24 | dec b 25 | dec c 26 | jnz 1 -4 27 | jnz 0 0 28 | out b 29 | jnz a -19 30 | jnz 1 -21 31 | -------------------------------------------------------------------------------- /2017/01.py: -------------------------------------------------------------------------------- 1 | """--- Day 1: Inverse Captcha ---""" 2 | 3 | with open("input/01.txt", "r") as f: 4 | data = f.readline()[:-1] # drop newline 5 | print(sum([int(x) for x, y in zip(data, data[1:]+data[0]) if x == y])) 6 | #1047 7 | print(2*sum([int(x) for x, y in zip(data[:int(.5*len(data))], data[int(.5*len(data)):]) if x == y])) 8 | #892 9 | -------------------------------------------------------------------------------- /2017/01.q: -------------------------------------------------------------------------------- 1 | /--- Day 1: Inverse Captcha --- 2 | 3 | sum f where f=1 rotate f:-48+"j"$first read0 `:input/01.txt 4 | /1047 5 | 2*sum f where (=) . 2 0N#f 6 | /982 7 | -------------------------------------------------------------------------------- /2017/02.py: -------------------------------------------------------------------------------- 1 | """--- Day 2: Corruption Checksum ---""" 2 | 3 | import itertools 4 | 5 | with open("input/02.txt", "r") as f: 6 | data = f.read()[:-1] # drop newline 7 | rows = data.split("\n") 8 | print(sum([max(x)-min(x) for x in [list(map(int, x)) for x in map(str.split, rows)]])) 9 | #45351 10 | print(sum(map(lambda x: [a//b for a, b in x if a%b == 0][0], 11 | [itertools.permutations(x, 2) for x in [map(int, x.split()) for x in rows]]))) 12 | #275 13 | -------------------------------------------------------------------------------- /2017/02.q: -------------------------------------------------------------------------------- 1 | /--- Day 2: Corruption Checksum --- 2 | 3 | sum { (max x) - min x } each r:"J"$ "\t" vs'read0 `:input/02.txt 4 | /45351 5 | sum { (%) . x (where 2=sum a),where 2=sum flip a:0=x mod/:x } each r 6 | /275 -------------------------------------------------------------------------------- /2017/03.py: -------------------------------------------------------------------------------- 1 | """--- Day 3: Spiral Memory (Part 2)---""" 2 | 3 | def g(x, y): 4 | return visited[(x, y)] if (x, y) in visited.keys() else 0 5 | 6 | def u(): 7 | return g(x, y+1) 8 | 9 | def d(): 10 | return g(x, y-1) 11 | 12 | def l(): 13 | return g(x-1, y) 14 | 15 | def r(): 16 | return g(x+1, y) 17 | 18 | def n(): 19 | return (g(x-1, y+1) + g(x, y+1) + g(x+1, y+1) + 20 | g(x-1, y ) + g(x+1, y ) + 21 | g(x-1, y-1) + g(x, y-1) + g(x+1, y-1)) 22 | 23 | 24 | visited = {} 25 | # bootstrap 26 | visited[(0, 0)] = 1 27 | x = 1 28 | y = 0 29 | 30 | i = 0 31 | while i < 368078: 32 | i = n() # calculate value for current cell 33 | visited[(x,y)] = i # update current position 34 | 35 | p = [1*(a > 0) for a in (u(), d(), l(), r())] # next move 36 | 37 | if p in ([1, 0, 1, 0], [1, 0, 0, 0]): # right 38 | x += 1 39 | elif p in ([0, 0, 1, 0], [0, 1, 1, 0]): # up 40 | y += 1 41 | elif p in ([0, 1, 0, 0], [0, 1, 0, 1]): # left 42 | x -= 1 43 | elif p in ([0, 0, 0, 1], [1, 0, 0, 1]): # down 44 | y -= 1 45 | 46 | print(i) 47 | #369601 48 | -------------------------------------------------------------------------------- /2017/03.q: -------------------------------------------------------------------------------- 1 | /--- Day 3: Spiral Memory --- 2 | 3 | /OEIS A016754 4 | A016754:{ x*(x:1+2*x) } 5 | { ring:{ { x+1 }/[{[MAX;x] MAX > A016754 x}[x];0] } x; 6 | ring + abs ring - (x - A016754 ring - 1) mod floor 0.25*(A016754 ring)-A016754 ring - 1 } 368078 7 | /371 8 | 9 | g:(enlist 0 0)!enlist 1 / bootstrap 10 | l:1 0 / move east 11 | 12 | while[368078>g[l]:sum (n:g l +/:(0 1;1 0;0 -1;-1 0)),g l+/:(1 1;1 -1;-1 -1;-1 1); 13 | $[(d:"b"$0^n) in (0001b;0011b); 14 | l+:0 1; / move north 15 | d in (0010b;0110b); 16 | l+:-1 0; / move west 17 | d in (0100b;1100b); 18 | l+:0 -1; / move south 19 | l+:1 0 / move east 20 | ] 21 | ]; 22 | g l 23 | /369601 24 | -------------------------------------------------------------------------------- /2017/04.py: -------------------------------------------------------------------------------- 1 | """--- Day 4: High-Entropy Passphrases ---""" 2 | 3 | with open("input/04.txt", "r") as f: 4 | data = f.read()[:-1] # drop newline 5 | rows = data.split("\n") 6 | print(sum([len(set(x)) == len(x) for x in [row.split() for row in rows]])) 7 | #386 8 | print(sum(map(lambda x: len(set(x))==len(x), map(lambda x: ["".join(sorted(y)) for y in x], map(str.split, rows))))) 9 | #208 10 | -------------------------------------------------------------------------------- /2017/04.q: -------------------------------------------------------------------------------- 1 | /--- Day 4: High-Entropy Passphrases --- 2 | 3 | sum { (count distinct x)=count x } each r:" "vs'read0 `:input/04.txt 4 | /386 5 | 6 | sum { (count x)=count distinct asc each x } each r 7 | /208 -------------------------------------------------------------------------------- /2017/05.py: -------------------------------------------------------------------------------- 1 | """--- Day 5: A Maze of Twisty Trampolines, All Alike ---""" 2 | 3 | with open("input/05.txt") as f: 4 | data = f.read()[:-1] # drop newline 5 | inst = list(map(int, data.split("\n"))) 6 | ptr = cnt = 0 7 | while ptr < len(inst): 8 | o = inst[ptr] 9 | inst[ptr] += 1 10 | ptr += o 11 | cnt += 1 12 | print(cnt) 13 | #343467 14 | inst = list(map(int, data.split("\n"))) 15 | ptr = cnt = 0 16 | while ptr < len(inst): 17 | o = inst[ptr] 18 | inst[ptr] += -1 if o >= 3 else 1 19 | ptr += o 20 | cnt += 1 21 | print(cnt) 22 | #24774780 23 | -------------------------------------------------------------------------------- /2017/05.q: -------------------------------------------------------------------------------- 1 | / --- Day 5: A Maze of Twisty Trampolines, All Alike --- 2 | 3 | ll:l:"J"$read0 `:input/05.txt 4 | p:c:0; / pointer, counter 5 | 6 | while[not null j:l[p];l[p]+:1;c+:1;p+:j];c 7 | /343467 8 | 9 | l:ll / reset list 10 | p:c:0; / reset pointer, counter 11 | 12 | while[not null j:l[p];l[p]+:$[j>2;-1;1];c+:1;p+:j];c 13 | /24774780 14 | -------------------------------------------------------------------------------- /2017/06.q: -------------------------------------------------------------------------------- 1 | /--- Day 6: Memory Reallocation --- 2 | 3 | / redistribution function 4 | f:{[x] 5 | w:first where x=m:max x; 6 | x[w]:0; 7 | c:count each group mod[;count x] w + 1 + til m; 8 | @[x;key c;+;value c] 9 | } 10 | 11 | s:`u#enlist r:"J"$ "\t" vs first read0 `:input/06.txt 12 | 13 | while[not (r:f r) in s;s,:r] 14 | count s 15 | /6681 16 | 17 | (count s) - first where r~/:s 18 | /2392 19 | -------------------------------------------------------------------------------- /2017/07.q: -------------------------------------------------------------------------------- 1 | / --- Day 7: Recursive Circus --- 2 | 3 | t:`n xkey flip `n`w`p!"sjs"$\:(); / node, weight, total, parent 4 | 5 | { 6 | x:" " vs x except ",()"; 7 | n:`$x 0; / node 8 | w:"J"$x 1; / weight 9 | `t upsert (n;w;t[n;`p]); / add node 10 | `t upsert {[p;n] (n;t[n;`w];p) }[n;] each `$3_x / update parent 11 | } each read0 `:input/07.txt; 12 | 13 | / find node without a parent 14 | exec first n from t where null p 15 | /`hlqnsbe 16 | 17 | / add column for total weight 18 | update t:0 from `t; 19 | 20 | / calculate total weights 21 | { 22 | $[count c:exec n from t where p=x; 23 | t[x;`t]:t[x;`w]+sum .z.s each c; 24 | t[x;`w] 25 | ] 26 | }`; 27 | 28 | /delete null node added by recursion 29 | s:delete from t where null n 30 | / delete leaves 31 | s:delete from s where t = 0 32 | / add ws column, number of distinct weights 33 | s:select n, w, t, ws:count distinct t by p from s 34 | / remove entries with correct weights 35 | s:delete from s where ws = 1 36 | / remove parents of the bad node 37 | s:select from s where not any each n in\:exec p from s 38 | / locate (badnode;weight_difference) 39 | res:exec {(first x where y=z;(first y where y<>z)-z)}[n;t;first where 1=count each group t] from ungroup s 40 | / calculate correct weight 41 | (last res)+exec first w from t where n=first res 42 | /1993 43 | -------------------------------------------------------------------------------- /2017/08.q: -------------------------------------------------------------------------------- 1 | / --- Day 8: I Heard You Like Registers --- 2 | 3 | i:flip R:("*** ***";" ") 0: read0 `:input/08.txt; 4 | 5 | r:set[;0] each `$".r.",/:distinct R 0; / initialise registers to zero 6 | i[;1]:("+:";"-:")"dec"~/:R 1; / replace "inc/dec" with "+:/-:" 7 | 8 | res:{ 9 | value raze "if[.r.",x[3],$["=="~e:x 4;"=";"!="~e;"<>";e],x[5],";.r.",x[0 1 2],"]"; 10 | max value each r 11 | } each i; 12 | 13 | last res 14 | /4902 15 | max res 16 | /7037 -------------------------------------------------------------------------------- /2017/09.q: -------------------------------------------------------------------------------- 1 | /--- Day 9: Stream Processing --- 2 | 3 | rg:{ 4 | $[first x; / inside garbage? 5 | $[y=">"; / > is the end-of-garbage marker 6 | (0b;""); / no longer inside garbage, but still chomp '>' char 7 | (1b;"") / still inside garbage therefore chomp character 8 | ]; 9 | $[y="<"; / < is the beginning-of-garbage marker 10 | (1b;""); / we are in garbage, so chomp '<' char 11 | (0b;y) / we are NOT in garbage, therefore return character 12 | ] 13 | ] 14 | }\[0b;] / initialise outside of garbage 15 | 16 | cg:{ 17 | $[y="{"; / { is the start of a new group 18 | x+1; / therefore increment group level counter 19 | y="}"; / } is the end of a new group 20 | x-1; / threfore decrement group level counter 21 | x / otherwise do nothing to the counter 22 | ] 23 | }\[0;] / initialise at zero 24 | 25 | sum {x where x > prev x} cg raze last f:flip rg ssr[;"!?";""] first read0 `:input/09.txt 26 | /12505 27 | sum 1=deltas where first f 28 | /6671 29 | -------------------------------------------------------------------------------- /2017/10.q: -------------------------------------------------------------------------------- 1 | / --- Day 10: Knot Hash --- 2 | 3 | knot:{ 4 | first { 5 | p:x 1; 6 | s:x 2; 7 | w:(p+til y) mod count l:x 0; 8 | (@[l;w;:;reverse l w];p+s+y;s+1) 9 | }/[(x;0;0);y] 10 | }; 11 | 12 | xor:{0b sv(<>/)0b vs'(x;y)}; 13 | 14 | hash:{"x"$(xor over) each 16 cut knot[til 256;raze 64#enlist ("j"$x),17 31 73 47 23]}; 15 | 16 | if[`10.q~.z.f; 17 | 0N!prd 2#knot[til 256;"J"$","vs i:first read0 `:input/10.txt]; 18 | /8536 19 | 0N!raze string hash i 20 | /aff593797989d665349efe11bb4fd99b 21 | ]; 22 | -------------------------------------------------------------------------------- /2017/11.q: -------------------------------------------------------------------------------- 1 | /--- Day 11: Hex Ed --- 2 | 3 | / inspired by /u/gyorokpeter @ https://www.reddit.com/r/adventofcode/comments/7izym2/2017_day_11_solutions/dr2uady/ 4 | 5 | max abs last r:sums(`n`s`ne`nw`se`sw!(0 1 -1;0 -1 1;1 0 -1;-1 1 0;1 -1 0;-1 0 1))`$","vs first read0 `:input/11.txt 6 | /761 7 | max max abs flip r 8 | /1542 9 | -------------------------------------------------------------------------------- /2017/12.q: -------------------------------------------------------------------------------- 1 | /--- Day 12: Digital Plumber --- 2 | 3 | p:{ (`$x[;0])!`$", "vs/:x[;1] }" <-> "vs/:read0 `:input/12.txt; 4 | r:{ distinct x,raze p x } 5 | 6 | count r over `0 7 | /134 8 | -1+count { x except r over first x } scan key p 9 | /193 10 | -------------------------------------------------------------------------------- /2017/13.q: -------------------------------------------------------------------------------- 1 | /--- Day 13: Packet Scanners --- 2 | 3 | i:flip "J"$": "vs/:read0 `:input/13.txt 4 | f:@[(1+last l)#0N;l:first i;:;last i] 5 | sum w*f w:where 0=(til count g) mod g:{2*x-1}f 6 | /1476 7 | (2+)/[{max 0=(x+til count g) mod g};0] / ~4s 8 | /3937334 9 | -------------------------------------------------------------------------------- /2017/14.q: -------------------------------------------------------------------------------- 1 | /--- Day 14: Disk Defragmentation --- 2 | 3 | \l 10.q 4 | 5 | sum raze h:raze each 0b vs''hash peach (first read0 `:input/14.txt),/:"-",'string til 128 6 | /8214 7 | 8 | v:(`u#enlist 0N 0N)!enlist 0N; / visited, add dummy value to setup key 9 | 10 | f:{[x;id] 11 | if[not h . x; / ignore 'off' bits 12 | :() 13 | ]; 14 | if[x in key v; / ignore already visited 15 | :() 16 | ]; 17 | v[x]:id; / set region for this bit 18 | .z.s[x+1 0;id]; / go right 19 | .z.s[x-1 0;id]; / go left 20 | .z.s[x+0 1;id]; / go up 21 | .z.s[x-0 1;id]; / go down 22 | }; 23 | 24 | f'[til[128] cross til 128;til 128*128]; 25 | -1+count distinct value v 26 | /1093 27 | -------------------------------------------------------------------------------- /2017/15.q: -------------------------------------------------------------------------------- 1 | /--- Day 15: Dueling Generators --- 2 | 3 | g:{ (first[x]+(~/)mod[r;65536];r:(16807 48271*last x) mod 2147483647) }/[40000000;] 4 | first g (0;) i:first (" j";" ") 0: `:input/15.txt 5 | /650 6 | 7 | A:B:() 8 | 9 | { if[0=mod[x:mod[x*16807;2147483647];4];A,:x];x }/[{ 5000000>count A };first i]; 10 | { if[0=mod[x:mod[x*48271;2147483647];8];B,:x];x }/[{ 5000000>count B };last i]; 11 | 12 | sum mod[A;65536]=mod[B;65536] 13 | /336 14 | -------------------------------------------------------------------------------- /2017/16.q: -------------------------------------------------------------------------------- 1 | /--- Day 16: Permutation Promenade --- 2 | 3 | i:{ 4 | $["s"=x 0; 5 | rotate[neg["J"$1_x];]; 6 | "x"=x 0; 7 | {@[x;y;:;reverse x y]}[;"J"$"/"vs 1_x]; 8 | {@[x;x?y;:;reverse y]}[;"/"vs 1_x] 9 | ] 10 | } peach "," vs first read0 `:input/16.txt 11 | 12 | {y x}/[16#.Q.a;i] 13 | /ceijbfoamgkdnlph 14 | 15 | r 1000000000 mod count r:{ {y x}/[y;x] }[i]\[16#.Q.a] 16 | /pnhajoekigcbflmd 17 | -------------------------------------------------------------------------------- /2017/17.q: -------------------------------------------------------------------------------- 1 | /--- Day 17: Spinlock --- 2 | 3 | first {(x rotate (),y),z}[s:"J"$first read0 `:input/17.txt]/[til 2018] 4 | /596 5 | 6 | last where 1={ 1+(x+y) mod z }[s]\[til 50000000] 7 | /39051595 8 | -------------------------------------------------------------------------------- /2017/18.q: -------------------------------------------------------------------------------- 1 | /--- Day 18: Duet --- 2 | 3 | i:" "vs'read0`:input/18.txt / instructions 4 | i[;0 1]:`$i[;0 1]; / cast to symbol 5 | 6 | tr:{[x;y] $[all y in .Q.a;r[x;`$y];value y] }; / try register 7 | 8 | On:enlist[`]!enlist[(::)]; 9 | 10 | On.snd:{[p;x] snd::r[p;x];1 } 11 | On.rcv:{[p;x] $[0=r[p;x];1;0] } 12 | On.set:{[p;x;y] r[p;x]:tr[p;y]; 1 } 13 | On.add:{[p;x;y] r[p;x]+:tr[p;y]; 1 } 14 | On.mul:{[p;x;y] r[p;x]*:tr[p;y]; 1 } 15 | On.mod:{[p;x;y] r[p;x]:r[p;x] mod tr[p;y]; 1 } 16 | On.jgz:{[p;x;y] $[0= " } each read0 `:input/20.txt; 4 | 5 | exec first id from `a xasc update id:i, sum each abs each a from t 6 | /376 7 | 8 | count {select from (update p:p+'v from update v:v+'a from x) where 1=(count;i) fby p}/[50;t] 9 | / 574 10 | -------------------------------------------------------------------------------- /2017/21.q: -------------------------------------------------------------------------------- 1 | /--- Day 21: Fractal Art --- 2 | 3 | b:010001111b / book 4 | r:()!() / rules 5 | { { r[enlist raze x]:enlist raze y }[;"#"="/"vs y] each (reverse reverse each f;reverse each f;reverse f;f:flip x; 6 | reverse reverse each x;reverse each x;reverse x;x:"#"="/"vs x) 7 | } .' " => " vs/:read0 `:input/21.txt; 8 | 9 | f:{ $[9=count x;r[x] 0 2 8 10+\:0 1 4 5;r x] }; 10 | g:{ raze f each (raze (raze f each f x) 0 3 6 18 21 24+\:0 1 2 9 10 11) 0 12 24+\:0 2 4+\:0 1 6 7 }; 11 | 12 | sum { raze f each x }/[2;g b] 13 | /117 14 | 15 | sum raze { raze g each x }/[5;] g b 16 | /2026963i 17 | -------------------------------------------------------------------------------- /2017/22.q: -------------------------------------------------------------------------------- 1 | /--- Day 22: Sporifica Virus --- 2 | 3 | d:"U" / initial direction 4 | l:12 12 / starting location 5 | i:0 / infected count 6 | g:(`u#enlist 0N 0N)!enlist " " / initialise grid to null 7 | 8 | (til count r){ {g[(x;y)]:z}'[x;til count y;y] }'r:read0 `:input/22.txt; 9 | 10 | On:()!(); 11 | On["#"]: { 12 | d::"RLUD""UDLR"?d; / turn right 13 | g[l]:"." / clean 14 | }; 15 | On[" ."]:{ 16 | d::"LRDU""UDLR"?d; / turn left 17 | g[l]:"#"; / infect 18 | i+:1 / infected++ 19 | }; 20 | 21 | move:{ l+:(1 0;-1 0;0 -1; 0 1)"DULR"?d }; 22 | burst:{ On[g[l]][]; move[] }; 23 | 24 | do[10000;burst[]]; 25 | i 26 | /5433 27 | 28 | d:"U" / re-initialise direction 29 | l:12 12; / re-initialise starting location 30 | i:0 / re-initialise infected count 31 | g:(`u#enlist 0N 0N)!enlist " " / re-initialise grid to null 32 | 33 | (til count r){ {g[(x;y)]:z}'[x;til count y;y] }'r; / re-build grid 34 | 35 | On["#"]: { 36 | g[l]:"F"; / flag 37 | d::"RLUD""UDLR"?d / turn right 38 | }; 39 | On["F"]: { 40 | g[l]:" "; / clean 41 | d::"DURL""UDLR"?d / turn 180 42 | }; 43 | On["W"]:{ 44 | g[l]:"#"; / infect 45 | i+:1 / infected++ 46 | }; 47 | On[" ."]:{ 48 | g[l]:"W"; / weaken 49 | d::"LRDU""UDLR"?d / turn left 50 | }; 51 | 52 | do[10000000;burst[]]; 53 | i 54 | /2512599 -------------------------------------------------------------------------------- /2017/24.q: -------------------------------------------------------------------------------- 1 | /--- Day 24: Electromagnetic Moat --- 2 | 3 | f:{[bridge;options] 4 | w1:where options[;0]=lc:last bridge; / options that match 5 | w2:except[;w1] where options[;1]=lc; / options that match in reverse 6 | if[0=count w1,w2;:enlist bridge]; / return if no further options 7 | 8 | raze .z.s'[bridge,/:(options w1),reverse each options w2;options _/:w1,w2] 9 | } 10 | 11 | max sum each res:f[0;] "J"$"/"vs'read0 `:input/24.txt 12 | /1940 13 | max sum each res where c=max c:count each res 14 | /1928 15 | -------------------------------------------------------------------------------- /2017/25.q: -------------------------------------------------------------------------------- 1 | /--- Day 25: The Halting Problem --- 2 | 3 | s:`A 4 | p:0 5 | t:(`u#enlist 0N)!(enlist 0N) 6 | On:(`u#enlist`)!enlist[(::)] 7 | 8 | On.A:{ $[null t p;[t[p]:1;p+:1;s::`B];[t[p]:0N;p-:1;s::`D]] }; 9 | On.B:{ $[null t p;[t[p]:1;p+:1;s::`C];[t[p]:0N;p+:1;s::`F]] }; 10 | On.C:{ $[null t p;[t[p]:1;p-:1]; [p-:1;s::`A]] }; 11 | On.D:{ $[null t p; [p-:1;s::`E]; [p+:1;s::`A]] }; 12 | On.E:{ $[null t p;[t[p]:1;p-:1;s::`A];[t[p]:0N;p+:1;s::`B]] }; 13 | On.F:{ $[null t p;[p+:1; s::`C];[t[p]:0N;p+:1;s::`E]] }; 14 | 15 | do[12302209;On[s][]]; 16 | sum t 17 | /633 -------------------------------------------------------------------------------- /2017/input/02.txt: -------------------------------------------------------------------------------- 1 | 3093 749 3469 142 2049 3537 1596 3035 2424 3982 3290 125 249 131 118 3138 2 | 141 677 2705 2404 2887 2860 1123 2714 117 1157 2607 1800 153 130 1794 3272 3 | 182 93 2180 114 103 1017 95 580 2179 2470 2487 2806 1574 1325 1898 1706 4 | 3753 233 3961 3747 3479 3597 1303 2612 4043 1815 3318 737 197 3943 239 254 5 | 113 147 961 157 3514 3045 1270 3528 1369 3377 492 156 1410 3251 1839 1249 6 | 3948 3651 888 3631 253 220 4266 1284 3595 237 2138 3799 2319 254 267 1182 7 | 399 446 795 653 154 762 140 487 750 457 730 150 175 841 323 492 8 | 999 979 103 99 1544 1404 100 1615 840 92 1552 1665 1686 76 113 1700 9 | 4049 182 3583 1712 200 3326 3944 715 213 1855 2990 3621 2560 842 249 2082 10 | 2610 4749 2723 2915 2189 3911 124 164 1895 3095 3992 134 127 4229 3453 4428 11 | 105 692 101 150 193 755 84 185 622 851 706 251 86 408 774 831 12 | 238 217 224 1409 1850 2604 363 265 596 2933 2641 2277 803 2557 1399 237 13 | 304 247 192 4369 997 5750 85 1248 4718 3888 5228 5116 5880 5348 6052 245 14 | 238 373 228 395 86 59 289 87 437 384 233 79 470 403 441 352 15 | 151 3473 1435 87 1517 1480 140 2353 1293 118 163 3321 2537 3061 1532 3402 16 | 127 375 330 257 220 295 145 335 304 165 151 141 289 256 195 272 17 | -------------------------------------------------------------------------------- /2017/input/06.txt: -------------------------------------------------------------------------------- 1 | 4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3 2 | -------------------------------------------------------------------------------- /2017/input/10.txt: -------------------------------------------------------------------------------- 1 | 97,167,54,178,2,11,209,174,119,248,254,0,255,1,64,190 2 | -------------------------------------------------------------------------------- /2017/input/13.txt: -------------------------------------------------------------------------------- 1 | 0: 3 2 | 1: 2 3 | 2: 4 4 | 4: 6 5 | 6: 5 6 | 8: 6 7 | 10: 6 8 | 12: 4 9 | 14: 8 10 | 16: 8 11 | 18: 9 12 | 20: 8 13 | 22: 6 14 | 24: 14 15 | 26: 12 16 | 28: 10 17 | 30: 12 18 | 32: 8 19 | 34: 10 20 | 36: 8 21 | 38: 8 22 | 40: 12 23 | 42: 12 24 | 44: 12 25 | 46: 12 26 | 48: 14 27 | 52: 14 28 | 54: 12 29 | 56: 12 30 | 58: 12 31 | 60: 12 32 | 62: 14 33 | 64: 14 34 | 66: 14 35 | 68: 14 36 | 70: 14 37 | 72: 14 38 | 80: 18 39 | 82: 14 40 | 84: 20 41 | 86: 14 42 | 90: 17 43 | 96: 20 44 | 98: 24 45 | -------------------------------------------------------------------------------- /2017/input/14.txt: -------------------------------------------------------------------------------- 1 | hxtvlmkl 2 | -------------------------------------------------------------------------------- /2017/input/15.txt: -------------------------------------------------------------------------------- 1 | Generator A starts with 783 2 | Generator B starts with 325 3 | -------------------------------------------------------------------------------- /2017/input/17.txt: -------------------------------------------------------------------------------- 1 | 377 2 | -------------------------------------------------------------------------------- /2017/input/18.txt: -------------------------------------------------------------------------------- 1 | set i 31 2 | set a 1 3 | mul p 17 4 | jgz p p 5 | mul a 2 6 | add i -1 7 | jgz i -2 8 | add a -1 9 | set i 127 10 | set p 618 11 | mul p 8505 12 | mod p a 13 | mul p 129749 14 | add p 12345 15 | mod p a 16 | set b p 17 | mod b 10000 18 | snd b 19 | add i -1 20 | jgz i -9 21 | jgz a 3 22 | rcv b 23 | jgz b -1 24 | set f 0 25 | set i 126 26 | rcv a 27 | rcv b 28 | set p a 29 | mul p -1 30 | add p b 31 | jgz p 4 32 | snd a 33 | set a b 34 | jgz 1 3 35 | snd b 36 | set f 1 37 | add i -1 38 | jgz i -11 39 | snd a 40 | jgz f -16 41 | jgz a -19 42 | -------------------------------------------------------------------------------- /2017/input/22.txt: -------------------------------------------------------------------------------- 1 | ..#..##...##.######.##... 2 | ..#...#####..#.#####..#.. 3 | ...##.#..##.#.##....#...# 4 | #.#.#.#..###...#....##..# 5 | ..#..#####.....##..#.#..# 6 | .##.#####.#.....###.#..#. 7 | ##..####...#.##.#...##... 8 | ###.#.#####...##.#.##..#. 9 | #.##..##.#....#.#..#.##.. 10 | ###.######......####..#.# 11 | ###.....#.##.##.######..# 12 | ...####.###.#....#..##.## 13 | #..####.#.....#....###.#. 14 | #..##..#.####.#.##..#.#.. 15 | #..#.#.##...#...#####.##. 16 | #.###..#.##.#..##.####### 17 | ...###..#..####.####.#.#. 18 | .#..###..###.#....####### 19 | .####..##.#####.#.#..#.#. 20 | #.#....##.....##.##.....# 21 | ....####.....#..#.##..##. 22 | ######..##..#.###...###.. 23 | ..##...##.....#..###.###. 24 | ##.#.#..##.#.#.##....##.# 25 | .#.###..##..#....#...##.# 26 | -------------------------------------------------------------------------------- /2017/input/23.txt: -------------------------------------------------------------------------------- 1 | set b 57 2 | set c b 3 | jnz a 2 4 | jnz 1 5 5 | mul b 100 6 | sub b -100000 7 | set c b 8 | sub c -17000 9 | set f 1 10 | set d 2 11 | set e 2 12 | set g d 13 | mul g e 14 | sub g b 15 | jnz g 2 16 | set f 0 17 | sub e -1 18 | set g e 19 | sub g b 20 | jnz g -8 21 | sub d -1 22 | set g d 23 | sub g b 24 | jnz g -13 25 | jnz f 2 26 | sub h -1 27 | set g b 28 | sub g c 29 | jnz g 2 30 | jnz 1 3 31 | sub b -17 32 | jnz 1 -23 33 | -------------------------------------------------------------------------------- /2017/input/24.txt: -------------------------------------------------------------------------------- 1 | 42/37 2 | 28/28 3 | 29/25 4 | 45/8 5 | 35/23 6 | 49/20 7 | 44/4 8 | 15/33 9 | 14/19 10 | 31/44 11 | 39/14 12 | 25/17 13 | 34/34 14 | 38/42 15 | 8/42 16 | 15/28 17 | 0/7 18 | 49/12 19 | 18/36 20 | 45/45 21 | 28/7 22 | 30/43 23 | 23/41 24 | 0/35 25 | 18/9 26 | 3/31 27 | 20/31 28 | 10/40 29 | 0/22 30 | 1/23 31 | 20/47 32 | 38/36 33 | 15/8 34 | 34/32 35 | 30/30 36 | 30/44 37 | 19/28 38 | 46/15 39 | 34/50 40 | 40/20 41 | 27/39 42 | 3/14 43 | 43/45 44 | 50/42 45 | 1/33 46 | 6/39 47 | 46/44 48 | 22/35 49 | 15/20 50 | 43/31 51 | 23/23 52 | 19/27 53 | 47/15 54 | 43/43 55 | 25/36 56 | 26/38 57 | 1/10 58 | -------------------------------------------------------------------------------- /2018/01.py: -------------------------------------------------------------------------------- 1 | """--- Day 1: Chronal Calibration ---""" 2 | 3 | with open("input/01.txt") as f: 4 | freq = list(map(int, f.read().split("\n")[:-1])) 5 | c = set([0]) 6 | a = i = 0 7 | while True: 8 | a += freq[i % len(freq)] 9 | if a in c: 10 | break 11 | c.add(a) 12 | i += 1 13 | print(sum(freq)) 14 | # 536 15 | print(a) 16 | # 75108 17 | -------------------------------------------------------------------------------- /2018/01.q: -------------------------------------------------------------------------------- 1 | // --- Day 1: Chronal Calibration --- 2 | 3 | sum r:"J"$read0 `:input/01.txt 4 | / 536 5 | d?min d:.[;(::;1)] group 0,sums 200000#r 6 | / 75108 7 | 8 | \ 9 | k)(+/r),d?&/d:.[;(::;1)]@=0,+\200000#r:"J"$0:`:input/01.txt 10 | -------------------------------------------------------------------------------- /2018/02.py: -------------------------------------------------------------------------------- 1 | """--- Day 2: Inventory Management System ---""" 2 | 3 | with open("input/02.txt", "r") as f: 4 | words = f.read().split("\n")[:-1] 5 | twos = threes = 0 6 | diff = None 7 | for word in words: 8 | two = three = 0 9 | for letter in set(word): 10 | c = word.count(letter) 11 | if c == 2: 12 | two = 1 13 | if c == 3: 14 | three = 1 15 | twos += two 16 | threes += three 17 | if diff is None: 18 | for wrd in words: 19 | m = [x != y for x, y in zip(word, wrd)] 20 | if sum(m) == 1: 21 | i = m.index(True) 22 | diff = word[:i] + word[i+1:] 23 | break 24 | print(twos * threes) 25 | # 5658 26 | print(diff) 27 | # nmgyjkpruszlbaqwficavxneo 28 | -------------------------------------------------------------------------------- /2018/02.q: -------------------------------------------------------------------------------- 1 | // --- Day 2: Inventory Management System --- 2 | 3 | prd sum 2 3 in/:count@''group each r:read0 `:input/02.txt 4 | /5658i 5 | {x where x=y}. r where sum@'1=sum@''not r=/:\:r 6 | /"nmgyjkpruszlbaqwficavxneo" 7 | 8 | \ 9 | k)(*/+/2 3 in/:#:''=:'r),,{x@&x=y}. r@&+/'1=+/''~r=/:\:r:0:`:input/02.txt 10 | -------------------------------------------------------------------------------- /2018/03.q: -------------------------------------------------------------------------------- 1 | // --- Day 3: No Matter How You Slice It --- 2 | 3 | sum 1sum each raze each a 10 | / 35928 11 | -------------------------------------------------------------------------------- /2018/07.q: -------------------------------------------------------------------------------- 1 | // --- Day 7: The Sum of Its Parts --- 2 | 3 | t:select raze b by a from flip `a`b!(" c c";" ")0:`:input/07.txt 4 | 5 | r:{ x,first $[count ex:exec a from t where not a in x, not a in\:raze b;ex;exec b from t where not a in (-1_x)] }/[""];r 6 | /"BFGKNRTWXZIHUMPQLVOYJACDSE" 7 | 8 | / offset & workers 9 | offset:60;workers:5 10 | / create worker queues 11 | W:workers#enlist""; 12 | / queue of work 13 | q:(offset+1+.Q.A?r)#'r 14 | / done, in progress 15 | dn:ip:"" 16 | / next job: 17 | nj:{[] 18 | n:first (exec a from t where not a in dn, not a in\:raze b) except ip; 19 | / last item in the queue and nothing else in progress 20 | $[(0=count ip)&(1=count q)&null n;first (first q) except ip;n] 21 | }; 22 | / counter 23 | i:0 24 | / process work 25 | while[count[dn]=c:count distinct first p:f x;[m::c;1b];0b] };0] 8 | 9 | -1@trim r where not all null flip r:./[(g;g:1+max (raze/) msg)#" ";;:;"#"] msg:flip f s; 10 | /###### ##### ###### # # # ### ###### #### 11 | /# # # # # # # # # # # 12 | /# # # # # # # # # # 13 | /# # # # # # # # # # 14 | /##### ##### # ## # # # # 15 | /# # # ## # # # # ### 16 | /# # # # # # # # # # 17 | /# # # # # # # # # # # 18 | /# # # # # # # # # # ## 19 | /# # ###### # # ###### ### ###### ### # 20 | -1"\n",string s; 21 | /10867 22 | -------------------------------------------------------------------------------- /2018/11.q: -------------------------------------------------------------------------------- 1 | // --- Day 11: Chronal Charge --- 2 | 3 | sn:"J"$first read0 `:input/11.txt 4 | g:300 5 | / apply power to grid 6 | grid:(2#g)#{ mod[div[(x+11)*sn+(1+y)*x+11;100];10]-5 }.'raze til[g],\:/:til g; 7 | / determine max and coords of max 8 | f:{ (max r;1+(mod[;1+g-x];div[;1+g-x])@\:r?max r:{ sum raze .[grid;x] } each (x cut) each raze a,/:\:a:til[1+g-x]+\:til x) } 9 | / solve for sliding window of size 3 10 | -1@","sv string last f 3; 11 | / "20,62" 12 | -1@{ (","sv string last x),",",string 2 + y }[;w] res w:res[;0]?max (res:f peach 2 + til 18)[;0]; 13 | / "229,61,16" 14 | -------------------------------------------------------------------------------- /2018/12.q: -------------------------------------------------------------------------------- 1 | // --- Day 12: Subterranean Sustainability --- 2 | 3 | i:read0 `:input/12.txt 4 | padding:4;generations:200 5 | state:(padding#0b),("#"=is:(" "vs i 0) 2),generations#0b 6 | rules:raze { $["#"=first (x:" "vs x) 2;enlist "#"=x 0;()] } each 2_ i 7 | states:(); 8 | 9 | while[generations > count states,:enlist state:(state (-2+til 5)+/:til count state) in rules]; 10 | 11 | sum neg[padding]+where states 19 12 | / 3051 13 | sum (50000000000-count states)+neg[padding]+where state 14 | / 1300000000669 15 | -------------------------------------------------------------------------------- /2018/13.q: -------------------------------------------------------------------------------- 1 | /--- Day 13: Mine Cart Madness --- 2 | 3 | / coordinates of carts 4 | carts:raze { y,'where any x=/:"^v<>" }'[r;til count r:read0 `:input/13.txt] 5 | / current direction of carts 6 | carts:carts,'enlist each t:.[r;] each carts 7 | / set next turn to left 8 | carts:carts,'"l" 9 | / replace carts with straight track 10 | .[`r;;:;].'flip(carts[;0 1];"|-"t in\:"<>"); 11 | 12 | dir:{ (-1 0;0 1;1 0;0 -1) "^>v<"?x } 13 | turn:{ (("<>";"><";"v^";"^v")"^v><"?x)y="/" } 14 | crossroad:{ ((("<^>";">v<";"^>v";"v<^")"^v><"?x)"lsr"?y;"srl""lsr"?y) } 15 | 16 | crashed:0b 17 | go:{[x] 18 | / already crashed 19 | if[x in d;:()]; 20 | / pick out cart 21 | c:carts x; 22 | / position of moved cart 23 | p:.[r;] n:c[0 1] + dir c 2; 24 | / crash 25 | if[n in carts[;0 1]; 26 | / part 1 27 | if[not crashed; 28 | -1@","sv string reverse n; 29 | / 58,93 30 | crashed::1b 31 | ]; 32 | / return crasher and crashee 33 | :d,:x,first where carts[;0 1]~\:n 34 | ]; 35 | / turn cart 36 | if[p in "/\\"; 37 | c[2]:turn[c 2;p] 38 | ]; 39 | / crossroad 40 | if[p="+"; 41 | c[2 3]:crossroad[c 2;c 3]; 42 | ]; 43 | / update new cart position 44 | c[0 1]:n; 45 | / update cart in carts 46 | carts[x]:c; 47 | / nothing to return 48 | :() 49 | }; 50 | 51 | while[not 1=count carts; 52 | / reset drop list 53 | d:(); 54 | / process each cart 55 | res:raze go each iasc carts[;0 1]; 56 | / drop crashed carts 57 | carts:carts (til count carts) except res; 58 | ]; 59 | 60 | -1","sv string reverse first carts[;0 1]; 61 | / 91,72 62 | -------------------------------------------------------------------------------- /2018/14.q: -------------------------------------------------------------------------------- 1 | // --- Day 14: Chocolate Charts --- 2 | 3 | s:string i:"J"$first read0 `:input/14.txt 4 | r:"37"; 5 | f:{ mod[1 + x + a;count r,:string sum a:"J"$string r x] } 6 | 7 | f/[{count[r] < 10+i};0 1]; 8 | -1@10#i _r; 9 | / 1191216109 10 | 11 | r:"37";n:`long$() 12 | f/[{ss[-7#r;s]~n};0 1]; 13 | $[s~-1_-7#r;-7;-6] + count r 14 | /20268576 15 | -------------------------------------------------------------------------------- /2018/18.q: -------------------------------------------------------------------------------- 1 | // --- Day 18: Settlers of The North Pole --- 2 | 3 | g:count r:read0 `:input/18.txt 4 | s:{x+/:(-1 0;-1 1;0 1;1 1;1 0;1 -1;0 -1;-1 -1)} each c:til[g] cross til g 5 | 6 | f:{ 7 | ca:.[x;y]; 8 | sa:raze .[x;] each z; 9 | $[ca="."; 10 | ".|"2count r3s; 24 | r5:AND[r1;255]; 25 | r3+:r5; 26 | r3:AND[r3;16777215]; 27 | r3*:65899; 28 | r3:AND[r3;16777215]; 29 | $[256>r1; 30 | [ 31 | r3s,:r3; 32 | r1:OR[r3;65536]; 33 | r3:10373714 34 | ]; 35 | [ 36 | r1:r1 div 256 37 | ] 38 | ] 39 | ]; 40 | 41 | r3s (count distinct r3s) - 1 42 | /16477902 43 | -------------------------------------------------------------------------------- /2018/22.q: -------------------------------------------------------------------------------- 1 | // --- Day 22: Mode Maze --- 2 | 3 | depth:"J"$last first i:" "vs'read0 `:input/22.txt 4 | target:"J"$","vs last last i 5 | gridsize:5+max target 6 | g:(`u#enlist 0 0)!enlist 0 7 | 8 | { 9 | g[x,y]:mod[;20183] depth + $[x=0; 10 | y*48271; 11 | y=0; 12 | x*16807; 13 | target~x,y; 14 | 0; 15 | prd g (-1 0;0 -1)+\:x,y] 16 | } .' til[gridsize] cross til gridsize; 17 | 18 | G:flip ./[(2#gridsize)#0;key g;:;mod[value g;3]] 19 | sum raze .[G;reverse til each 1 + target] 20 | /11462 21 | -------------------------------------------------------------------------------- /2018/23.q: -------------------------------------------------------------------------------- 1 | // --- Day 23: Experimental Emergency Teleportation --- 2 | 3 | / read input 4 | i:{"J"$","vs x inter .Q.n,",-" } each read0 `:input/23.txt 5 | / index of strongest nanobot 6 | si:i[;3]?max i[;3] 7 | 8 | sum i[si;3]>=sum each abs i[si;0 1 2]-/:i[;0 1 2] 9 | / 294 10 | -------------------------------------------------------------------------------- /2018/25.q: -------------------------------------------------------------------------------- 1 | // --- Day 25: Four-Dimensional Adventure --- 2 | 3 | C:enlist first w:where each 4>sum@''abs c-\:/:c:"J"$","vs'read0 `:input/25.txt; 4 | 5 | { 6 | $[s:sum a:any each in[;x] each C; 7 | // known constellation 8 | $[1=s; 9 | // add to single constellation 10 | C[first where a]:distinct C[first where a],x; 11 | [ 12 | // merge into first found 13 | C[first where a]:raze C[where a]; 14 | //{ C::C _ x } each reverse 1 _ where a 15 | C::C _ last where a // only need to merge max of 2 constellations 16 | ] 17 | ]; 18 | // else unknown constellation 19 | C,:x 20 | ] 21 | } each w; 22 | 23 | count C 24 | // 381 25 | -------------------------------------------------------------------------------- /2018/input/06.txt: -------------------------------------------------------------------------------- 1 | 80, 357 2 | 252, 184 3 | 187, 139 4 | 101, 247 5 | 332, 328 6 | 302, 60 7 | 196, 113 8 | 271, 201 9 | 334, 89 10 | 85, 139 11 | 327, 161 12 | 316, 352 13 | 343, 208 14 | 303, 325 15 | 316, 149 16 | 270, 319 17 | 318, 153 18 | 257, 332 19 | 306, 348 20 | 299, 358 21 | 172, 289 22 | 303, 349 23 | 271, 205 24 | 347, 296 25 | 220, 276 26 | 235, 231 27 | 133, 201 28 | 262, 355 29 | 72, 71 30 | 73, 145 31 | 310, 298 32 | 138, 244 33 | 322, 334 34 | 278, 148 35 | 126, 135 36 | 340, 133 37 | 311, 118 38 | 193, 173 39 | 319, 99 40 | 50, 309 41 | 160, 356 42 | 155, 195 43 | 61, 319 44 | 80, 259 45 | 106, 318 46 | 49, 169 47 | 134, 61 48 | 74, 204 49 | 337, 174 50 | 108, 287 51 | -------------------------------------------------------------------------------- /2018/input/09.txt: -------------------------------------------------------------------------------- 1 | 476 players; last marble is worth 71657 points 2 | -------------------------------------------------------------------------------- /2018/input/11.txt: -------------------------------------------------------------------------------- 1 | 7139 2 | -------------------------------------------------------------------------------- /2018/input/12.txt: -------------------------------------------------------------------------------- 1 | initial state: ###..#...####.#..###.....####.######.....##.#####.##.##..###....#....##...##...##.#..###..#.#...#..# 2 | 3 | .###. => . 4 | ..#.. => . 5 | .#### => . 6 | .##.. => # 7 | #.#.# => . 8 | ..#.# => # 9 | #.##. => # 10 | #...# => # 11 | ..... => . 12 | ##..# => # 13 | .#.#. => . 14 | ..##. => # 15 | ##.#. => . 16 | ###.. => . 17 | .#... => # 18 | ..### => . 19 | #..## => . 20 | ...#. => . 21 | ###.# => # 22 | .##.# => . 23 | .#.## => . 24 | ....# => . 25 | ##### => . 26 | #.#.. => # 27 | ...## => # 28 | #.... => . 29 | #.### => # 30 | ##... => # 31 | .#..# => . 32 | ####. => . 33 | #..#. => # 34 | ##.## => # 35 | -------------------------------------------------------------------------------- /2018/input/14.txt: -------------------------------------------------------------------------------- 1 | 190221 2 | -------------------------------------------------------------------------------- /2018/input/15.txt: -------------------------------------------------------------------------------- 1 | ################################ 2 | #########.####...#####.######### 3 | #####...#G...#.G.##.#...##.##..# 4 | ####...G####.G....#..........E.# 5 | #####..#######.................# 6 | #####..###########.............# 7 | #GG...############.............# 8 | #...#.#.##..######..........#..# 9 | ##............#G.#..E.......#### 10 | ##.G........#....#.........##### 11 | ###....G...................##### 12 | ###............G.....G.......### 13 | #####.....#..G#####..........### 14 | ####..#......#######G...#.E..E## 15 | ####G##.G...#########.#.......## 16 | ###..###....#########...E....### 17 | ##...G......#########.E...###### 18 | ##G.........#########......##### 19 | ##...#.G....#########.#...###### 20 | ##...#.......#######E.########## 21 | ####.#........#####...########## 22 | #######............E..########## 23 | ####..#...........E############# 24 | ##...G#...........############## 25 | ##........#.......############## 26 | #####G..###..E..################ 27 | ##########......################ 28 | ##########.....################# 29 | #########......################# 30 | ###########.....################ 31 | ###########...################## 32 | ################################ 33 | -------------------------------------------------------------------------------- /2018/input/22.txt: -------------------------------------------------------------------------------- 1 | depth: 7863 2 | target: 14,760 3 | -------------------------------------------------------------------------------- /2019/01.js: -------------------------------------------------------------------------------- 1 | // --- Day 1: The Tyranny of the Rocket Equation --- 2 | 3 | const fs = require('fs') 4 | 5 | function fuel (x) { 6 | return Math.max(0, Math.floor(x / 3) - 2) 7 | } 8 | 9 | function sum (x, y) { 10 | return x + y 11 | } 12 | 13 | fs.readFile('input/01.txt', { encoding: 'utf-8' }, (err, data) => { 14 | if (!err) { 15 | const lines = data 16 | .split('\n') 17 | .filter(x => x.length) 18 | .map(Number) 19 | 20 | const p1 = lines.map(fuel).reduce(sum) 21 | const p2 = lines.map(x => { 22 | let f = 0 23 | while ((x = fuel(x)) > 0) { 24 | f += x 25 | } 26 | return f 27 | }).reduce(sum) 28 | 29 | console.log(p1) 30 | console.log(p2) 31 | } 32 | }) 33 | -------------------------------------------------------------------------------- /2019/01.q: -------------------------------------------------------------------------------- 1 | //--- Day 1: The Tyranny of the Rocket Equation --- 2 | 3 | sum (r:{0|-2+x div 3}\'["J"$read0`:input/01.txt])[;1] 4 | /3291760 5 | sum sum r[;1+til 10] 6 | /4934767 7 | 8 | \ 9 | k)(+/r[;1]),+/+/(r:{0|-2+x div 3}\'["J"$0:`:input/01.txt])[;1+!9] 10 | -------------------------------------------------------------------------------- /2019/02.js: -------------------------------------------------------------------------------- 1 | // --- Day 2: 1202 Program Alarm --- 2 | 3 | const fs = require('fs') 4 | 5 | function op (i, n, v) { 6 | i[1] = n 7 | i[2] = v 8 | let idx = 0 9 | while (idx < i.length) { 10 | if (i[idx] === 1) { 11 | i[i[idx + 3]] = i[i[idx + 1]] + i[i[idx + 2]] 12 | } else if (i[idx] === 2) { 13 | i[i[idx + 3]] = i[i[idx + 1]] * i[i[idx + 2]] 14 | } 15 | idx += 4 16 | } 17 | return i[0] 18 | } 19 | 20 | fs.readFile('input/02a.txt', { encoding: 'utf-8' }, (err, data) => { 21 | if (!err) { 22 | const code = data.split(',').map(Number) 23 | 24 | console.log(op([...code], 12, 2)) 25 | 26 | const target = 19690720 27 | let noun = 0 28 | let verb = 0 29 | 30 | let res 31 | while ((res = op([...code], ++noun, 0)) < target) {} 32 | noun-- 33 | while ((res = op([...code], noun, ++verb)) !== target) {} 34 | 35 | console.log(100 * noun + verb) 36 | } 37 | }) 38 | -------------------------------------------------------------------------------- /2019/02.q: -------------------------------------------------------------------------------- 1 | //--- Day 2: 1202 Program Alarm --- 2 | 3 | f:{[i;stdin] 4 | 5 | O:( 6 | // 0 noop 7 | { -1"noop" }; 8 | // 1 adds, z:x+y 9 | { (0N;z;x+y) }; 10 | // 2 multiplies, z:x*y 11 | { (0N;z;x*y) }; 12 | // 3 stores input in x 13 | { r:first STDIN;STDIN::1_STDIN;(0N;x;r) }; 14 | // 4 outputs x 15 | { if[x>0;STDOUT::x];0N }; 16 | // 5 jump if true 17 | { $[x;y;0N] }; 18 | // 6 jump if false 19 | { $[x;0N;y] }; 20 | // 7 less-than 21 | { (0N;z;"j"$xo:first ii:reverse (5#10) vs i p; 33 | a:i p + 1 + til A o; 34 | if[o in 1 2 5 6 7 8; 35 | a[w]:i a w:where not 2#2_ii; 36 | ]; 37 | if[o=4; 38 | a[w]:i a w:where not 1#2_ii; 39 | ]; 40 | if[1R:first first f[@[I;1 2;:;(N;V)];0];N+:1]; 59 | while[W&N; 60 | V:0; 61 | while[W&T>R:first first f[@[I;1 2;:;(N;V)];0];V+:1]; 62 | $[T=R;W:0b;N-:1] 63 | ]; 64 | 65 | -1@string V+100*N 66 | /3146 67 | ]; 68 | -------------------------------------------------------------------------------- /2019/03.q: -------------------------------------------------------------------------------- 1 | //--- Day 3: Crossed Wires --- 2 | 3 | f:{ 4 | r:{ 5 | p:first x; 6 | d:first y; 7 | t:1 + til"J"$1_y; 8 | s:$[d="U"; 9 | (p[0]+t),'p 1; 10 | d="D"; 11 | (p[0]-t),'p 1; 12 | d="R"; 13 | p[0],'(p[1]+t); 14 | p[0],'(p[1]-t) 15 | ]; 16 | (last s;s) 17 | }\[enlist 0 0;x]; 18 | :last each r 19 | }; 20 | 21 | min sum each abs c:(inter). w:raze each f each i:","vs'read0 `:input/03.txt 22 | /489 23 | min { sum 1+x?\:y }[w;] each c 24 | /93654 25 | -------------------------------------------------------------------------------- /2019/04.q: -------------------------------------------------------------------------------- 1 | //--- Day 4: Secure Container --- 2 | 3 | s:string {x+til 1+y-x}."J"$"-"vs first read0`:input/04.txt 4 | sum any flip r=prev@'r:s where all flip s>=prev@'s 5 | /1099 6 | sum 2 in'count@''group each r 7 | /710 8 | -------------------------------------------------------------------------------- /2019/05.q: -------------------------------------------------------------------------------- 1 | //--- Day 5: Sunny with a Chance of Asteroids --- 2 | 3 | \l 09.q 4 | 5 | g[I:"J"$"," vs first read0 `:input/05.txt;1#`a] 5#1 6 | /12428642 7 | g[I;1#`a] 5#5 8 | /918655 9 | -------------------------------------------------------------------------------- /2019/06.q: -------------------------------------------------------------------------------- 1 | //--- Day 6: Universal Orbit Map --- 2 | 3 | t:flip`obj`orb!flip`$")"vs'read0 `:input/06.txt 4 | f:{exec orb from t where obj = x} 5 | g:{exec obj from t where orb = x} 6 | 7 | o:0 8 | 9 | { r:f first p:first x; 10 | o+:d:last p; 11 | 1_x,r,\:d+1 12 | }/[count;(f `COM),\:1]; 13 | 14 | o 15 | /245089 16 | -2+first sum where each i=first(inter/)i:{first g x}\'[`YOU`SAN] 17 | /511 18 | -------------------------------------------------------------------------------- /2019/07.q: -------------------------------------------------------------------------------- 1 | //--- Day 7: Amplification Circuit --- 2 | 3 | \l 09.q 4 | 5 | I:"J"$"," vs first read0 `:input/07.txt; 6 | max g[I;`a`b`c`d`e] each d where (all each 5>d) and 5=count each d:distinct each "J"$string "0"^-5$string til 43211 7 | /65464 8 | max g[I;`a`b`c`d`e] each d where (all each 4x }'[i;til count i] 6 | 7 | f:{ 8 | ae:all each 0>a:(x except enlist y)-\:y; 9 | cnt:count fnd a where not[ae]&a[;1]<0; 10 | cnt+:count fnd a where all each a>=0; 11 | cnt+:count fnd a where not[ae]&a[;0]<0; 12 | cnt+:count fnd a where ae; 13 | cnt 14 | } 15 | 16 | fnd:{group (%).'x} 17 | lzr:{x value first each a!g a:asc key g:fnd x} 18 | 19 | g:{ 20 | ae:all each 0>a:(x except enlist y)-\:y; 21 | q1:lzr a where not[ae]&a[;0]<0; 22 | q2:lzr a where all each a>=0; 23 | q3:lzr a where not[ae]&a[;1]<0; 24 | q4:lzr a where ae; 25 | y+/:q1,q2,q3,q4 26 | } 27 | 28 | max r:ast f/:ast 29 | /278 30 | sum 100 1*reverse (g[ael iasc sum each abs l-/:ael:ast except enlist l;l:ast r?max r]) 199 31 | /1417 32 | -------------------------------------------------------------------------------- /2019/11.q: -------------------------------------------------------------------------------- 1 | //--- Day 11: Space Police --- 2 | 3 | \l 09.q 4 | 5 | f:{[i;p] 6 | if[9 9~2#ii:reverse (5#10) vs i p; 7 | :() 8 | ]; 9 | a:i p + 1 + til A o:first ii; 10 | if[3=o; 11 | STDIN::0^PANELS LOC; 12 | ]; 13 | if[not o=3; 14 | a[w]:i a w:where 0=B[o]#2_ii; 15 | a[w]:i BASE + a w:where 2=B[o]#2_ii 16 | if[2=ii 4; 17 | a[2]:BASE + a 2 18 | ]; 19 | ]; 20 | if[1x[0 1 2]) + (0 -1 y[0 1 2]paddle; 21 | 1; 22 | 0] 23 | ]; 24 | if[not o=3; 25 | a[w]:i a w:where 0=B[o]#2_ii; 26 | a[w]:i BASE + a w:where 2=B[o]#2_ii 27 | if[2=ii 4; 28 | a[2]:BASE + a 2 29 | ]; 30 | ]; 31 | if[1y 0;x;.[x;y 0 1;:;" $=#o" y 2]] }/[38 21#" ";x] } 53 | -------------------------------------------------------------------------------- /2019/14.q: -------------------------------------------------------------------------------- 1 | //--- Day 14: Space Stoichiometry --- 2 | 3 | i:" => " vs/:i:read0 `:input/14.txt 4 | 5 | tab:flip `res`qty`ing!flip (reverse each "JS"$/:" "vs'i[;1]),'flip enlist reverse@''"JS"$/:/:" "vs''", "vs/:i[;0] 6 | 7 | leftovers:enlist[`]!enlist 0N 8 | 9 | f:{ 10 | ore:0; 11 | queue:enlist (`FUEL;x); 12 | 13 | while[count queue; 14 | pop:first queue; 15 | queue:1_queue; 16 | 17 | item:first pop; 18 | required:last pop; 19 | 20 | creates:first exec qty from tab where res=item; 21 | 22 | if[(0=left:required mod creates; 23 | required-:left; 24 | leftovers[item]-:left 25 | ]; 26 | leftovers[item]+:(creates - required mod creates) mod creates; 27 | 28 | $[`ORE~item; 29 | ore+:required; 30 | [ 31 | ing:raze exec ing from tab where res=item; 32 | queue,:ing[;0],'(ceiling required%creates)*ing[;1] 33 | ] 34 | ] 35 | ]; 36 | ore 37 | }; 38 | 39 | f 1 40 | /301997 41 | 42 | TARGET:1000000000000 43 | MIN:1 44 | MAX:TARGET 45 | FUEL:1 46 | LAST:0N 47 | 48 | while[LAST<>FUEL; 49 | LAST:FUEL; 50 | $[TARGETTARGET;FUEL-1;FUEL] 57 | /6216589 58 | -------------------------------------------------------------------------------- /2019/16.q: -------------------------------------------------------------------------------- 1 | //--- Day 16: Flawed Frequency Transmission --- 2 | 3 | p:{ 1_'(1 + x)#'{ raze x#'0 1 0 -1 } each 1 + til x } 4 | 5 | m:p count i:"J"$'first read0 `:input/16.txt 6 | 7 | f:{ mod[;10] abs sum each x*/:y }[;m] 8 | g:{ mod[;10] sums x } 9 | 10 | 10 sv 8#f/[100;i] 11 | /59522422 12 | 10 sv 8#reverse g/[100;reverse (10 sv 7#i) _ raze 10000#enlist i] 13 | /18650834 14 | -------------------------------------------------------------------------------- /2019/22.q: -------------------------------------------------------------------------------- 1 | //--- Day 22: Slam Shuffle --- 2 | 3 | c:{ $[y>0;(y _ x),y#x;(y # x),y _ x] } 4 | d:{ x iasc mod[;count x] y * til count x } 5 | 6 | f:{ 7 | $[y like "*stack*"; 8 | reverse; 9 | y like "*cut*"; 10 | c[;"J"$ last " " vs y]; 11 | d[;"J"$ last " " vs y] 12 | ] x 13 | }; 14 | 15 | r:f/[til 10007;i:read0 `:input/22.txt] 16 | 17 | r?2019 18 | /2939 19 | -------------------------------------------------------------------------------- /2019/24.q: -------------------------------------------------------------------------------- 1 | //--- Day 24: Planet of Discord --- 2 | 3 | g:@[(raze til[5],\:/:til 5)!25#0b;raze (where each i="#"),\:'til count i:read0 `:input/24.txt;:;1b] 4 | 5 | f:{ key[x]!{ $[x;y=1;y in 1 2] }'[value x;sum each x (0 1;1 0;0 -1;-1 0)+\:/:key x] } 6 | 7 | k:enlist value g; 8 | 9 | while[not value[g:f g] in k;k,:value g]; 10 | 11 | sum g*prds 1,24#2 12 | /18400817 13 | -------------------------------------------------------------------------------- /2019/input/01.txt: -------------------------------------------------------------------------------- 1 | 73365 2 | 84016 3 | 98122 4 | 114871 5 | 111575 6 | 130294 7 | 59341 8 | 102810 9 | 92986 10 | 81275 11 | 83155 12 | 74530 13 | 67858 14 | 54107 15 | 62099 16 | 82535 17 | 122085 18 | 139127 19 | 131939 20 | 130125 21 | 96285 22 | 70572 23 | 129325 24 | 132261 25 | 69597 26 | 93574 27 | 70089 28 | 130711 29 | 109220 30 | 50155 31 | 98250 32 | 51054 33 | 100028 34 | 86985 35 | 106638 36 | 116993 37 | 95130 38 | 115571 39 | 56707 40 | 105988 41 | 123358 42 | 95470 43 | 71094 44 | 126310 45 | 66153 46 | 128788 47 | 51605 48 | 70044 49 | 70180 50 | 141905 51 | 96494 52 | 89332 53 | 96688 54 | 131936 55 | 83782 56 | 108218 57 | 60650 58 | 91289 59 | 126406 60 | 112939 61 | 76303 62 | 115774 63 | 135382 64 | 116478 65 | 77898 66 | 98611 67 | 89155 68 | 114878 69 | 110085 70 | 114482 71 | 61585 72 | 103480 73 | 99198 74 | 81082 75 | 113501 76 | 100167 77 | 100085 78 | 128747 79 | 79731 80 | 93289 81 | 121516 82 | 116380 83 | 56592 84 | 146611 85 | 132595 86 | 60483 87 | 105828 88 | 118923 89 | 141972 90 | 126450 91 | 77214 92 | 97406 93 | 77745 94 | 101982 95 | 120919 96 | 71085 97 | 118300 98 | 92440 99 | 115184 100 | 142447 101 | -------------------------------------------------------------------------------- /2019/input/02.txt: -------------------------------------------------------------------------------- 1 | 1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,6,19,23,2,23,6,27,1,5,27,31,1,31,9,35,2,10,35,39,1,5,39,43,2,43,10,47,1,47,6,51,2,51,6,55,2,55,13,59,2,6,59,63,1,63,5,67,1,6,67,71,2,71,9,75,1,6,75,79,2,13,79,83,1,9,83,87,1,87,13,91,2,91,10,95,1,6,95,99,1,99,13,103,1,13,103,107,2,107,10,111,1,9,111,115,1,115,10,119,1,5,119,123,1,6,123,127,1,10,127,131,1,2,131,135,1,135,10,0,99,2,14,0,0 2 | -------------------------------------------------------------------------------- /2019/input/04.txt: -------------------------------------------------------------------------------- 1 | 245182-790572 2 | -------------------------------------------------------------------------------- /2019/input/07.txt: -------------------------------------------------------------------------------- 1 | 3,8,1001,8,10,8,105,1,0,0,21,30,39,64,81,102,183,264,345,426,99999,3,9,1001,9,2,9,4,9,99,3,9,1002,9,4,9,4,9,99,3,9,1002,9,5,9,101,2,9,9,102,3,9,9,1001,9,2,9,1002,9,2,9,4,9,99,3,9,1002,9,3,9,1001,9,5,9,1002,9,3,9,4,9,99,3,9,102,4,9,9,1001,9,3,9,102,4,9,9,1001,9,5,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,99 2 | -------------------------------------------------------------------------------- /2019/input/10.txt: -------------------------------------------------------------------------------- 1 | .#......#...#.....#..#......#..##..# 2 | ..#.......#..........#..##.##....... 3 | ##......#.#..#..#..##...#.##.###.... 4 | ..#........#...........#.......##... 5 | .##.....#.......#........#..#.#..... 6 | .#...#...#.....#.##.......#...#....# 7 | #...#..##....#....#......#.......... 8 | ....#......#.#.....#..#...#......#.. 9 | ......###.......#..........#.##.#... 10 | #......#..#.....#..#......#..#..#### 11 | .##...##......##..#####.......##.... 12 | .....#...#.........#........#....#.. 13 | ....##.....#...#........#.##..#....# 14 | ....#........#.###.#........#...#..# 15 | ....#..#.#.##....#.........#.....#.# 16 | ##....###....##..#..#........#...... 17 | .....#.#.........#.......#....#....# 18 | .###.....#....#.#......#...##.##.... 19 | ...##...##....##.........#...#...... 20 | .....#....##....#..#.#.#...##.#...#. 21 | #...#.#.#.#..##.#...#..#..#..#...... 22 | ......#...#...#.#.....#.#.....#.#### 23 | ..........#..................#.#.##. 24 | ....#....#....#...#..#....#.....#... 25 | .#####..####........#............... 26 | #....#.#..#..#....##......#...#..... 27 | ...####....#..#......#.#...##.....#. 28 | ..##....#.###.##.#.##.#.....#......# 29 | ....#.####...#......###.....##...... 30 | .#.....#....#......#..#..#.#..#..... 31 | ..#.......#...#........#.##...#..... 32 | #.....####.#..........#.#.......#... 33 | ..##..#..#.....#.#.........#..#.#.## 34 | .........#..........##.#.##.......## 35 | #..#.....#....#....#.#.......####..# 36 | ..............#.#...........##.#.#.. 37 | -------------------------------------------------------------------------------- /2019/input/12.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /2019/input/16.txt: -------------------------------------------------------------------------------- 1 | 59791871295565763701016897619826042828489762561088671462844257824181773959378451545496856546977738269316476252007337723213764111739273853838263490797537518598068506295920453784323102711076199873965167380615581655722603274071905196479183784242751952907811639233611953974790911995969892452680719302157414006993581489851373437232026983879051072177169134936382717591977532100847960279215345839529957631823999672462823375150436036034669895698554251454360619461187935247975515899240563842707592332912229870540467459067349550810656761293464130493621641378182308112022182608407992098591711589507803865093164025433086372658152474941776320203179747991102193608 2 | -------------------------------------------------------------------------------- /2019/input/24.txt: -------------------------------------------------------------------------------- 1 | .##.. 2 | ##.#. 3 | ##.## 4 | .#..# 5 | #.### 6 | -------------------------------------------------------------------------------- /2020/01.q: -------------------------------------------------------------------------------- 1 | //--- Day 1: Report Repair --- 2 | 3 | prd i inter j:2020-i:"J"$read0 `:input/01.txt 4 | /996996 5 | prd i inter raze j-/:i 6 | /9210402 7 | -------------------------------------------------------------------------------- /2020/02.q: -------------------------------------------------------------------------------- 1 | //--- Day 2: Password Philosophy --- 2 | 3 | sum { within[sum y=z;x] }.'i:{ ("J"$"-"vs x;first y;z) }.'" "vs'read0 `:input/02.txt 4 | /418 5 | sum { 1=sum y=z x-1 }.'i 6 | /616 7 | -------------------------------------------------------------------------------- /2020/03.q: -------------------------------------------------------------------------------- 1 | //--- Day 3: Toboggan Trajectory --- 2 | 3 | f:{ x[y;z mod count x y] }; 4 | g:{ "j"$ sum f[x;;]'[z*til count x;y*til count x] }; 5 | 6 | g[;3;1] t:"#"=read0 `:input/03.txt 7 | / 162 8 | prd g[t;;].'(1 1;3 1;5 1;7 1;1 2) 9 | / 3064612320 10 | -------------------------------------------------------------------------------- /2020/04.q: -------------------------------------------------------------------------------- 1 | //--- Day 4: Passport Processing --- 2 | 3 | p:{(0,where 0=count each x) _ x} read0 `:input/04.txt 4 | t:raze {[x;y] update id:y from (flip `f`v!"S*"$flip ":"vs'" "vs" "sv x where 03 occupied 9 | ] 10 | } 11 | 12 | g:{ "j"$c cut f[x;] peach (til count x) cross til c:count first x } 13 | 14 | sum raze g/[i:(".L#"!0N 0 1) read0 `:input/11.txt] 15 | /2296 16 | 17 | f:{[x;y] 18 | if[0N=s:.[x;y];:0N]; 19 | a:sum { first d where not null d:.[x;] each y+/:z*/:1+til 20 }[x;y] each (0 1;0 -1;1 0;-1 0;1 1;-1 -1;-1 1;1 -1); 20 | $[s=0; 21 | a=0; // empty seat becomes occupied if no adjacent 22 | a<5 // occupied seat beomes empty if >4 occupied 23 | ] 24 | } 25 | 26 | sum raze g/[i] 27 | /2089 28 | -------------------------------------------------------------------------------- /2020/12.q: -------------------------------------------------------------------------------- 1 | //--- Day 12: Rain Risk --- 2 | 3 | f:{[pd;a] 4 | p:first pd; 5 | d:last pd; 6 | if[a[0] in `N`E`S`W; 7 | :(p+a[1]*(`N`S`E`W!(0 1;0 -1;1 0;-1 0)) a[0];d) 8 | ]; 9 | if[a[0]=`F; 10 | :(p+a[1]*(`N`S`E`W!(0 1;0 -1;1 0;-1 0)) d;d) 11 | ]; 12 | if[a[0] in `L`R; 13 | r:"j"$a[1] % 90; 14 | :$[a[0]=`L; 15 | (p;{(`N`E`S`W!`W`N`E`S) x}/[r;d]); 16 | (p;{(`N`E`S`W!`E`S`W`N) x}/[r;d]) 17 | ] 18 | ]; 19 | }; 20 | 21 | i:{(`$first x;"J"$1_x)} each read0 `:input/12.txt 22 | 23 | sum abs first f/[(0 0;`E);i] 24 | /1148 25 | 26 | f:{[wp;a] 27 | w:first wp; 28 | p:last wp; 29 | if[a[0] in `N`E`S`W; 30 | :(w+a[1]*(`N`S`E`W!(0 1;0 -1;1 0;-1 0)) a[0];p) 31 | ]; 32 | if[a[0] = `F; 33 | :(p+d;p:p+a[1]*d:w - p) 34 | ]; 35 | if[a[0] in `L`R; 36 | r:"j"$a[1] % 90; 37 | d:w - p; 38 | :$[a[0]=`L; 39 | (p+{[x] (-1*x[1];x[0]) }/[r;d];p); 40 | (p+{[x] (x[1];-1*x[0]) }/[r;d];p) 41 | ] 42 | ]; 43 | }; 44 | 45 | sum abs last f/[(10 1;0 0);i] 46 | /52203 47 | -------------------------------------------------------------------------------- /2020/13.q: -------------------------------------------------------------------------------- 1 | //--- Day 13: Shuttle Search --- 2 | 3 | i:read0 `:input/13.txt 4 | t:"J"$first i 5 | r:"J"$"," vs last i 6 | 7 | (o w)*r w:first where o=m:min o:r-t mod r 8 | / 2382 9 | 10 | / convert negative modulos to positive 11 | a:neg[(til count r) w] mod r w:where not null r 12 | / calculate modulo for each 13 | b:{ prd each x except/:x } r w 14 | / iterate until each matches the original modulo 15 | s:sum b*{ (1+)/[{[x;y;z;m] not z=mod[m*x;y]}[x;y;z];1] }'[b;r w;a] 16 | / modulo with lcm 17 | s mod prd r w 18 | /906332393333683 19 | 20 | 21 | /bruteforce 22 | /{ {y + first x}[first x;]/[{sum 0^(y + til count x) mod x}[x;];0] } r 23 | -------------------------------------------------------------------------------- /2020/14.q: -------------------------------------------------------------------------------- 1 | //--- Day 14: Docking Data --- 2 | 3 | cf:{ asc sum each x where each neg[c]#'0b vs'til prd (c:count x)#2 } 4 | 5 | // yuck 6 | m:{[x] 7 | on::"1"=mask:last " = " vs x; 8 | off::not "0"=mask; 9 | flt::not "X"=mask; 10 | float::cf "j"$2 xexp where reverse "X"=mask; 11 | }; 12 | 13 | p:{ { "J"$(x inter .Q.n;y) } . " = " vs x } 14 | 15 | f:{ mem[first x]:2 sv on|off&-36#0b vs last x } 16 | g:{ $["mask"~4#x;m x;f p x] } 17 | 18 | mem:()!() 19 | g each i:read0 `:input/14.txt; 20 | sum mem 21 | /17934269678453 22 | 23 | mem:()!() 24 | f:{ mem[(2 sv flt&on|-36#0b vs first x)+/:float]:last x } 25 | 26 | g each i; 27 | sum mem 28 | /3440662844064 29 | -------------------------------------------------------------------------------- /2020/15.q: -------------------------------------------------------------------------------- 1 | //--- Day 15: Rambunctious Recitation --- 2 | 3 | f:{[x] 4 | x,$[0=sum e:x=last x; 5 | 0; 6 | (-). reverse 1+-2#where e 7 | ] 8 | }; 9 | 10 | i:"J"$","vs first read0 `:input/15.txt 11 | last f/[2020-count i;i] 12 | /257 13 | 14 | 15 | g:{[xy] 16 | d[i:0-(-/)0^-2#d first xy],:y:last xy; 17 | (i;y+1) 18 | }; 19 | 20 | d:(`u#i)!enlist each 1+til count i 21 | 22 | first g/[30000000-count i;(last i;1+count i)] 23 | /8546398 24 | -------------------------------------------------------------------------------- /2020/17.q: -------------------------------------------------------------------------------- 1 | //--- Day 17: Conway Cubes --- 2 | 3 | k:{ x,\:0 } (til count first i) cross til count i:"#"=read0 `:input/17.txt 4 | 5 | n:0 1 -1 cross (0 0;0 1;0 -1;1 0;-1 0;1 1;-1 -1;-1 1;1 -1) 6 | 7 | f:{ d!{ $[y z;in[;2 3];3=] sum y z+/:x }[x;y;] each d:`u#distinct raze key[y]+\:/:x } 8 | 9 | sum f[1 _ n]/[6;k!raze i] 10 | /348 11 | sum f[1 _ 0 1 -1 cross n]/[6;(k,\:0)!raze i] 12 | /2236 13 | -------------------------------------------------------------------------------- /2020/input/10.txt: -------------------------------------------------------------------------------- 1 | 104 2 | 83 3 | 142 4 | 123 5 | 87 6 | 48 7 | 102 8 | 159 9 | 122 10 | 69 11 | 127 12 | 151 13 | 147 14 | 64 15 | 152 16 | 90 17 | 117 18 | 132 19 | 63 20 | 109 21 | 27 22 | 47 23 | 7 24 | 52 25 | 59 26 | 11 27 | 161 28 | 12 29 | 148 30 | 155 31 | 129 32 | 10 33 | 135 34 | 17 35 | 153 36 | 96 37 | 3 38 | 93 39 | 82 40 | 55 41 | 34 42 | 65 43 | 89 44 | 126 45 | 19 46 | 72 47 | 20 48 | 38 49 | 103 50 | 146 51 | 14 52 | 105 53 | 53 54 | 77 55 | 120 56 | 39 57 | 46 58 | 24 59 | 139 60 | 95 61 | 140 62 | 33 63 | 21 64 | 84 65 | 56 66 | 1 67 | 32 68 | 31 69 | 28 70 | 4 71 | 73 72 | 128 73 | 49 74 | 18 75 | 62 76 | 81 77 | 66 78 | 121 79 | 54 80 | 160 81 | 158 82 | 138 83 | 94 84 | 43 85 | 2 86 | 114 87 | 111 88 | 110 89 | 78 90 | 13 91 | 99 92 | 108 93 | 141 94 | 40 95 | 25 96 | 154 97 | 26 98 | 35 99 | 88 100 | 76 101 | 145 102 | -------------------------------------------------------------------------------- /2020/input/13.txt: -------------------------------------------------------------------------------- 1 | 1000434 2 | 17,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,983,x,29,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19,x,x,x,23,x,x,x,x,x,x,x,397,x,x,x,x,x,37,x,x,x,x,x,x,13 3 | -------------------------------------------------------------------------------- /2020/input/15.txt: -------------------------------------------------------------------------------- 1 | 0,14,6,20,1,4 2 | -------------------------------------------------------------------------------- /2020/input/17.txt: -------------------------------------------------------------------------------- 1 | ######.# 2 | ##.###.# 3 | #.###.## 4 | ..#..### 5 | ##.#.#.# 6 | ##...##. 7 | #.#.##.# 8 | .###.### 9 | -------------------------------------------------------------------------------- /2021/01.q: -------------------------------------------------------------------------------- 1 | // --- Day 1: Sonar Sweep --- 2 | 3 | sw:{neg[x-1]_flip next\[x-1;y]} 4 | 5 | -1 + sum 0 < deltas i:"J"$read0 `:input/01.txt 6 | /1387 7 | 8 | -1 + sum 0 < deltas sum each sw[3;i] 9 | /1362 10 | -------------------------------------------------------------------------------- /2021/02.q: -------------------------------------------------------------------------------- 1 | // --- Day 2: Dive! --- 2 | 3 | i:flip "SJ"$flip 1#''" "vs'read0 `:input/02.txt 4 | 5 | f:{ $[`f ~ first y; x + (last[y];0); `d ~ first y; x + (0;last[y]); x - (0;last[y])] } 6 | / (h;d) 7 | prd f/[(0;0);i] 8 | /1484118 9 | 10 | / prd sum f:{ (`f`d`u!((y;0);(0;y);(0;0-y))) x } .'i 11 | 12 | 13 | f:{$[`f~first y; x + (last y;x[2]*last y;0); `d~first y; x + (0;0;last y); x - (0;0;last y)] } 14 | 15 | / (h;d;a) 16 | prd 2#f/[(0;0;0);i] 17 | /1463827010 18 | -------------------------------------------------------------------------------- /2021/03.q: -------------------------------------------------------------------------------- 1 | // --- Day 3: Binary Diagnostic --- 2 | 3 | prd 2 sv'(not;::)@\:"j"$avg i:-48+"j"$read0 `:input/03.txt 4 | /2250414 5 | 6 | f:{first {$[1=count x;x;x where x[;y] = z "j"$avg x[;y]]}[;;y]/[x;til count flip x]} 7 | prd 2 sv'f[i;] each (not;::) 8 | /6085575 9 | -------------------------------------------------------------------------------- /2021/04.q: -------------------------------------------------------------------------------- 1 | // --- Day 4: Giant Squid --- 2 | 3 | b:"J"$"," vs first i:read0 `:input/04.txt 4 | p:"J"$(3 cut)@''1_'6 cut 1 _ i 5 | 6 | f:{ 7 | { $[1=count x; 8 | x; 9 | any all flip c,flip c:last[x]|y=g:first x; 10 | y*sum raze g*not c; 11 | (g;c) 12 | ] 13 | }\[(x;5 5#0b);y] } 14 | 15 | last last r m?min m:sum each 0=type@''r:f[;b] each p 16 | /29440 17 | 18 | last last r m?max m 19 | /13884 20 | -------------------------------------------------------------------------------- /2021/05.q: -------------------------------------------------------------------------------- 1 | // --- Day 5: Hydrothermal Venture --- 2 | 3 | i:(2 cut) each raze each "J"$","vs''" -> "vs/:read0 `:input/05.txt 4 | 5 | rng:{ $[0d; reverse y + til 1 + neg d; x] } 6 | 7 | f:{[x;y;diag] 8 | $[((x 0) = y 0) or (x 1) = y 1; 9 | (,\:/:). rng ./:flip (x;y); 10 | diag; 11 | (,'). rng ./:flip (x;y); 12 | ()] 13 | } 14 | 15 | sum 1 6 and add 1 to 8s column 15 | 1 -> 0 16 | 2 -> 1 17 | 3 -> 2 18 | 4 -> 3 19 | 5 -> 4 20 | 6 -> 5 21 | 7 -> 6 22 | 8 -> 7 23 | 24 | f:{ @[x;where x < 7;mod;7],(sum -1=x:-1+x)#8 } 25 | -------------------------------------------------------------------------------- /2021/07.q: -------------------------------------------------------------------------------- 1 | // --- Day 7: The Treachery of Whales --- 2 | 3 | min sum abs i-\:i:"J"$"," vs first read0 `:input/07.txt 4 | /345197 5 | min sum { sum til 1 + x }@''abs i-\:min[i] + til 1 + max[i] - min i 6 | /96361606 7 | 8 | \ 9 | sum abs i - med i / part 1 10 | -------------------------------------------------------------------------------- /2021/08.q: -------------------------------------------------------------------------------- 1 | // --- Day 8: Seven Segment Search --- 2 | 3 | i:" | " vs/: read0 `:input/08.txt 4 | 5 | p1:" "vs'i[;1] 6 | p2:" "vs'i[;0] 7 | 8 | sum sum 2 3 4 7=\:raze count@''p1 9 | /284 10 | 11 | // via https://code.kx.com/phrases/math/#permutations 12 | c:("abcdefg"!) each {(1 0#x){raze(1 rotate)scan'x,'y}/x}"abcdefg" 13 | 14 | // segments 15 | s:("abcefg";"cf";"acdeg";"acdfg";"bcdf";"abdfg";"abdefg";"acf";"abcdefg";"abcdfg") 16 | 17 | res:{ last { $[x 0;x;all (asc each y x 1) in s;(1b;y);(0b;x 1)] }/[(0b;x);y] }[; c] each p2 18 | 19 | sum { 10 sv s?asc each y x }'[p1;res] 20 | /973499 21 | -------------------------------------------------------------------------------- /2021/09.q: -------------------------------------------------------------------------------- 1 | // --- Day 9: Smoke Basin --- 2 | 3 | c:raze til[count i],\:/:til count flip i:-48+"j"$read0 `:input/09.txt 4 | 5 | s:(0 1;1 0;0 -1;-1 0) 6 | sum 1 + i ./:lp:c where { all (i . x) < 0w^i ./:x+/:s } each c 7 | /535 8 | 9 | f:{[x] 10 | v:enlist 0N 0N; // initialise visited 11 | q:enlist x; // initialise queue 12 | while[count q; // while there is work to do... 13 | n:first q; // pop first 14 | q:1 _ q; // update queue 15 | if[not n in v; // check we've not been here before 16 | v,:enlist n; // add to visited 17 | o:n+/:s; // all options 18 | o:o where not o in v; // filter visited 19 | sc:i ./:o; // get surrounding cells 20 | q:q,o where (sc < 9) and sc > i . n // filter visited and add to queue 21 | ]; 22 | ]; 23 | :-1+count v; // return length of visited 24 | } 25 | 26 | prd -3#asc f peach lp 27 | /1122700 28 | -------------------------------------------------------------------------------- /2021/10.q: -------------------------------------------------------------------------------- 1 | // --- Day 10: Syntax Scoring --- 2 | 3 | d:")]}>([{<"!"([{<)]}>" 4 | 5 | f:{ 6 | $[not first x; // already errored? 7 | x; // return last result 8 | y in ")]}>"; // closing stream? 9 | $[(last last x)=d y; // as expected? 10 | (1b;-1 _ last x); // (good;drop last) 11 | (0b;y)]; // (bad;found) 12 | (1b;last[x],y) // (good;append new) 13 | ] 14 | } 15 | 16 | r:{ f/[(1b;"");x] } each read0 `:input/10.txt 17 | 18 | s:")]}>"!3 57 1197 25137 19 | 20 | sum s r[;1] where not r[;0] 21 | /319329 22 | 23 | s2:")]}>"!1 2 3 4 24 | 25 | "j"$med { { (s2 y)+5*x }/[0;reverse x] } each d r[;1] where r[;0] 26 | /3515583998 27 | -------------------------------------------------------------------------------- /2021/11.q: -------------------------------------------------------------------------------- 1 | // --- Day 11: Dumbo Octopus --- 2 | 3 | i:.Q.n?read0 `:input/11.txt 4 | 5 | s:(-1 0;-1 1;0 1;1 1;1 0;1 -1;0 -1;-1 -1) 6 | 7 | f:{ 8 | fo:raze { (til count x),/:'where each x } 9 -1) and so < count o; // filter to confines of grid 12 | o:{ .[x;y;+;1] }/[o;so]; // update octos 13 | (o;pf,fo) 14 | } 15 | 16 | g:{ 17 | octo:x 0; 18 | flash:x 1; 19 | step:1+x 2; 20 | // perform step 21 | octo:first f/[(octo+1;enlist 0N 0N)]; 22 | // anything above 9 is a flashed octo, if all flashed return 23 | if[(count raze octo)=nf:sum raze 9 < octo;:step]; 24 | // reset to zero 25 | octo:{ @[x;where 9 "vs/:2 _ i 8 | k:key[pt]!"j"$sum(`$sw[2;s])=\:key pt 9 | 10 | f:{ 11 | x:{ @[x;last y;+;first y] }/[x;flip (xw:x w;pt w:where 0 < x)]; 12 | x:{ @[x;last y;-;first y] }/[x;flip (xw;w)]; 13 | x 14 | } 15 | 16 | g:{ 17 | // pull out distinct keys 18 | k:distinct raze string key x; 19 | // sum up each pair 20 | d:k!{ div[;2] sum (sum each y=/:string key x) * value x }[x;] each k; 21 | // add 1 to last letter in original string 22 | d[last s]+:1; 23 | // difference of min/max 24 | (-). (max;min)@\:d 25 | } 26 | 27 | g f/[10;k] 28 | /2712 29 | g f/[40;k] 30 | /8336623059567 31 | -------------------------------------------------------------------------------- /2021/15.q: -------------------------------------------------------------------------------- 1 | // --- Day 15: Chiton --- 2 | 3 | i:.Q.n?read0 `:input/15.txt 4 | 5 | s:(0 1;1 0;0 -1;-1 0) 6 | 7 | f:{ 8 | nxt:y+/:s; 9 | nxt:nxt where all each (nxt > -1) and nxt < count x; 10 | g[;;z]/[x;nxt] 11 | } 12 | 13 | g:{ 14 | sur:y+/:s; 15 | sur:sur where all each (sur > -1) and sur < count x; 16 | $[.[x;y] > nc:.[z;y]+min .[x;] each sur; 17 | .[x;y;:;nc]; 18 | x 19 | ] 20 | } 21 | 22 | h:{ 23 | c:(2#count x)#0W; // costs 24 | c:.[c;0 0;:;0]; // seed initial 25 | 26 | r:f[;;x]/[c;t:til[count x] cross til count x]; 27 | r . last t 28 | } 29 | 30 | h i 31 | / 685 32 | 33 | a:{ raze { x#enlist y}[count x;] each { raze x#'y }[count x;] each 5#5#'next\[til 25] } i 34 | b:(5*count i)#{ (5*count x)#x } each i 35 | cc:{ @[x;where x > 9;{ 1 + x mod 10 }] } each a+b 36 | h cc 37 | / 2995 38 | -------------------------------------------------------------------------------- /2021/16.q: -------------------------------------------------------------------------------- 1 | // --- Day 16: Packet Decoder --- 2 | 3 | i:raze -4#'0b vs'"0123456789ABCDEF"?first read0`:input/16.txt 4 | 5 | pt:(sum/;prd/;min/;max/;::;>/;v 0;1 -1;0 ty) }[;max tx;min ty];(0 0;pv)]; 14 | a:any {[xy;tx;ty] (xy[0] within tx) and xy[1] within ty }[;tx;ty] each r[;0]; 15 | m:max r[;0;1]; // max y value 16 | (a;m) 17 | } 18 | 19 | r:g[;X;Y] peach t:til[1 + max X] cross min[Y] + til[1 + 2 * max abs Y] 20 | max r[;1] where r[;0] 21 | /3003 22 | sum r[;0] 23 | /940i 24 | -------------------------------------------------------------------------------- /2021/input/06.txt: -------------------------------------------------------------------------------- 1 | 1,1,3,1,3,2,1,3,1,1,3,1,1,2,1,3,1,1,3,5,1,1,1,3,1,2,1,1,1,1,4,4,1,2,1,2,1,1,1,5,3,2,1,5,2,5,3,3,2,2,5,4,1,1,4,4,1,1,1,1,1,1,5,1,2,4,3,2,2,2,2,1,4,1,1,5,1,3,4,4,1,1,3,3,5,5,3,1,3,3,3,1,4,2,2,1,3,4,1,4,3,3,2,3,1,1,1,5,3,1,4,2,2,3,1,3,1,2,3,3,1,4,2,2,4,1,3,1,1,1,1,1,2,1,3,3,1,2,1,1,3,4,1,1,1,1,5,1,1,5,1,1,1,4,1,5,3,1,1,3,2,1,1,3,1,1,1,5,4,3,3,5,1,3,4,3,3,1,4,4,1,2,1,1,2,1,1,1,2,1,1,1,1,1,5,1,1,2,1,5,2,1,1,2,3,2,3,1,3,1,1,1,5,1,1,2,1,1,1,1,3,4,5,3,1,4,1,1,4,1,4,1,1,1,4,5,1,1,1,4,1,3,2,2,1,1,2,3,1,4,3,5,1,5,1,1,4,5,5,1,1,3,3,1,1,1,1,5,5,3,3,2,4,1,1,1,1,1,5,1,1,2,5,5,4,2,4,4,1,1,3,3,1,5,1,1,1,1,1,1 2 | -------------------------------------------------------------------------------- /2021/input/11.txt: -------------------------------------------------------------------------------- 1 | 8448854321 2 | 4447645251 3 | 6542573645 4 | 4725275268 5 | 6442514153 6 | 4515734868 7 | 5513676158 8 | 3257376185 9 | 2172424467 10 | 6775163586 11 | -------------------------------------------------------------------------------- /2021/input/12.txt: -------------------------------------------------------------------------------- 1 | start-YY 2 | av-rz 3 | rz-VH 4 | fh-av 5 | end-fh 6 | sk-gp 7 | ae-av 8 | YY-gp 9 | end-VH 10 | CF-qz 11 | qz-end 12 | qz-VG 13 | start-gp 14 | VG-sk 15 | rz-YY 16 | VH-sk 17 | rz-gp 18 | VH-av 19 | VH-fh 20 | sk-rz 21 | YY-sk 22 | av-gp 23 | rz-qz 24 | VG-start 25 | sk-fh 26 | VG-av 27 | -------------------------------------------------------------------------------- /2021/input/14.txt: -------------------------------------------------------------------------------- 1 | SCVHKHVSHPVCNBKBPVHV 2 | 3 | SB -> B 4 | HH -> P 5 | VF -> N 6 | BS -> S 7 | NC -> C 8 | BF -> H 9 | BN -> H 10 | SP -> H 11 | BK -> H 12 | FF -> N 13 | VN -> B 14 | FN -> C 15 | FS -> S 16 | PP -> F 17 | ON -> H 18 | FV -> F 19 | KO -> F 20 | PK -> H 21 | VB -> S 22 | HS -> B 23 | NV -> O 24 | PN -> S 25 | VH -> B 26 | OS -> P 27 | BP -> H 28 | OV -> B 29 | HK -> S 30 | NN -> K 31 | SV -> C 32 | PB -> F 33 | SK -> F 34 | FB -> S 35 | NB -> K 36 | HF -> P 37 | FK -> K 38 | KV -> P 39 | PV -> F 40 | BC -> S 41 | FO -> N 42 | HC -> F 43 | CP -> B 44 | KK -> F 45 | PC -> S 46 | HN -> O 47 | SH -> H 48 | CK -> P 49 | CO -> F 50 | HP -> K 51 | PS -> C 52 | KP -> F 53 | OF -> K 54 | KS -> F 55 | NO -> V 56 | CB -> K 57 | NF -> N 58 | SF -> F 59 | SC -> P 60 | FC -> V 61 | BV -> B 62 | SS -> O 63 | KC -> K 64 | FH -> C 65 | OP -> C 66 | CF -> K 67 | VO -> V 68 | VK -> H 69 | KH -> O 70 | NP -> V 71 | NH -> O 72 | NS -> V 73 | BH -> C 74 | CH -> S 75 | CC -> F 76 | CS -> P 77 | SN -> F 78 | BO -> S 79 | NK -> S 80 | OO -> P 81 | VV -> F 82 | FP -> V 83 | OK -> C 84 | SO -> H 85 | KN -> P 86 | HO -> O 87 | PO -> H 88 | VS -> N 89 | PF -> N 90 | CV -> F 91 | BB -> H 92 | VC -> H 93 | HV -> B 94 | CN -> S 95 | OH -> K 96 | KF -> K 97 | HB -> S 98 | OC -> H 99 | KB -> P 100 | OB -> C 101 | VP -> C 102 | PH -> K 103 | -------------------------------------------------------------------------------- /2021/input/16.txt: -------------------------------------------------------------------------------- 1 | 220D700071F39F9C6BC92D4A6713C737B3E98783004AC0169B4B99F93CFC31AC4D8A4BB89E9D654D216B80131DC0050B20043E27C1F83240086C468A311CC0188DB0BA12B00719221D3F7AF776DC5DE635094A7D2370082795A52911791ECB7EDA9CFD634BDED14030047C01498EE203931BF7256189A593005E116802D34673999A3A805126EB2B5BEEBB823CB561E9F2165492CE00E6918C011926CA005465B0BB2D85D700B675DA72DD7E9DBE377D62B27698F0D4BAD100735276B4B93C0FF002FF359F3BCFF0DC802ACC002CE3546B92FCB7590C380210523E180233FD21D0040001098ED076108002110960D45F988EB14D9D9802F232A32E802F2FDBEBA7D3B3B7FB06320132B0037700043224C5D8F2000844558C704A6FEAA800D2CFE27B921CA872003A90C6214D62DA8AA9009CF600B8803B10E144741006A1C47F85D29DCF7C9C40132680213037284B3D488640A1008A314BC3D86D9AB6492637D331003E79300012F9BDE8560F1009B32B09EC7FC0151006A0EC6082A0008744287511CC0269810987789132AC600BD802C00087C1D88D05C001088BF1BE284D298005FB1366B353798689D8A84D5194C017D005647181A931895D588E7736C6A5008200F0B802909F97B35897CFCBD9AC4A26DD880259A0037E49861F4E4349A6005CFAD180333E95281338A930EA400824981CC8A2804523AA6F5B3691CF5425B05B3D9AF8DD400F9EDA1100789800D2CBD30E32F4C3ACF52F9FF64326009D802733197392438BF22C52D5AD2D8524034E800C8B202F604008602A6CC00940256C008A9601FF8400D100240062F50038400970034003CE600C70C00F600760C00B98C563FB37CE4BD1BFA769839802F400F8C9CA79429B96E0A93FAE4A5F32201428401A8F508A1B0002131723B43400043618C2089E40143CBA748B3CE01C893C8904F4E1B2D300527AB63DA0091253929E42A53929E420 2 | -------------------------------------------------------------------------------- /2021/input/17.txt: -------------------------------------------------------------------------------- 1 | target area: x=236..262, y=-78..-58 2 | -------------------------------------------------------------------------------- /2022/01.q: -------------------------------------------------------------------------------- 1 | // --- Day 1: Calorie Counting --- 2 | 3 | first d:desc sum each (where null i) cut i:0N,"J"$read0 `:input/01.txt 4 | /74394 5 | sum 3#d 6 | /212836 7 | -------------------------------------------------------------------------------- /2022/02.q: -------------------------------------------------------------------------------- 1 | // --- Day 2: Rock Paper Scissors --- 2 | 3 | S:`r`p`s!1 2 3 // choice score 4 | G:`l`d`w!0 3 6 // game score 5 | 6 | O:`A`B`C!`r`p`s // opponent 7 | E:`X`Y`Z!`r`p`s // elf strategy 8 | D:`X`Y`Z!`l`d`w // desired outcome 9 | 10 | B:`r`p`s!`s`r`p // beats 11 | L:value[B]!key[B] // loses to 12 | 13 | 14 | rps:{[x;y;z] 15 | o:O[x]; 16 | d:D[y]; 17 | e:$[z; 18 | $[d~`d; 19 | o; // force draw 20 | d~`l; 21 | B o; // force elf loss 22 | L o // force elf win 23 | ]; 24 | E[y] // part 1 25 | ]; 26 | 27 | r:$[e~o; 28 | `d; // draw 29 | e~B o; 30 | `l; // elf loss 31 | `w // elf win 32 | ]; 33 | :G[r] + S e; 34 | }; 35 | 36 | 37 | sum rps[;;0b].'i:`$" "vs'read0 `:input/02.txt 38 | /14264 39 | sum rps[;;1b].'i 40 | /12382 41 | -------------------------------------------------------------------------------- /2022/03.q: -------------------------------------------------------------------------------- 1 | // --- Day 3: Rucksack Reorganization --- 2 | 3 | sum 1+.Q.an?raze (distinct@(inter/)2 0N#) each r:read0 `:input/03.txt 4 | /7990 5 | sum 1+.Q.an?raze (distinct@inter/) each 3 cut r 6 | /2602 7 | -------------------------------------------------------------------------------- /2022/04.q: -------------------------------------------------------------------------------- 1 | // --- Day 4: Camp Cleanup --- 2 | 3 | range:{x + til 1 + y - x}; 4 | sum {$[all x in y;1b;all y in x]}.'p:{range .'"J"$"-"vs'","vs x} each read0`:input/04.txt 5 | /567 6 | sum {any x in y}.'p 7 | /907 8 | -------------------------------------------------------------------------------- /2022/05.q: -------------------------------------------------------------------------------- 1 | // --- Day 5: Supply Stacks --- 2 | 3 | w:first where 0 = count each r:read0 `:input/05.txt 4 | crates:(w-1)#r 5 | steps:(w+1)_r 6 | 7 | steps:"J"$(" "vs'steps)[;1 3 5] 8 | steps:0 -1 -1+/:steps 9 | 10 | crates:crates[;(1 + 4 * til ceiling (max count each crates) % 4)] 11 | crates:{x except " "} each flip crates 12 | 13 | f:{[f;x;y] 14 | cnt: y 0; 15 | frm: y 1; 16 | to: y 2; 17 | x: @[x;to;{y,x};f cnt#x[frm]]; 18 | x: @[x;frm;{y _ x};cnt]; 19 | :x; 20 | } 21 | 22 | -1@raze first each f[reverse]/[crates;steps]; 23 | / JRVNHHCSJ 24 | 25 | -1@raze first each f[(::)]/[crates;steps]; 26 | / GNFBSBJLH 27 | -------------------------------------------------------------------------------- /2022/06.q: -------------------------------------------------------------------------------- 1 | // --- Day 6: Tuning Trouble --- 2 | 3 | f:{y + count[x] - count (1_)/[{not y = count distinct y#x}[;y];x]} 4 | 5 | f[;4] i:first read0 `:input/06.txt 6 | / 1238 7 | f[i;14] 8 | / 3037 9 | -------------------------------------------------------------------------------- /2022/07.q: -------------------------------------------------------------------------------- 1 | // --- Day 7: No Space Left On Device --- 2 | 3 | lines:1 _ read0 `:input/07.txt 4 | 5 | dirs:enlist[1#`]!enlist 0 6 | cwd:` 7 | isls:0b 8 | 9 | while[count line:first lines; 10 | if[line like "$ cd*"; 11 | isls:0b; 12 | dir:`$last " " vs line; 13 | cwd:$[dir~`$".."; 14 | -1_ cwd; 15 | cwd,dir 16 | ]; 17 | ]; 18 | 19 | if[isls; 20 | if[not "dir"~first split:" " vs line; 21 | size: "J"$ first split; 22 | {dirs[enlist[x]]+:y}[;size] each ,\[cwd] // yuck 23 | ]; 24 | ]; 25 | 26 | if[line like "$ ls"; 27 | isls:1b; 28 | ]; 29 | 30 | lines: 1 _ lines; 31 | ]; 32 | 33 | 34 | sum (value dirs) where (value dirs) < 100000 35 | /1084134 36 | min dirs where dirs > 30000000 - 70000000 - dirs[enlist `] 37 | /6183184 38 | -------------------------------------------------------------------------------- /2022/08.q: -------------------------------------------------------------------------------- 1 | // --- Day 8: Treetop Tree House --- 2 | 3 | f: {(x > prev m) and x >= m:maxs x} 4 | g: {{(count 1 _ x) ^ 1 + first where first[x] <= 1 _ x} each (til count x)_\:x} 5 | 6 | w: { 7 | l:x y; 8 | d:flip x flip y; 9 | r:reverse each x reverse each y; 10 | u:flip reverse each x reverse each flip y; 11 | :(l;d;r;u); 12 | }; 13 | 14 | (sum/)(|/)w[f@';] i:"J"$string read0 `:input/08.txt 15 | / 1711 16 | (max/)(*/)w[g@';i] 17 | /301392 18 | -------------------------------------------------------------------------------- /2022/09.q: -------------------------------------------------------------------------------- 1 | // --- Day 9: Rope Bridge --- 2 | 3 | lut:"UDLR"!(1 0;-1 0;0 -1;0 1) 4 | 5 | i:raze { y#enlist lut x }.' flip ("CJ";" ")0: `:input/09.txt 6 | 7 | f:{[x;y] 8 | h:x 0; 9 | t:x 1; 10 | // move head in direction 11 | h+:y; 12 | // determine whether to move tail 13 | :$[(2 > sum d) or 1 1~d:abs h - t; 14 | (h;t); 15 | (h;t+1&-1|h - t) 16 | ]; 17 | } 18 | 19 | r:f\[(0 0;0 0);i] 20 | count distinct last flip r 21 | / 6269 22 | 23 | r2:{ f\[(0 0;0 0);deltas {x where differ x} last flip x] }/[8;r] 24 | count distinct last flip r2 25 | / 2557 26 | -------------------------------------------------------------------------------- /2022/10.q: -------------------------------------------------------------------------------- 1 | // --- Day 10: Cathode-Ray Tube --- 2 | 3 | i: raze {$[x like "addx*";(0;"J"$last " " vs x);0]} each read0 `:input/10.txt 4 | 5 | r:1+\i 6 | 7 | sum o*r -2+o:20 60 100 140 180 220 8 | /17180 9 | 10 | -1" #"40 cut (0^prev r) in'-1 0 1+/:mod[;40] til 240; 11 | // ### #### # # ### ### # # # ### 12 | // # # # # # # # # # # # # # # 13 | // # # ### #### # # # # # # # ### 14 | // ### # # # ### ### # # # # # 15 | // # # # # # # # # # # # # # 16 | // # # #### # # # # # #### ## ### 17 | -------------------------------------------------------------------------------- /2022/11.q: -------------------------------------------------------------------------------- 1 | // --- Day 11: Monkey in the Middle --- 2 | 3 | inp:{1 _ '(where 0 = count each x) cut x} (enlist ""),read0 `:input/11.txt 4 | 5 | p:{[x] 6 | items:"J"$"," vs 17 _ x[1]; 7 | op:value "{",(ssr[19 _ x[2];"old";"x"]),"}"; 8 | test:"J"$21 _ x[3]; 9 | true:"J"$29 _ x[4]; 10 | false:"J"$30 _ x[5]; 11 | :`items`op`test`true`false!(items;op;test;true;false); 12 | }; 13 | 14 | t:update inspected:0 from p each inp 15 | 16 | f:{[fn;tab;idx] 17 | m:tab idx; 18 | 19 | if[0=count m`items;:tab]; // fast exit 20 | 21 | tab[idx;`inspected]+:count m`items; 22 | tab[idx;`items]:0#0N; 23 | 24 | new:fn m[`op] m`items; 25 | eq:(new div m`test) = new % m`test; 26 | 27 | tab[m`true;`items],:new where eq; 28 | tab[m`false;`items],:new where not eq; 29 | :tab; 30 | } 31 | 32 | t2:{f[x;;]/[y;] til count y}[{x div 3}]/[20;t] 33 | prd 2#desc exec inspected from t2 34 | /117640 35 | 36 | modulo:prd exec test from t; 37 | t3:{f[x;;]/[y;] til count y}[{x mod y}[;modulo]]/[10000;t] 38 | prd 2#desc exec inspected from t3 39 | /30616425600 40 | -------------------------------------------------------------------------------- /2022/12.q: -------------------------------------------------------------------------------- 1 | // --- Day 12: Hill Climbing Algorithm --- 2 | 3 | i:read0 `:input/12.txt 4 | 5 | coords:{raze (til count x),/:'where each x=y} 6 | 7 | start:first coords[i;"S"] 8 | end:first coords[i;"E"] 9 | 10 | i:.[i;start;:;"a"] 11 | i:.[i;end;:;"z"] 12 | 13 | m:.Q.a?i 14 | 15 | f:{[best;pos] 16 | queue:enlist (pos;0); 17 | visited:`u#enlist pos; // 'global' visited 18 | 19 | while[count queue; 20 | pop:first queue; 21 | current:first pop; 22 | steps:last pop; 23 | queue:1 _ queue; 24 | 25 | if[steps>=best;:best]; // fast fail 26 | 27 | if[current~end;:min steps,best]; // success 28 | 29 | move:(1 0;-1 0;0 -1;0 1)+\:current; 30 | move:move where not move in\:visited; 31 | move:move where 2 > ((0W^.[m;] each move) - .[m;current]); 32 | 33 | visited,:move; // dont revisit (new) locations 34 | queue,:(;steps+1) each move; 35 | ]; 36 | :best; // slow fail 37 | } 38 | 39 | r:f[0W;start];r 40 | / 456 41 | f/[r;] coords[i;"a"] 42 | / 454 43 | -------------------------------------------------------------------------------- /2022/13.q: -------------------------------------------------------------------------------- 1 | // --- Day 13: Distress Signal --- 2 | 3 | ii:.j.k@''{ 1_'(where 0 = count each x) cut x } enlist[""],i:read0 `:input/13.txt 4 | 5 | f:{[left;right] 6 | if[(-9h ~ type left) and (-9h ~ type right); 7 | if[left=right; 8 | :0N 9 | ]; 10 | :0 1 @ left < right 11 | ]; 12 | if[(type[left] in 0 9h) and (-9h ~ type right); 13 | :f[left;enlist right]; 14 | ]; 15 | if[(-9h ~ type left) and (type[right] in 0 9h); 16 | :f[enlist left;right]; 17 | ]; 18 | if[(type[left] in 0 9h) and (type[right] in 0 9h); 19 | if[(0 = count left) and (0 = count right); 20 | :0N; 21 | ]; 22 | if[(0 = count left); 23 | :1; 24 | ]; 25 | if[(0 = count right); 26 | :0; 27 | ]; 28 | r:f[first left;first right]; 29 | :$[r~0N; 30 | f[1 _ left;1 _ right]; 31 | r 32 | ]; 33 | ]; 34 | 35 | '"fail"; 36 | }; 37 | 38 | sum 1 + where f .'ii 39 | / 6235 40 | 41 | pairs:.j.k each {x where not 0 = count each x} i 42 | prd 0 1 + {1 + sum 0 = f[x;] each y}[;pairs] each (enlist enlist 2f;enlist enlist 6f) 43 | / 22866 44 | -------------------------------------------------------------------------------- /2022/14.q: -------------------------------------------------------------------------------- 1 | // --- Day 14: Regolith Reservoir --- 2 | 3 | i:raze {(-1+count c)#2#'next\[c:" -> " vs x]} each read0 `:input/14.txt 4 | 5 | range:{x + til 1 + y - x} 6 | 7 | f:{ 8 | xx:reverse each "J"$"," vs'x; // flip x/y 9 | l:first xx; 10 | r:last xx; 11 | :$[l[0]=r[0]; 12 | l[0],/:range . asc (l 1;r 1); 13 | (range . asc (l 0;r 0)),\:r[1] 14 | ]; 15 | } 16 | 17 | ii:asc distinct raze f each i 18 | 19 | maxy: max ii[;0] 20 | 21 | // add flooring 22 | ii,:(maxy+2),/:250 + til 500 // should base this on 2*maxy? 23 | 24 | cave:{[x;y] 25 | x[y]:"#"; 26 | :x; 27 | }/[(`u#enlist[0N 0N])!enlist"X";ii] 28 | 29 | pf:{[pt2;x] 30 | if[1 = count x;:x]; // break iteration 31 | 32 | $[pt2; 33 | if[cave[0 500]="o";:0b]; // reached the top (part2) 34 | if[maxy = x 0;:0b] // reached the floor (part1) 35 | ]; 36 | 37 | $[(cave 1 0+x) = " "; // empty space? 38 | :1 0+x; // keep going down 39 | (cave 1 -1+x) = " "; // empty space on diagonal left? 40 | :1 -1+x; // go diagonal left 41 | (cave 1 1+x) = " "; // empty space on diagonal right? 42 | :1 1+x; // go diagonal right 43 | [cave[x]:"o";:1b] // place some sand and we're done 44 | ]; 45 | }; 46 | 47 | {pf[0b]/[0 500]}/[{x};1]; 48 | sum"o"=value cave 49 | /719 50 | 51 | {pf[1b]/[0 500]}/[{x};1]; 52 | sum"o"=value cave 53 | /23390 54 | -------------------------------------------------------------------------------- /2022/input/11.txt: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 63, 84, 80, 83, 84, 53, 88, 72 3 | Operation: new = old * 11 4 | Test: divisible by 13 5 | If true: throw to monkey 4 6 | If false: throw to monkey 7 7 | 8 | Monkey 1: 9 | Starting items: 67, 56, 92, 88, 84 10 | Operation: new = old + 4 11 | Test: divisible by 11 12 | If true: throw to monkey 5 13 | If false: throw to monkey 3 14 | 15 | Monkey 2: 16 | Starting items: 52 17 | Operation: new = old * old 18 | Test: divisible by 2 19 | If true: throw to monkey 3 20 | If false: throw to monkey 1 21 | 22 | Monkey 3: 23 | Starting items: 59, 53, 60, 92, 69, 72 24 | Operation: new = old + 2 25 | Test: divisible by 5 26 | If true: throw to monkey 5 27 | If false: throw to monkey 6 28 | 29 | Monkey 4: 30 | Starting items: 61, 52, 55, 61 31 | Operation: new = old + 3 32 | Test: divisible by 7 33 | If true: throw to monkey 7 34 | If false: throw to monkey 2 35 | 36 | Monkey 5: 37 | Starting items: 79, 53 38 | Operation: new = old + 1 39 | Test: divisible by 3 40 | If true: throw to monkey 0 41 | If false: throw to monkey 6 42 | 43 | Monkey 6: 44 | Starting items: 59, 86, 67, 95, 92, 77, 91 45 | Operation: new = old + 5 46 | Test: divisible by 19 47 | If true: throw to monkey 4 48 | If false: throw to monkey 0 49 | 50 | Monkey 7: 51 | Starting items: 58, 83, 89 52 | Operation: new = old * 19 53 | Test: divisible by 17 54 | If true: throw to monkey 2 55 | If false: throw to monkey 1 56 | -------------------------------------------------------------------------------- /2023/01.q: -------------------------------------------------------------------------------- 1 | // --- Day 1: Trebuchet?! --- 2 | 3 | f:{ "J"$(first;last)@\:x inter .Q.n } 4 | 5 | n:string `one`two`three`four`five`six`seven`eight`nine 6 | 7 | g:{ { @[x;last y;:;first string 1 + first y] }/[x;raze w,/:'s w:where 0 < count each s:ss[x;] each n] } 8 | 9 | sum f each i:read0 `:input/01.txt 10 | / 54953 11 | 12 | sum f each g each i 13 | / 53868 14 | -------------------------------------------------------------------------------- /2023/02.q: -------------------------------------------------------------------------------- 1 | // --- Day 2: Cube Conundrum --- 2 | 3 | sum 1 + where { all { all 12 13 14 >= 0 ^ `red`green`blue#(!). flip x } each { { raze "SJ"$reverse " " vs x } each trim "," vs x } each ";" vs last ":"vs x } each read0 `:input/02.txt 4 | / 2810 5 | 6 | sum { prd value exec max red, max green, max blue from { 0 ^ `red`green`blue#(!). flip x } each { { raze "SJ"$reverse " " vs x } each trim "," vs x } each ";" vs last ":"vs x } each read0 `:input/02.txt 7 | / 69110 8 | -------------------------------------------------------------------------------- /2023/03.q: -------------------------------------------------------------------------------- 1 | // --- Day 3: Gear Ratios --- 2 | 3 | i:read0 `:input/03.txt 4 | 5 | f:{[grid;x;pos] 6 | xx:last x; 7 | gear:xx[0]; 8 | symb:xx[1]; 9 | cood:xx[2]; 10 | 11 | // new gear or still in current gear 12 | c:grid . pos; 13 | if[c in .Q.n; 14 | gear,:c; 15 | s:grid ./:p:pos+/:(1 0; 1 1; 0 1; -1 1; -1 0; -1 -1; 0 -1; 1 -1); 16 | symb,:s except .Q.n," ."; 17 | cood:distinct cood,p where "*"=s; 18 | :(-1 _ x),enlist (gear;symb;cood) 19 | ]; 20 | 21 | // finished current gear, add new empty entry 22 | if[count gear; 23 | :x, enlist () 24 | ]; 25 | 26 | // no current gear, just continue 27 | :x; 28 | }[i;;] 29 | 30 | t: flip `gear`val`typ!"J**"$flip -1 _ f/[(); ] { til[x] cross til x } count i 31 | exec sum gear from t where 0 < count each val 32 | /519444 33 | 34 | sum exec prd gear by typ from t where 2 = (count;typ) fby typ 35 | /74528807 36 | -------------------------------------------------------------------------------- /2023/04.q: -------------------------------------------------------------------------------- 1 | // --- Day 4: Scratchcards --- 2 | 3 | i:{ "J"${ 3 cut x } each " | " vs 1_ last ":" vs x } each read0 `:input/04.txt 4 | 5 | sum { floor 2 xexp -1 + count y inter x }.'i 6 | / 21919 7 | 8 | sum {[x;y] idx:first y; x[idx + 1 + til {count y inter x }. last y] +: x[idx];x }/[count[i]#1;flip (til count i;i)] 9 | / 9881048 10 | -------------------------------------------------------------------------------- /2023/06.q: -------------------------------------------------------------------------------- 1 | // --- Day 6: Wait For It --- 2 | 3 | f:{ "j"$sum y < { y * x - y }[x;til 1 + x] } 4 | 5 | prd f .' flip "J"$ i:{ " " vs trim ssr[;" ";" "]/[x] } each i:9 _'read0 `:input/06.txt 6 | / 2269432 7 | 8 | f . "J"$raze each i 9 | / 35865985 10 | -------------------------------------------------------------------------------- /2023/08.q: -------------------------------------------------------------------------------- 1 | // --- Day 8: Haunted Wasteland --- 2 | 3 | / helpers from 2019.12 4 | gcd_helper:{$[0=m:mod[x;y];y;.z.s[y;m]]}; 5 | gcd:{$[any 0=(x;y);x|y;gcd_helper[x|y;x&y]]}; 6 | lcm:{div[x*y;gcd[x;y]]}; 7 | 8 | i: "R"=first r:read0 `:input/04.txt 9 | network: { x!flip (y;z) } . ("s ss"; " ") 0:{ x inter .Q.A," " } each 2 _ r 10 | 11 | f:{[x] 12 | :$["Z" = last string r:network[x[1]] first x[2]; 13 | :x; 14 | :(x[0] + 1 ; r ; 1 _ x[2]); 15 | ]; 16 | }; 17 | g:{ first f/[(1; x; 25000#i)] } 18 | 19 | g `AAA 20 | / 14257 21 | 22 | res:g peach k where "Z"=(string k:key network)[;2] 23 | lcm/[res] 24 | / 16187743689077 25 | -------------------------------------------------------------------------------- /2023/09.q: -------------------------------------------------------------------------------- 1 | // --- Day 9: Mirage Maintenance --- 2 | 3 | i:{ "J"$" " vs x } each read0 `:input/09.txt 4 | 5 | sum { sum last each x } each r:{ 1 _ deltas x }\[{ 1 < count distinct x };] each i 6 | / 1980437560 7 | 8 | sum { last { first[y] - x }\[0; reverse x] } each r 9 | /977 10 | -------------------------------------------------------------------------------- /2023/15.q: -------------------------------------------------------------------------------- 1 | // --- Day 15: Lens Library --- 2 | 3 | hash:{[x;y] (17 * (x + "i"$y)) mod 256 } 4 | 5 | sum { hash/[0;x] } each "," vs first read0 `:input/15.txt 6 | /510273 7 | -------------------------------------------------------------------------------- /2023/input/06.txt: -------------------------------------------------------------------------------- 1 | Time: 49 78 79 80 2 | Distance: 298 1185 1066 1181 3 | -------------------------------------------------------------------------------- /2024/01.q: -------------------------------------------------------------------------------- 1 | // --- Day 1: Historian Hysteria --- 2 | 3 | sum abs (-). asc each i:flip "J"$" "vs/:read0 `:input/01.txt 4 | /1320851 5 | 6 | sum { y inter x } . i 7 | /26859182 8 | -------------------------------------------------------------------------------- /2024/02.q: -------------------------------------------------------------------------------- 1 | // --- Day 2: Red-Nosed Reports --- 2 | 3 | safe:{ 4 | :$[(x~desc x) | (x~asc x); 5 | all within[; 1 3] 1 _ abs deltas x; 6 | 0b 7 | ]; 8 | }; 9 | 10 | sum safe each i:"J"$" "vs/:read0 `:input/02.txt 11 | / 510 12 | 13 | sum { any safe each x _/: til count x } each i 14 | / 553 15 | 16 | 17 | -------------------------------------------------------------------------------- /2024/03.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | with open("input/03.txt") as f: 4 | lines = f.readlines() 5 | 6 | total = 0 7 | for line in lines: 8 | match = re.findall(r"mul\(([0-9]+),([0-9]+)\)", line) 9 | for x, y in match: 10 | total += int(x) * int(y) 11 | 12 | print(total) 13 | # 161289189 14 | 15 | total = 0 16 | enabled = True 17 | for line in lines: 18 | match = re.findall(r"mul\(([0-9]+),([0-9]+)\)|(do)\(\)|(don't)\(\)", line) 19 | for x, y, do, dont in match: 20 | if do: 21 | enabled = True 22 | continue 23 | if dont: 24 | enabled = False 25 | continue 26 | if enabled: 27 | total += int(x) * int(y) 28 | 29 | print(total) 30 | # 83595109 31 | -------------------------------------------------------------------------------- /2024/03.q: -------------------------------------------------------------------------------- 1 | // --- Day 3: Mull It Over --- 2 | 3 | i: raze read0 `:input/03.txt 4 | 5 | mul:( 6 | ("mul([0-9],[0-9])";8); 7 | ("mul([0-9],[0-9][0-9])";9); 8 | ("mul([0-9],[0-9][0-9][0-9])";10); 9 | ("mul([0-9][0-9],[0-9])";9); 10 | ("mul([0-9][0-9],[0-9][0-9])";10); 11 | ("mul([0-9][0-9],[0-9][0-9][0-9])";11); 12 | ("mul([0-9][0-9][0-9],[0-9])";10); 13 | ("mul([0-9][0-9][0-9],[0-9][0-9])";11); 14 | ("mul([0-9][0-9][0-9],[0-9][0-9][0-9])";12) 15 | ); 16 | 17 | f:{ $[count s:ss[x;first y];(s,\:last y);()] } 18 | 19 | muls:raze f[i;] each mul 20 | 21 | g:{prd "J"$"," vs inter[;.Q.n,","] x y + til z} 22 | 23 | sum g[i;].'muls 24 | /161289189 25 | 26 | dos:ss[i;"do()"],\:1b 27 | donts:ss[i;"don't()"],\:0b 28 | 29 | first { 30 | $[-1h~type last z; 31 | (first[y] ;last z); 32 | (first[y] + last[y] * g[x;]. z;last y) 33 | ] 34 | }[i;;]/[(0;1b);] { x iasc x[;0] } muls,dos,donts 35 | / 83595109 36 | -------------------------------------------------------------------------------- /2024/04.q: -------------------------------------------------------------------------------- 1 | // --- Day 4: Ceres Search --- 2 | 3 | i:read0 `:input/04.txt 4 | 5 | d:{ .[x;] @'' { ((til count x),'til count x) +\:y }[x;] each (0,/:til count x),1_(til count x),\:0 } 6 | 7 | count (raze/) ss[;"XMAS"]@''(i; flip i; reverse each i; reverse each flip i; d i; d reverse i; d reverse each i; d reverse reverse each i) 8 | / 2575 9 | 10 | f:{ 11 | nw:.[x;] each y +/:(-1 -1;0 0; 1 1); 12 | ne:.[x;] each y +/:(-1 1;0 0; 1 -1); 13 | :(nw in ("MAS";"SAM")) and ne in ("MAS";"SAM"); 14 | } 15 | 16 | sum f[i;] each t cross t:til count i 17 | / 2041 18 | -------------------------------------------------------------------------------- /2024/05.q: -------------------------------------------------------------------------------- 1 | // --- Day 5: Print Queue --- 2 | 3 | i:(0,where 0 = count each i) cut i:read0 `:input/05.txt 4 | 5 | i0:"J"$("|"vs) each i 0 6 | i1:"J"$(","vs) each 1 _ i 1 7 | 8 | f:{ all { $[all y in x; a~asc a:raze where each y=\:x; 1b] }[y; ] each x } 9 | 10 | res:f[i0;] each i1 11 | 12 | sum { x count[x] div 2 } each i1 where res 13 | / 5651 14 | 15 | d:asc each group (!). flip reverse each i0; 16 | 17 | g:{ 18 | $[()~m:min raze where each x=/:d[y]; 19 | x; 20 | m >= w:first where x=y; 21 | x; 22 | [ 23 | // swap places 24 | t:x[m]; 25 | x[m]:y; 26 | x[w]:t; 27 | x 28 | ] 29 | ] 30 | } 31 | 32 | sum { x count[x] div 2 } each { g/[x;x] }/[20;] each i1 where not res 33 | / 4743 34 | -------------------------------------------------------------------------------- /2024/07.q: -------------------------------------------------------------------------------- 1 | // --- Day 7: Bridge Repair --- 2 | 3 | i:{("J"$" "vs y;"J"$x) }.' ": "vs/:read0 `:input/07.txt 4 | 5 | c:{x@/:neg[y]#'(16#0),/:(count x) vs/:til "j"$count[x] xexp y } 6 | 7 | C:{t!c[x;] each t:til y} 8 | 9 | / build combo dicts 10 | c2:C[(+;*);] m:max count each i[;0] 11 | c3:C[(+;*;{"J"$string[x],string[y]}); m] 12 | 13 | 14 | f:{z in {{(last y). (x;first y)}/[x]} peach {(1#x),(1_ x),'enlist each y}[y;] peach x -1+count y} 15 | 16 | sum raze i[;1] where p1:f[c2;] .'i 17 | / 12839601725877 18 | 19 | sum raze i[;1] (where p1),w where p2:f[c3;] .'i w:where not p1 20 | / 149956401519484 21 | -------------------------------------------------------------------------------- /2024/08.q: -------------------------------------------------------------------------------- 1 | // --- Day 8: Resonant Collinearity --- 2 | 3 | // from https://code.kx.com/phrases/math/#combinations 4 | comb:{[n;k] $[k=n;enlist til k; k=1;enlist each til n; .z.s[n-1;k],.z.s[n-1;k-1],\:enlist n-1] } 5 | 6 | i:read0 `:input/08.txt 7 | 8 | // identify antennas 9 | a:{x[;1] group x[;0] } raze { $["."=v:.[i;x];();enlist (v;enlist x)]} each t cross t:til count i 10 | 11 | // antenna pair combos 12 | c:{x@/:comb[count x;2]} 13 | 14 | // antinodes either side of antenna 15 | f:{x+(::;neg)@\:(-) . x} 16 | 17 | g:{count distinct {x where all each (x >= 0) and (x < count i)} raze each raze { raze x each c y }[x;] peach a } 18 | g f 19 | / 357 20 | 21 | // antinodes along a line up to 50x distance between antenna 22 | f2:{ distinct raze x+\:/:raze (1 _ til count i)*\:(::;neg)@\:(-) . x} 23 | g f2 24 | / 1266 25 | -------------------------------------------------------------------------------- /2024/09.q: -------------------------------------------------------------------------------- 1 | // --- Day 9: Disk Fragmenter --- 2 | 3 | i:0N 2#first read0 `:input/09.txt 4 | 5 | u:-48+"j"$ i[;0] / used 6 | s:-48+"j"$"0"^i[;1] / space 7 | 8 | d:raze { (x#z),(y#0N) } .' (u,'s),'til count u 9 | 10 | d[where null d]: (sum null d)#r where not null r:reverse d 11 | sum (til sum u) * sum[u]#d 12 | / 6201130364722 13 | 14 | 15 | f:{ 16 | s:sum each null x { x where mod[x;2] x } til count x; 17 | /s:sum each null (0N 2#x)[;1]; 18 | 19 | if[0N~w:first where s >= c:count x[y*2]; 20 | / no space large enough 21 | :x 22 | ]; 23 | 24 | if[w >= y; 25 | / avoid moving used to the RIGHT! 26 | :x 27 | ]; 28 | 29 | / copy used into space 30 | t: x[1+2*w]; 31 | x[1+2*w]: @[t;(first where null t) + til c;:;y]; 32 | 33 | / clear the used and return 34 | :@[x;2*y;:;c#0N]; 35 | } 36 | 37 | / reset 38 | d:raze { enlist[(x#z)],enlist[(y#0N)] } .' (u,'s),'til count u 39 | 40 | sum {x*til count x} raze f/[d;reverse 1 _ til count u] 41 | / 6221662795602 42 | -------------------------------------------------------------------------------- /2024/11.q: -------------------------------------------------------------------------------- 1 | // --- Day 11: Plutonian Pebbles --- 2 | 3 | i:"J"$" " vs first read0 `:input/11.txt 4 | 5 | f:{ 6 | $[x=0; 7 | enlist 1; 8 | 0=mod[;2] count s:string x; 9 | "J"$2 0N#s; 10 | enlist 2024*x 11 | ] 12 | } 13 | 14 | r:{ {flip (key x; value x)} sum each x[;1] group x[;0] } 15 | g:{ r raze { (f first x),\:last x } peach x } 16 | 17 | { sum x[;1] } g/[25;] i,'(count i)#1 18 | / 203457 19 | 20 | { sum x[;1] } g/[75;] i,'(count i)#1 21 | / 241394363462435 22 | -------------------------------------------------------------------------------- /2024/input/11.txt: -------------------------------------------------------------------------------- 1 | 1 24596 0 740994 60 803 8918 9405859 2 | -------------------------------------------------------------------------------- /c/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc aoc.c -shared -lcrypto -fPIC -o aoc.so -std=c99 -O3 3 | -------------------------------------------------------------------------------- /c/README.md: -------------------------------------------------------------------------------- 1 | # src 2 | 3 | Debian: 4 | 5 | ``` 6 | # apt-get install make gcc libssl-dev 7 | # make 8 | ``` 9 | 10 | Centos: 11 | 12 | ``` 13 | # yum install make gcc openssl-devel 14 | # make 15 | -------------------------------------------------------------------------------- /c/aoc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include "k.h" 8 | 9 | // gcc aoc.c -shared -lcrypto -fPIC -o aoc.so -std=c99 10 | 11 | // md5C:*"../src/aoc.so"2:`md5!1 12 | // md5:{[x] `c md5C x} 13 | K md5(const S x) 14 | { 15 | unsigned char digest[16]; 16 | MD5((const unsigned char*)x, strlen(x), digest); 17 | K d = k((S)'I', 16); 18 | for (U i = 0; i < 16; i++) 19 | { 20 | ((I*)d)[i] = digest[i]; 21 | } 22 | return d; 23 | } 24 | 25 | //ssC:*"../src/aoc.so"2:`ss!2 26 | //ss:{[x;y] (),ssC[x,0x0;y,0x0]} 27 | K ss(const S x, const S y) 28 | { 29 | I lx = strlen(x); 30 | I ly = strlen(y); 31 | 32 | if (lx == 0 || ly == 0) 33 | { 34 | return k((S)'I', 0); 35 | } 36 | 37 | const char *ptr = x; 38 | const char *p_idx = ptr; 39 | 40 | I *tmp = (I*) malloc((lx / ly) * sizeof(I)); 41 | I fnd = 0; 42 | 43 | while ((ptr = strstr(ptr, y)) != NULL) 44 | { 45 | ((I*)tmp)[fnd++] = ptr - p_idx; 46 | ptr = ptr + ly * sizeof(char); 47 | } 48 | K r = k((S)'I', fnd); 49 | for (int i = 0; i < fnd; i++) 50 | { 51 | ((I*)r)[i] = tmp[i]; 52 | } 53 | free(tmp); 54 | 55 | return r; 56 | } 57 | -------------------------------------------------------------------------------- /c/k.h: -------------------------------------------------------------------------------- 1 | typedef int I;typedef long J;typedef float E;typedef double F;typedef const char*S; 2 | 3 | typedef unsigned long K,U; 4 | 5 | #define R return 6 | #define P(b,a...) if(b)R({a;}); 7 | #define N(n,a...) {I _n=(n),i=-1;while(++i>56) 14 | #define xs (S)(-1UL>>16&(U)x) 15 | #define xj *(J*)xs 16 | #define xf *(F*)xs 17 | 18 | #define xn ((I*)x)[-2] 19 | #define Xx ((K*)x)[i] 20 | 21 | #define KI 1*8+5 22 | #define KJ 1*8+6 23 | #define KE 5*8+5 24 | #define KF 5*8+6 25 | #define KS 7*8 26 | -------------------------------------------------------------------------------- /rs/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /rs/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "aoc" 5 | version = "0.1.0" 6 | dependencies = [ 7 | "md5", 8 | ] 9 | 10 | [[package]] 11 | name = "md5" 12 | version = "0.7.0" 13 | source = "registry+https://github.com/rust-lang/crates.io-index" 14 | checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" 15 | -------------------------------------------------------------------------------- /rs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "aoc" 3 | version = "0.1.0" 4 | authors = ["Mark Street "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | md5 = "0.7.0" 9 | 10 | [lib] 11 | name = "aoc" 12 | path = "src/aoc.rs" 13 | crate-type = ["dylib"] 14 | -------------------------------------------------------------------------------- /rs/README.md: -------------------------------------------------------------------------------- 1 | # Rust 2 | 3 | `ss` and `md5` functions implemented in Rust. 4 | 5 | ## Build 6 | 7 | ```sh 8 | docker run --rm -ti -v $(pwd):/app rust sh -c "cd /app && cargo build --release" 9 | ``` 10 | -------------------------------------------------------------------------------- /rs/src/aoc.rs: -------------------------------------------------------------------------------- 1 | use std::ffi::CStr; 2 | use std::os::raw::c_char; 3 | use std::ptr; 4 | 5 | extern "C" { 6 | fn k(t: c_char, l: u64) -> *mut u32; 7 | } 8 | 9 | // md5C:*"../rs/target/libaoc.so"2:`md5!1 10 | // md5:{[x] `c md5C x} 11 | #[no_mangle] 12 | pub extern "C" fn md5(s: *const c_char) -> *mut u32 { 13 | let p; 14 | unsafe { 15 | let digest = md5::compute(CStr::from_ptr(s).to_bytes()); 16 | p = k('I' as c_char, 16); 17 | for i in 0..16 { 18 | ptr::write(p.add(i), digest[i].into()) 19 | } 20 | } 21 | return p 22 | } 23 | 24 | // ssC:*"../rs/target/release/libaoc.so"2:`ss!2 25 | // ss:{[x;y] (),ssC[x,0x0;y,0x0]} 26 | #[no_mangle] 27 | pub extern "C" fn ss(h: *const c_char, n: *const c_char) -> *mut u32 { 28 | let p; 29 | unsafe { 30 | let hs = CStr::from_ptr(h).to_str().expect("fail"); 31 | let ns = CStr::from_ptr(n).to_str().expect("fail"); 32 | let v: Vec<_> = hs.match_indices(ns).collect(); 33 | p = k('I' as c_char, v.len() as u64); 34 | for i in 0..v.len() { 35 | ptr::write(p.add(i), v[i].0 as u32) 36 | } 37 | } 38 | return p 39 | } 40 | --------------------------------------------------------------------------------