From 540bbb9a9ef2836748a7deeec364940aac48c245 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Fri, 23 Dec 2022 13:21:01 -0800 Subject: [PATCH] day 23 ivy wip --- day23/input.ivy | 1 + day23/sol.ivy | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ day23/toivy.py | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 day23/input.ivy create mode 100644 day23/sol.ivy create mode 100755 day23/toivy.py diff --git a/day23/input.ivy b/day23/input.ivy new file mode 100644 index 0000000..b8b5594 --- /dev/null +++ b/day23/input.ivy @@ -0,0 +1 @@ +input = 72 72 rho '#.#####..#..##......##.#..####....#.#.####..##.#######..##..##.###...#..' '..#..##.#.#.#.##.#......######.#...###....#....#.#..#####.....#..#.####.' '....#..#.###.####.......#.#.###..#.######...##..#.#.#..#.######.####..##' '...#.#..####...#..##.#.####..#..##.#####..##.#...#..##..#..#.#.#.####...' '.##..###..###.#.##...#..#.#.##.##.###.##....#.####.#.##.#..##.#####.#..#' '#.#.###.#######..#...#####.##..##..##.#...#.#...#####.##....###.#...###.' '...#...##.#..#.###....#.#.##..#.#..##.#.#.#..#...#.....#.####.##.#..##..' '####.#...##.##.#..#.#####.#.##.###..#...#.#.##.##......##..#.#..##..##..' '..#.##.##.####....##.....#.#.......##.#..#.###.####.###.#.....#.#.#...##' '##...###..###.#.####.#..#...###...####..#####.#.#..##....#.#.####...#..#' '####..#.#.##.##....#..##.###.#..#..###.##.#.....#######..##.##....#.#.##' '.##...#.#####...#......##.#.##..#.####......#..###.##.##..#.#...##....##' '.#.##.#.##.#..#..###.#.###....#.#.#.#.#.###....##..####.#######.##.##.#.' '#######.##.#...###.#..#######.#..##.#..#.#...#.#.#.#..##.##.#.#.#....#..' '#.#.#.#.##..##..#.##...##.#.###.####...####.##..#.##.##.###.###.#....##.' '#...######.#....####.#.#.##....#..#...##.........#####.###..##.#.#..#.#.' '#.#.#.#.#...#.###...#.....#.#....######......#..#..#.###.#..#.#.##.#....' '#.##.#...#####.#....#.#.#..##...##.##.##.#####.####.#.##.#.###...#.##.##' '#...#.#.###........##..#...#.###...###.##......##.####..#.##....##.#..##' '#####.#..#.....#######..........#..####..##.#.#####..###...#...##.#.####' '...###..##.##.##.##.#..###...###.#.##.#.###.##.#.#........###...#....##.' '###.##...##.###..#..#.#.#...##.#.#.....##.#.##....####.####..#######..##' '##.#.#..#..###.#.#######..#..#.#.#..#####...####...#.####....##.###..##.' '..#.#.#....####.#.......#..#..###.##.#...##.####..###.#.#..####..#.####.' '.#.#..##.##..#...##...###.##.#.#.#..#####...#...#####..##.#...###..#.##.' '..###.#...#.#...#.###...###.##.#.#....#.#...##...##.....#.##.#.###.#....' '##....#..#........###..#.##.#..##...#.#.#..##.####.#...#.########.###...' '..#####.##.#...#..##...###.#####...#....#.##.######.#..#.##....###..##..' '.####.##..###...#.##...#.#.##..###.......#.###.#.##...#.#....#.#.##..##.' '#..#..###....#..#.#.....#.#.#####..##...####....####.#.#.##....#####.##.' '##########.####.#...##.##...#....##..##...###....#..###.##..##.###...###' '##.###..##.####.....#.#.....#.##...#...###..#.#...#....###..###.......#.' '##..###.#....##..#.#....###...#.####.#.###.#.#.#..##.##..##.######.##...' '#...####...#.##..##.##.#..####.#..#..#.##.##....#..#.##..##.#.###..#.###' '#.#..##.###.....#....###.##.##.#.###.#####.#.....##.##.##..#.##.###..###' '#...#..##..#..###....#..#...#..###...#.##.#.#.###...###..#.##..#..##.##.' '#.#..#.##.#...#..............#.###...##.##.#.....########.###....#.....#' '#..#.#...##..#.#.###.##.#.#....####.#.##.#.###.###.#..#..##.....#..####.' '.....###....##.##..##..#....######......##.###..#.#......#.#..#..#...#.#' '.##...###.#.#.###...##...##.#.####..#.###.#.#.#.###...##.#...#....###..#' '#..####.#...#.##..#..#.###.###...#.#.#..##....####..#...##........####..' '....##.##....#.....#..##..###..####..##.#.#.######..#....#..####.###.#..' '.#.##..#..#.#.....#........#..###.##.###.##.#..#..##.....#.#.###.###..#.' '#.#.#.#..#.###.#..#.##.##..######.##.######.#...#.###..#...#...##....#.#' '##..#...##.#.#.#.######.####.###...####..#....####...#.#..###...##.##..#' '.##.#....#...##...#...##.##.####..#.##..#.##.####..#.##..#..###..######.' '.##.#.#.##.#.####..#..#..##.#....#...####.###.##.####.....###..#..#.#..#' '#.##..#....##....##...#.##..####.#..####....###.##...###....##.###.##...' '..##..##.#...#.#..###...#.#.#.###.#.#######..#..###.#.###..#.###....#..#' '#..##.##.####..##.#....##..#.#......###....######..#.....###.....#.#.#..' '.#...###.##...#.##.##...##...##..####...#..########.###..########.#..#.#' '###.##.###.#.##.#..#.###....##...###.#.###.##..#.#...#..#####.##.##...#.' '.###.####..###.#.....####.#...###.##.#.##..#..#....##....#.#..#....###..' '##.#.#..##.#####..########..##...####....#.##.#.#.###.###.####.....####.' '#....###.....####.###.###.##..#...#.##.##...##.#..#.........#####......#' '##.####.#.#..#..#.##...#.##......#...#.#.#.####.##..##...####.....#.#.#.' '...#.###.#..###.....#.#####.#.....####.###...#.###....#...#..#.#...##.#.' '.#.......#...#......##..#.###....#.######..###.##...###...##.#.#.##..#.#' '.##.###.#..#.#####..###...#.###...##.#.####.#.#.#..#....#####...#..##.##' '.###..#...##..#.....##.#...#.#...##..##.#.###..###.#....##.###.###..#...' '#######.##..#..#.#...#..#.#..#.#..###..#########.#.....#.....####.##.#.#' '#.##..####...##.#..###.###...#....####...####..#...#####...##.#...#####.' '#..##.#..#.######.#.#.#.##...##..#...##...#.......#.#####.#...#.##..#.#.' '.##..#..###..#####.#.##..######.##...#.###.##.##.#..#....#..#.###..####.' '...#...##.#...##.#.###.#.#..##..####.....#..........#..#.........##..##.' '#####..##......##.##...#.##.###...####....##.##..#.###.#..##..#.##.#....' '.###....##.#####.#.###.#.##...##.####....###..#......#.#..#....##.#.#.##' '..###.#....#######.#.#..#.###.##.##..#..###...##.###.##.####.##.##.####.' '.#.#...##.#..##.....###.#..#.#...##..#..#.#.#.###.##.##.#.....#.##..#...' '.#####.#....#..#.####..####..#....#....##.#...#.##...###.#.#...#########' '###.####....#.####..#..###....#.###.##...#####.###...#.#..#.#........###' '#.#.######...#.#..#####.#.####.....##...#.###.#.##..##.#.#.##..#.####.#.' \ No newline at end of file diff --git a/day23/sol.ivy b/day23/sol.ivy new file mode 100644 index 0000000..0ce0400 --- /dev/null +++ b/day23/sol.ivy @@ -0,0 +1,64 @@ +)get "input.ivy" +sample = 7 7 rho '....#..' '..###.#' '#...#.#' '.#...##' '#.###..' '##.#.##' '.#..#..' + +board = sample == '#' +#board + +op north a = 1 flip a +op south a = -1 flip a +op west a = 1 rot a +op east a = -1 rot a + +#north 3 3 rho iota 9 + +op a coalesce b = + a: a + b + +op any a = or/,a +op pad a = 0,(transp (0,(transp a),0)),0 +op needsPad a = (any 0 != a[1,(rho a)[1]]) or (any 0 != a[;(1,(rho a)[2])]) +op maybePad a = + needsPad a: pad a + a + + +op smearWE a = a or (west a) or (east a) +op smearNS a = a or (north a) or (south a) +op lift a = (1,rho a) rho a + +op show a = (text ".#23456789"[1+a]),"\n-" + +M = 0 + +a = maybePad board +n = south smearWE a +s = north smearWE a +e = west smearNS a +w = east smearNS a +#n + +p = 4 3 2 1 + +C = (lift n) , (lift s) , (lift w) , (lift e) +rho C +canMove = a and or/ 3 1 2 transp C # can move if there is an elf nearby +C2 = (not C) and (rho C) rho canMove # can move in a direction if not alone and there is no elf in that direction +show C2 +P1 = max/ p * 3 1 2 transp C2 # select direction +P2 = p o.== P1 # decompose into 4x?x? matrix +P1; "\n-" +C3 = (north P2[1]) + (south P2[2]) + (west P2[3]) + (east P2[4]) +C3; "\n-" +notBlock = C3 == 1 +C4 = (lift south notBlock), (lift north notBlock), (lift east notBlock), (lift west notBlock) +rho C4 +P3 = P2 and C4 +show P3 +moved = or/ 3 1 2 transp P3 +moved +#a and not moved +#show P3 +B = (a and not moved) + (north P3[1]) + (south P3[2]) + (west P3[3]) + (east P3[4]) +show a +show B diff --git a/day23/toivy.py b/day23/toivy.py new file mode 100755 index 0000000..823ecc6 --- /dev/null +++ b/day23/toivy.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +""" +Converts a text file containing a list of integers +into a format that can be loaded by ivy. +""" + +import sys +if sys.argv[1:]: + filename = sys.argv[1] +else: + filename = "input" + +varname = filename.split(".",1)[0] + +with open(filename) as f: + lines = f.read().splitlines() + +data = [] +for line in lines: + line = line.replace("-", ",") + data.append(line.split(",")) + +def count(row): + n = 0 + for x in row: + if x.isdigit(): + n += 1 + else: + n += len(x) + return n + +cols = max(map(count, data)) + +print("{} = {} {} rho".format(varname, len(data), cols), end="") +for row in data: + print("", " ".join( + x if x.isdigit() else repr(x) + for x in row), end="") + n = count(row) + if n < cols: + print(" 0" * (cols - n), end="")