Skip to content

Instantly share code, notes, and snippets.

Avatar

Mark Woodworth Solaxun

View GitHub Profile
View day21.py
def mod1(num,m): return num % m or m
cache = {}
def play_round(player,pos1,score1,pos2,score2):
if (pos1,score1,pos2,score2) in cache:
return cache[(pos1,score1,pos2,score2)]
# swap 0/1 below to get the other player wins
if score1 >= 21:
return 1
if score2 >= 21:
@Solaxun
Solaxun / day1.clj
Created Dec 4, 2021
AoC 2021: Clojure Day 1
View day1.clj
(ns aoc-clj-2021.day1
(:require [clojure.string :as str]
[clojure.set :as set]
[clojure.java.io :as io]))
(def data
(->> (io/resource "day1.txt")
slurp
str/split-lines
(map parse-long)))
@Solaxun
Solaxun / day2.clj
Created Dec 4, 2021
AoC 2021: Clojure Day 2
View day2.clj
(ns aoc-clj-2021.day2
(:require [clojure.string :as str]
[clojure.set :as set]
[clojure.java.io :as io]))
(def data
(->> (io/resource "day2.txt")
slurp
str/split-lines
(map #(str/split % #" "))
@Solaxun
Solaxun / day3.clj
Created Dec 4, 2021
AoC 2021: Clojure Day 3
View day3.clj
(ns aoc-clj-2021.day3
(:require [clojure.string :as str]
[clojure.set :as set]
[clojure.java.io :as io]))
(def data
(->> (io/resource "day3.txt")
slurp
str/split-lines))
@Solaxun
Solaxun / day4.clj
Created Dec 4, 2021
AoC 2021: Clojure Day 4
View day4.clj
(ns aoc-clj-2021.day4
(:require [clojure.string :as str]
[clojure.set :as set]
[clojure.java.io :as io]))
(def data
(->> (io/resource "day4.txt")
slurp
str/split-lines
(remove #(= "" %))))
@Solaxun
Solaxun / day4.py
Created Dec 4, 2021
AoC 2021: Python Day 4
View day4.py
import re
data = open('day4.txt').read().split('\n\n')
hand, *boards = data
boards = " ".join(boards)
hands = [int(n) for n in hand.split(',')]
nums = [int(n) for n in re.findall("(\d+)",boards)]
fives = [nums[i:i+5] for i in range(0,len(nums),5)]
boards = [fives[i:i+5] for i in range(0,len(fives),5)]
@Solaxun
Solaxun / day3.py
Created Dec 4, 2021
AoC 2021: Python Day 3
View day3.py
from collections import Counter
data = open('day3.txt').read().splitlines()
def common_bits(bits,most=True):
data = list(zip(*bits))
mc = [Counter(d).most_common() for d in data]
mc = [d[0] if most else d[-1] for d in mc]
mc = [d[0] for d in mc]
return int("".join(mc),2)
@Solaxun
Solaxun / day2.py
Created Dec 4, 2021
AoC 2021: Python Day 2
View day2.py
data = open('day2.txt').read().splitlines()
depth, horizontal = 0,0
for line in data:
dir,amt = line.split()
amt = int(amt)
if dir == 'forward':
horizontal += amt
elif dir == 'down':
depth += amt
@Solaxun
Solaxun / day1.py
Created Dec 4, 2021
AoC 2021: Python Day 1
View day1.py
data = open('day1.txt').read().splitlines()
data = [int(a) for a in data]
cnt = 0
for a,b in zip(data,data[1:]):
if b > a:
cnt += 1
print(cnt)
@Solaxun
Solaxun / tetris.clj
Created Aug 14, 2021
Tetris in Clojure (basic game-play mostly done, no UI yet - switching to CLJS for that (can't use threads, reworking to use channels)
View tetris.clj
(ns tetris.game)
(def rows 20)
(def cols 10)
(def board (vec (repeat rows (vec (repeat cols " ")))))
(defn new-game-state []
{:board board
:active-piece nil
:game-over? false