├── resources ├── advent_2017 │ ├── day_06 │ │ └── input │ ├── day_23 │ │ └── input │ ├── day_13 │ │ └── input │ ├── day_18 │ │ └── input │ ├── day_24 │ │ └── input │ ├── day_22 │ │ └── input │ ├── day_02 │ │ └── input │ ├── day_25 │ │ └── input │ ├── day_01 │ │ └── input │ ├── day_21 │ │ └── input │ └── day_05 │ │ └── input └── advent_2018 │ ├── day_12 │ └── input │ ├── day_06 │ └── input │ ├── day_07 │ └── input │ ├── day_02 │ └── input │ └── day_01 │ └── input ├── src ├── advent_2017 │ ├── A17D01.h │ ├── A17D02.h │ ├── A17D03.h │ ├── A17D04.h │ ├── A17D05.h │ ├── A17D06.h │ ├── A17D07.h │ ├── A17D08.h │ ├── A17D09.h │ ├── A17D11.h │ ├── A17D13.h │ ├── A17D14.h │ ├── A17D15.h │ ├── A17D16.h │ ├── A17D17.h │ ├── A17D18.h │ ├── A17D19.h │ ├── A17D20.h │ ├── A17D21.h │ ├── A17D22.h │ ├── A17D23.h │ ├── A17D24.h │ ├── A17D25.h │ ├── A17D10.h │ ├── A17D20.m │ ├── A17D21.m │ ├── A17D22.m │ ├── A17D23.m │ ├── A17D24.m │ ├── A17D25.m │ ├── A17D12.h │ ├── day_04.cljc │ ├── day_01.cljc │ ├── day_05.cljc │ ├── day_13.cljc │ ├── day_17.cljc │ ├── day_11.cljc │ ├── day_24.cljc │ ├── day_02.cljc │ ├── day_06.cljc │ ├── day_09.cljc │ ├── A17D05.m │ ├── day_08.cljc │ ├── A17D01.m │ ├── day_15.cljc │ ├── day_19.cljc │ ├── day_16.cljc │ ├── day_20.cljc │ ├── day_14.cljc │ ├── day_10.cljc │ ├── day_03.cljc │ ├── day_21.cljc │ ├── A17D02.m │ ├── day_12.cljc │ ├── A17D13.m │ ├── A17D17.m │ ├── day_07.cljc │ ├── A17D06.m │ ├── day_25.cljc │ ├── A17D15.m │ ├── A17D04.m │ ├── A17D09.m │ ├── day_22.cljc │ ├── A17D14.m │ ├── day_23.cljc │ ├── A17D16.m │ ├── day_18.cljc │ ├── A17D10.m │ ├── A17D03.m │ ├── A17D19.m │ ├── A17D07.m │ ├── A17D12.m │ ├── A17D08.m │ ├── A17D18.m │ └── A17D11.m ├── advent_2022 │ ├── AOC04B.bas │ ├── AOC01A.bas │ ├── AOC04A.bas │ ├── AOC04L.bas │ ├── AOC02A.bas │ ├── AOC02B.bas │ ├── AOC01B.bas │ ├── AOC03A.bas │ └── AOC03B.bas ├── advent │ ├── ADProblem.h │ ├── util.cljc │ ├── ADProblem.m │ └── main.m └── advent_2018 │ ├── day_01.cljc │ ├── day_02.cljc │ ├── day_05.cljc │ ├── day_08.cljc │ ├── day_03.cljc │ ├── day_14.cljc │ ├── day_11.cljc │ ├── day_04.cljc │ ├── day_09.cljc │ ├── day_12.cljc │ ├── day_10.cljc │ ├── day_07.cljc │ ├── day_17.cljc │ ├── day_06.cljc │ ├── day_13.cljc │ └── day_16.cljc ├── .gitignore ├── advent.xcodeproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcshareddata │ └── xcschemes │ └── advent.xcscheme ├── README.md ├── deps.edn └── LICENSE /resources/advent_2017/day_06/input: -------------------------------------------------------------------------------- 1 | 10 3 15 10 5 15 5 15 9 2 5 8 5 2 3 6 2 | -------------------------------------------------------------------------------- /src/advent_2017/A17D01.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D01 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D02.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D02 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D03.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D03 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D04.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D04 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D05.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D05 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D06.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D06 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D07.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D07 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D08.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D08 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D09.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D09 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D11.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D11 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D13.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D13 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D14.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D14 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D15.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D15 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D16.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D16 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D17.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D17 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D18.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D18 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D19.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D19 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D20.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D20 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D21.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D21 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D22.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D22 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D23.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D23 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D24.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D24 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /src/advent_2017/A17D25.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D25 : ADProblem 7 | 8 | @end 9 | 10 | NS_ASSUME_NONNULL_END 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /lib 3 | /.planck_cache 4 | /.idea 5 | /pom.xml 6 | .cpcache 7 | .DS_Store 8 | .lsp 9 | /advent.xcodeproj/xcuserdata 10 | /advent.xcodeproj/project.xcworkspace/xcuserdata/ 11 | /.clj-kondo 12 | -------------------------------------------------------------------------------- /advent.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advent of Code 2 | 3 | [Advent of Code](http://adventofcode.com) in Clojure, Self-Hosted ClojureScript, HP-85 BASIC, and Objective-C. 4 | 5 | ## Clojure 6 | 7 | ``` 8 | clj 9 | ``` 10 | 11 | ## Self-Hosted ClojureScript 12 | 13 | ``` 14 | plk 15 | ``` 16 | -------------------------------------------------------------------------------- /src/advent_2017/A17D10.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D10 : ADProblem 7 | 8 | - (NSArray *)knotHashDecimal:(NSString *)s; 9 | 10 | @end 11 | 12 | NS_ASSUME_NONNULL_END 13 | -------------------------------------------------------------------------------- /src/advent_2017/A17D20.m: -------------------------------------------------------------------------------- 1 | #import "A17D20.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D20 6 | 7 | - (nullable id)part1 { 8 | return nil; 9 | } 10 | 11 | - (nullable id)part2 { 12 | return nil; 13 | } 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /src/advent_2017/A17D21.m: -------------------------------------------------------------------------------- 1 | #import "A17D21.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D21 6 | 7 | - (nullable id)part1 { 8 | return nil; 9 | } 10 | 11 | - (nullable id)part2 { 12 | return nil; 13 | } 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /src/advent_2017/A17D22.m: -------------------------------------------------------------------------------- 1 | #import "A17D22.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D22 6 | 7 | - (nullable id)part1 { 8 | return nil; 9 | } 10 | 11 | - (nullable id)part2 { 12 | return nil; 13 | } 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /src/advent_2017/A17D23.m: -------------------------------------------------------------------------------- 1 | #import "A17D23.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D23 6 | 7 | - (nullable id)part1 { 8 | return nil; 9 | } 10 | 11 | - (nullable id)part2 { 12 | return nil; 13 | } 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /src/advent_2017/A17D24.m: -------------------------------------------------------------------------------- 1 | #import "A17D24.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D24 6 | 7 | - (nullable id)part1 { 8 | return nil; 9 | } 10 | 11 | - (nullable id)part2 { 12 | return nil; 13 | } 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /src/advent_2017/A17D25.m: -------------------------------------------------------------------------------- 1 | #import "A17D25.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D25 6 | 7 | - (nullable id)part1 { 8 | return nil; 9 | } 10 | 11 | - (nullable id)part2 { 12 | return nil; 13 | } 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /src/advent_2022/AOC04B.bas: -------------------------------------------------------------------------------- 1 | 10 T0=TIME 2 | 20 F$="AOC04I" 3 | 30 ASSIGN# 1 TO F$ 4 | 40 T=0 5 | 50 ON ERROR GOTO 100 6 | 60 READ# 1 ; A,B,C,D 7 | 70 OFF ERROR 8 | 80 IF C<=B AND A<=D THEN T=T+1 9 | 90 GOTO 50 10 | 100 ASSIGN# 1 TO * 11 | 110 DISP T 12 | 120 DISP TIME-T0;"s" 13 | 130 END 14 | -------------------------------------------------------------------------------- /src/advent_2022/AOC01A.bas: -------------------------------------------------------------------------------- 1 | 10 N=2244 2 | 20 F$="AOC01I" 3 | 30 ASSIGN# 1 TO F$ 4 | 40 S=0 5 | 50 M=0 6 | 60 FOR I=1 TO N 7 | 70 READ# 1 ; X 8 | 80 S=S+X 9 | 90 IF X<>0 THEN GOTO 120 10 | 100 IF S>M THEN M=S @ DISP M 11 | 110 S=0 12 | 120 NEXT I 13 | 130 ASSIGN# 1 TO * 14 | 135 DISP "DONE";M 15 | 140 END 16 | -------------------------------------------------------------------------------- /src/advent_2022/AOC04A.bas: -------------------------------------------------------------------------------- 1 | 10 T0=TIME 2 | 20 F$="AOC04I" 3 | 30 ASSIGN# 1 TO F$ 4 | 40 T=0 5 | 50 ON ERROR GOTO 100 6 | 60 READ# 1 ; A,B,C,D 7 | 70 OFF ERROR 8 | 80 IF C>=A AND D<=B OR A>=C AND B<=D THEN T=T+1 9 | 90 GOTO 50 10 | 100 ASSIGN# 1 TO * 11 | 110 DISP T 12 | 120 DISP TIME-T0;"s" 13 | 130 END 14 | -------------------------------------------------------------------------------- /src/advent_2022/AOC04L.bas: -------------------------------------------------------------------------------- 1 | 10 T0=TIME 2 | 20 N=1000 3 | 30 F$="AOC04I" 4 | 40 CREATE F$,N,32 5 | 50 ASSIGN# 1 TO F$ 6 | 60 FOR I=1 TO N 7 | 70 ENTER 705 ; A,B,C,D 8 | 80 DISP I 9 | 90 DISP A;B;C;D 10 | 100 PRINT# 1 ; A,B,C,D 11 | 110 NEXT I 12 | 120 ASSIGN# 1 TO * 13 | 130 DISP TIME-T0;"s" 14 | 140 END 15 | -------------------------------------------------------------------------------- /advent.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | { 2 | :paths ["src" "resources"] 3 | 4 | :deps { 5 | org.clojure/clojurescript {:mvn/version "1.10.439"} 6 | org.clojure/test.check {:mvn/version "0.10.0-alpha3"} 7 | chivorcam {:mvn/version "1.0.0"} 8 | tubular {:mvn/version "1.3.0"} 9 | planck {:mvn/version "2.23.0"} 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/advent_2017/A17D12.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import "ADProblem.h" 3 | 4 | NS_ASSUME_NONNULL_BEGIN 5 | 6 | @interface A17D12 : ADProblem 7 | 8 | @end 9 | 10 | @interface UFStructure : NSObject 11 | 12 | - (NSUInteger)find:(NSUInteger)x; 13 | - (void)union:(NSUInteger)x with:(NSUInteger)y; 14 | - (NSUInteger)distinctSetCount; 15 | 16 | @end 17 | 18 | NS_ASSUME_NONNULL_END 19 | -------------------------------------------------------------------------------- /src/advent_2022/AOC02A.bas: -------------------------------------------------------------------------------- 1 | 5 T0=TIME 2 | 10 T=0 3 | 20 N=2500 4 | 30 F$="AOC02I" 5 | 40 ASSIGN# 1 TO F$ 6 | 50 FOR I=1 TO N 7 | 60 READ# 1 ; X$ 8 | 70 L=NUM(X$)-64 9 | 80 R=NUM(X$[3])-87 10 | 90 IF L=R THEN S=3 @ GOTO 110 11 | 100 IF L=3 AND R=1 OR L=2 AND R=3 OR L=1 AND R=2 THEN S=6 ELSE S=0 12 | 110 T=T+S+R 13 | 120 NEXT I 14 | 130 ASSIGN# 1 TO * 15 | 140 DISP T 16 | 145 DISP TIME-T0;"s" 17 | 150 END 18 | -------------------------------------------------------------------------------- /src/advent_2022/AOC02B.bas: -------------------------------------------------------------------------------- 1 | 5 T0=TIME 2 | 10 T=0 3 | 20 N=2500 4 | 30 F$="AOC02I" 5 | 40 ASSIGN# 1 TO F$ 6 | 50 FOR I=1 TO N 7 | 60 READ# 1 ; X$ 8 | 70 L=NUM(X$)-64 9 | 80 R=NUM(X$[3])-87 10 | 90 IF R=2 THEN S=3 @ P=L @ GOTO 110 11 | 100 IF R=3 THEN S=6 @ P=RMD(L,3)+1 @ GOTO 110 12 | 105 S=0 @ P=RMD(L+1,3)+1 13 | 110 T=T+S+P 14 | 120 NEXT I 15 | 130 ASSIGN# 1 TO * 16 | 140 DISP T 17 | 145 DISP TIME-T0;"s" 18 | 150 END 19 | -------------------------------------------------------------------------------- /resources/advent_2017/day_23/input: -------------------------------------------------------------------------------- 1 | set b 81 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 | -------------------------------------------------------------------------------- /src/advent_2022/AOC01B.bas: -------------------------------------------------------------------------------- 1 | 10 T0=TIME 2 | 20 N=2244 3 | 30 F$="AOC01I" 4 | 40 ASSIGN# 1 TO F$ 5 | 50 S=0 6 | 60 M0=0 @ M1=0 @ M2=0 7 | 70 FOR I=1 TO N 8 | 80 READ# 1 ; X 9 | 90 S=S+X 10 | 100 IF X<>0 THEN GOTO 160 11 | 110 IF S 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @interface ADProblem : NSObject 6 | 7 | @property (nonatomic, copy, readonly, nullable) NSString *inputPath; 8 | @property (nonatomic, copy, readonly) NSString *input; 9 | 10 | - (nullable instancetype)init NS_UNAVAILABLE; 11 | - (nullable instancetype)initWithInputPath:(nullable NSString *)inputPath NS_DESIGNATED_INITIALIZER; 12 | 13 | - (nullable id)part1; 14 | - (nullable id)part2; 15 | 16 | - (void)solve; 17 | 18 | @end 19 | 20 | NS_ASSUME_NONNULL_END 21 | -------------------------------------------------------------------------------- /resources/advent_2017/day_18/input: -------------------------------------------------------------------------------- 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 316 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 | -------------------------------------------------------------------------------- /resources/advent_2017/day_24/input: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /resources/advent_2018/day_12/input: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/advent_2017/day_04.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-04 2 | (:require 3 | #?(:cljs [planck.core :refer [line-seq]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (->> "advent_2017/day_04/input" io/resource io/reader line-seq)) 8 | 9 | (defn valid-passphrase? [normalize passphrase] 10 | (->> (str/split passphrase #" ") 11 | (map normalize) 12 | (apply distinct?))) 13 | 14 | (defn solve [normalize] 15 | (->> input 16 | (filter (partial valid-passphrase? normalize)) 17 | count)) 18 | 19 | (defn part-1 [] 20 | (solve identity)) 21 | 22 | (defn part-2 [] 23 | (solve sort)) 24 | -------------------------------------------------------------------------------- /src/advent_2022/AOC03A.bas: -------------------------------------------------------------------------------- 1 | 10 T0=TIME 2 | 20 F$="AOC03I" 3 | 30 ASSIGN# 1 TO F$ 4 | 40 DIM X$[60] 5 | 50 DIM S(52) 6 | 60 Z=0 7 | 70 DEF FNP(Q$) 8 | 80 Q=NUM(Q$)-96 9 | 90 IF Q<0 THEN Q=Q+58 10 | 100 FNP=Q 11 | 110 FN END 12 | 120 ON ERROR GOTO 260 13 | 130 READ# 1 ; X$ 14 | 140 OFF ERROR 15 | 150 FOR I=1 TO 52 @ S(I)=0 @ NEXT I 16 | 160 DISP X$ 17 | 170 H=LEN(X$)/2 18 | 180 FOR I=1 TO H @ S(FNP(X$[I,I]))=1 @ NEXT I 19 | 190 FOR I=H+1 TO LEN(X$) 20 | 200 T=FNP(X$[I,I]) 21 | 210 IF S(T)<>0 THEN P=T @ GOTO 230 22 | 220 NEXT I 23 | 230 DISP P 24 | 240 Z=Z+P 25 | 250 GOTO 120 26 | 260 ASSIGN# 1 TO * 27 | 270 DISP Z 28 | 280 DISP TIME-T0;"s" 29 | 290 END 30 | -------------------------------------------------------------------------------- /resources/advent_2018/day_06/input: -------------------------------------------------------------------------------- 1 | 242, 164 2 | 275, 358 3 | 244, 318 4 | 301, 335 5 | 310, 234 6 | 159, 270 7 | 82, 142 8 | 229, 286 9 | 339, 256 10 | 305, 358 11 | 224, 339 12 | 266, 253 13 | 67, 53 14 | 100, 143 15 | 64, 294 16 | 336, 303 17 | 261, 267 18 | 202, 86 19 | 273, 43 20 | 115, 256 21 | 78, 356 22 | 91, 234 23 | 114, 146 24 | 114, 260 25 | 353, 346 26 | 336, 283 27 | 312, 341 28 | 234, 119 29 | 281, 232 30 | 65, 203 31 | 95, 85 32 | 328, 72 33 | 285, 279 34 | 61, 123 35 | 225, 179 36 | 97, 140 37 | 329, 305 38 | 236, 337 39 | 277, 110 40 | 321, 335 41 | 261, 258 42 | 304, 190 43 | 41, 95 44 | 348, 53 45 | 226, 298 46 | 263, 187 47 | 106, 338 48 | 166, 169 49 | 310, 295 50 | 236, 191 51 | -------------------------------------------------------------------------------- /src/advent_2022/AOC03B.bas: -------------------------------------------------------------------------------- 1 | 10 T0=TIME 2 | 20 F$="AOC03I" 3 | 30 ASSIGN# 1 TO F$ 4 | 40 DIM X$[60] 5 | 50 DIM S(52) 6 | 60 Z=0 7 | 70 C=1 8 | 80 DEF FNP(Q$) 9 | 90 Q=NUM(Q$)-96 10 | 100 IF Q<0 THEN Q=Q+58 11 | 110 FNP=Q 12 | 120 FN END 13 | 130 FOR I=1 TO 52 @ S(I)=0 @ NEXT I 14 | 140 ON ERROR GOTO 310 15 | 150 READ# 1 ; X$ 16 | 160 OFF ERROR 17 | 170 DISP X$ 18 | 180 FOR I=1 TO LEN(X$) 19 | 190 T=FNP(X$[I,I]) 20 | 200 S(T)=BINIOR(S(T),C) 21 | 210 NEXT I 22 | 220 C=C*2 23 | 230 IF C<8 THEN GOTO 140 24 | 240 C=1 25 | 241 P=0 26 | 250 FOR I=1 TO 52 27 | 260 IF S(I)=7 THEN P=I @ GOTO 280 28 | 270 NEXT I 29 | 280 DISP P 30 | 290 Z=Z+P 31 | 300 GOTO 130 32 | 310 ASSIGN# 1 TO * 33 | 320 DISP Z 34 | 330 DISP TIME-T0;"s" 35 | 340 END 36 | -------------------------------------------------------------------------------- /src/advent_2018/day_01.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-01 2 | (:require 3 | #?(:cljs [planck.core :refer [line-seq read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io])) 5 | 6 | (def input (->> "advent_2018/day_01/input" io/resource io/reader line-seq)) 7 | 8 | (def data (map read-string input)) 9 | 10 | (defn part-1 [] 11 | (reduce + data)) 12 | 13 | (defn first-duplicate [xs] 14 | (let [result (reduce (fn [seen x] 15 | (if (seen x) 16 | (reduced x) 17 | (conj seen x))) 18 | #{} xs)] 19 | (if (set? result) 20 | nil 21 | result))) 22 | 23 | (defn part-2 [] 24 | (let [freqs (cons 0 (reductions + (cycle data)))] 25 | (first-duplicate freqs))) 26 | -------------------------------------------------------------------------------- /resources/advent_2017/day_22/input: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/advent_2017/day_01.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-01 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (-> "advent_2017/day_01/input" io/resource slurp)) 8 | 9 | (let [c->d (zipmap "0123456789" (range))] 10 | (defn str->digits 11 | [s] 12 | (map c->d s))) 13 | 14 | (def data (-> input str/trim str->digits)) 15 | 16 | (defn matches [xs ys] 17 | (->> 18 | (map vector xs ys) 19 | (filter (partial apply =)) 20 | (map first))) 21 | 22 | (defn solve [pair-up] 23 | (apply + (matches data (pair-up data)))) 24 | 25 | (defn part-1 [] 26 | (solve #(rest (cycle %)))) 27 | 28 | (defn part-2 [] 29 | (solve #(nthrest (cycle %) (/ (count %) 2)))) 30 | -------------------------------------------------------------------------------- /src/advent_2018/day_02.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-02 2 | (:require 3 | #?(:cljs [planck.core :refer [line-seq read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [advent-2018.day-01 :as day-01])) 6 | 7 | (def input (->> "advent_2018/day_02/input" io/resource io/reader line-seq)) 8 | 9 | (defn count-with-exact [box-ids n] 10 | (->> box-ids 11 | (map frequencies) 12 | (map vals) 13 | (keep #(some #{n} %)) 14 | count)) 15 | 16 | (defn part-1 [] 17 | (* (count-with-exact input 2) (count-with-exact input 3))) 18 | 19 | (defn delete [idx s] 20 | (str (subs s 0 idx) (subs s (inc idx)))) 21 | 22 | (defn part-2 [] 23 | (some (fn [idx] 24 | (->> input 25 | (map (partial delete idx)) 26 | day-01/first-duplicate)) 27 | (range))) 28 | -------------------------------------------------------------------------------- /src/advent_2017/day_05.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-05 2 | (:require 3 | #?(:cljs [planck.core :refer [line-seq read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (->> "advent_2017/day_05/input" io/resource io/reader line-seq)) 8 | 9 | (def data (map read-string input)) 10 | 11 | (defn solve [maze update-fn] 12 | (reduce (fn [[maze ndx] counter] 13 | (if-let [offset (get maze ndx)] 14 | [(update maze ndx update-fn) (+ ndx offset)] 15 | (reduced counter))) 16 | [(vec maze) 0] 17 | (range))) 18 | 19 | (defn part-1 [] 20 | (solve data inc)) 21 | 22 | (defn part-2 [] 23 | (solve data (fn [v] 24 | (if (<= 3 v) 25 | (dec v) 26 | (inc v))))) 27 | -------------------------------------------------------------------------------- /src/advent_2017/day_13.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-13 2 | (:require 3 | #?(:cljs [planck.core :refer [line-seq read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io])) 5 | 6 | (def input (->> "advent_2017/day_13/input" io/resource io/reader line-seq)) 7 | 8 | (def data (->> input (map #(mapv read-string (re-seq #"\d+" %))))) 9 | 10 | (defn caught? [depth range] 11 | (zero? (mod depth (* 2 (dec range))))) 12 | 13 | (defn part-1 [] 14 | (transduce (keep (fn [[depth range]] 15 | (when (caught? depth range) 16 | (* depth range)))) 17 | + data)) 18 | 19 | (defn part-2 [] 20 | (some (fn [delay] 21 | (when (not-any? (fn [[depth range]] 22 | (caught? (+ depth delay) range)) 23 | data) 24 | delay)) 25 | (range))) 26 | -------------------------------------------------------------------------------- /src/advent_2017/day_17.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-17 2 | (:require 3 | [advent.util :refer [nth']])) 4 | 5 | (def input 345) 6 | 7 | (defn spin [step [current-pos value buffer]] 8 | (let [new-pos (inc (mod (+ current-pos step) value))] 9 | [new-pos (inc value) (-> (subvec buffer 0 new-pos) 10 | (into [value]) 11 | (into (subvec buffer new-pos)))])) 12 | 13 | (defn part-1 [] 14 | (let [[current-pos _ buffer] (nth' (iterate (partial spin input) [0 1 [0]]) 2017)] 15 | (buffer (inc current-pos)))) 16 | 17 | (defn spin' [step [current-pos value after-zero]] 18 | (let [new-pos (inc (mod (+ current-pos step) value))] 19 | [new-pos (inc value) (if (== 1 new-pos) 20 | value 21 | after-zero)])) 22 | 23 | (defn part-2 [] 24 | (last (nth' (iterate (partial spin' input) [0 1 nil]) 50000000))) 25 | -------------------------------------------------------------------------------- /src/advent_2017/day_11.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-11 2 | (:require 3 | #?(:cljs [planck.core :refer [read]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io]) 5 | #?(:clj (:import (java.io PushbackReader)))) 6 | 7 | (def input (->> "advent_2017/day_11/input" io/resource io/reader #?(:clj PushbackReader.))) 8 | 9 | (def data (->> (repeatedly #(read {:eof nil} input)) (take-while some?))) 10 | 11 | (defn move [[x y z] dir] 12 | (case dir 13 | n [ x (inc y) (dec z)] 14 | ne [(inc x) y (dec z)] 15 | se [(inc x) (dec y) z] 16 | s [ x (dec y) (inc z)] 17 | sw [(dec x) y (inc z)] 18 | nw [(dec x) (inc y) z])) 19 | 20 | (defn dist [coords] 21 | (/ (apply + (map #(Math/abs ^long %) coords)) 2)) 22 | 23 | (defn part-1 [] 24 | (dist (reduce move [0 0 0] data))) 25 | 26 | (defn part-2 [] 27 | (apply max (map dist (reductions move [0 0 0] data)))) 28 | -------------------------------------------------------------------------------- /src/advent_2017/day_24.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-24 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (-> "advent_2017/day_24/input" io/resource slurp)) 8 | 9 | (def data (->> input str/split-lines (map #(re-seq #"\d+" %)) (map #(mapv read-string %)))) 10 | 11 | (defn optimum [base-total pins components component-value] 12 | (let [matches (filter #(some #{pins} %) components)] 13 | (if (empty? matches) 14 | base-total 15 | (apply max (map (fn [[a b :as match]] 16 | (optimum (apply + base-total component-value match) 17 | (if (= pins a) b a) 18 | (remove #{match} components) 19 | component-value)) 20 | matches))))) 21 | 22 | (defn part-1 [] 23 | (optimum 0 0 data 0)) 24 | 25 | (defn part-2 [] 26 | (rem (optimum 0 0 data 10000) 10000)) 27 | -------------------------------------------------------------------------------- /src/advent_2017/day_02.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-02 2 | (:refer-clojure :exclude [range]) 3 | (:require 4 | #?(:cljs [planck.core :refer [line-seq read-string]]) 5 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 6 | [clojure.string :as str])) 7 | 8 | (def input (->> "advent_2017/day_02/input" io/resource io/reader line-seq)) 9 | 10 | (def data (->> input 11 | (map #(str/split % #"\t")) 12 | (map #(map read-string %)))) 13 | 14 | (defn solve [f] 15 | (transduce 16 | (map f) 17 | + 18 | data)) 19 | 20 | (defn part-1 [] 21 | (solve #(- (apply max %) (apply min %)))) 22 | 23 | (defn divides? [x y] 24 | (and (not= 0 x y) 25 | (zero? (mod y x)))) 26 | 27 | (defn dividing-pairs [xs] 28 | (for [x1 xs 29 | x2 xs 30 | :when (and (distinct? x1 x2) 31 | (divides? x1 x2))] 32 | [x1 x2])) 33 | 34 | (defn first-integer-ratio [xs] 35 | (when-let [[x y] (first (dividing-pairs xs))] 36 | (/ y x))) 37 | 38 | (defn part-2 [] 39 | (solve first-integer-ratio)) 40 | -------------------------------------------------------------------------------- /src/advent_2018/day_05.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-05 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as string])) 6 | 7 | (def input (-> "advent_2018/day_05/input" io/resource slurp)) 8 | 9 | (defn lower-case [unit] 10 | #?(:clj (Character/toLowerCase ^Character unit) 11 | :cljs (string/lower-case unit))) 12 | 13 | (defn reacts? [x y] 14 | (and (not= x y) 15 | (= (lower-case x) (lower-case y)))) 16 | 17 | (defn add-unit [polymer unit] 18 | (if (some-> (peek polymer) (reacts? unit)) 19 | (pop polymer) 20 | (conj polymer unit))) 21 | 22 | (defn react [polymer] 23 | (reduce add-unit [] polymer)) 24 | 25 | (defn part-1 [] 26 | (count (react input))) 27 | 28 | (defn remove-units [x polymer] 29 | (remove (fn [y] 30 | (or (= x y) 31 | (reacts? x y))) 32 | polymer)) 33 | 34 | (defn part-2 [] 35 | (->> (into #{} (map lower-case input)) 36 | (map #(count (react (remove-units % input)))) 37 | (apply min))) 38 | -------------------------------------------------------------------------------- /src/advent_2017/day_06.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-06 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (-> "advent_2017/day_06/input" io/resource slurp)) 8 | 9 | (def data (as-> input x (str/trim x) (str/split x #"\t") (mapv read-string x))) 10 | 11 | (defn redistribute [banks] 12 | (let [max-val (apply max (vals banks)) 13 | max-ndx (apply min (keep (fn [[k v]] (when (= max-val v) k)) banks)) 14 | target-ndxs (map #(mod (+ max-ndx 1 %) (count banks)) 15 | (range (banks max-ndx)))] 16 | (merge-with + (assoc banks max-ndx 0) (frequencies target-ndxs)))) 17 | 18 | (defn solve [banks] 19 | (reduce (fn [[last-seen banks] steps] 20 | (if (last-seen banks) 21 | (reduced [steps (last-seen banks)]) 22 | [(assoc last-seen banks steps) (redistribute banks)])) 23 | [{} (zipmap (range) banks)] 24 | (range))) 25 | 26 | (defn part-1 [] 27 | (first (solve data))) 28 | 29 | (defn part-2 [] 30 | (apply - (solve data))) 31 | -------------------------------------------------------------------------------- /src/advent_2017/day_09.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-09 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (->> "advent_2017/day_09/input" io/resource slurp str/trim)) 8 | 9 | (defn solve [stream] 10 | (reduce (fn [acc ch] 11 | (case (:state acc) 12 | :cancel 13 | (assoc acc :state :garbage) 14 | 15 | :garbage 16 | (case ch 17 | \> (assoc acc :state :group) 18 | \! (assoc acc :state :cancel) 19 | (update acc :count inc)) 20 | 21 | :group 22 | (case ch 23 | \< (assoc acc :state :garbage) 24 | \{ (update acc :level inc) 25 | \} (-> acc 26 | (update :level dec) 27 | (update :score + (:level acc))) 28 | acc) 29 | 30 | (assoc acc :state :group))) 31 | {:level 1 :score 0 :count 0} 32 | stream)) 33 | 34 | (defn part-1 [] 35 | (:score (solve input))) 36 | 37 | (defn part-2 [] 38 | (:count (solve input))) 39 | -------------------------------------------------------------------------------- /resources/advent_2017/day_02/input: -------------------------------------------------------------------------------- 1 | 1919 2959 82 507 3219 239 3494 1440 3107 259 3544 683 207 562 276 2963 2 | 587 878 229 2465 2575 1367 2017 154 152 157 2420 2480 138 2512 2605 876 3 | 744 6916 1853 1044 2831 4797 213 4874 187 6051 6086 7768 5571 6203 247 285 4 | 1210 1207 1130 116 1141 563 1056 155 227 1085 697 735 192 1236 1065 156 5 | 682 883 187 307 269 673 290 693 199 132 505 206 231 200 760 612 6 | 1520 95 1664 1256 685 1446 253 88 92 313 754 1402 734 716 342 107 7 | 146 1169 159 3045 163 3192 1543 312 161 3504 3346 3231 771 3430 3355 3537 8 | 177 2129 3507 3635 2588 3735 3130 980 324 266 1130 3753 175 229 517 3893 9 | 4532 164 191 5169 4960 3349 3784 3130 5348 5036 2110 151 5356 193 1380 3580 10 | 2544 3199 3284 3009 3400 953 3344 3513 102 1532 161 143 2172 2845 136 2092 11 | 194 5189 3610 4019 210 256 5178 4485 5815 5329 5457 248 5204 4863 5880 3754 12 | 3140 4431 4534 4782 3043 209 216 5209 174 161 3313 5046 1160 160 4036 111 13 | 2533 140 4383 1581 139 141 2151 2104 2753 4524 4712 866 3338 2189 116 4677 14 | 1240 45 254 1008 1186 306 633 1232 1457 808 248 1166 775 1418 1175 287 15 | 851 132 939 1563 539 1351 1147 117 1484 100 123 490 152 798 1476 543 16 | 1158 2832 697 113 121 397 1508 118 2181 2122 809 2917 134 2824 3154 2791 17 | -------------------------------------------------------------------------------- /src/advent/util.cljc: -------------------------------------------------------------------------------- 1 | (ns advent.util) 2 | 3 | (defn count' 4 | "Like core count, but when applied to a directly reduceable coll, does not 5 | force the collection to be realized fully in memory." 6 | [coll] 7 | (transduce identity (completing (fn [c _] (inc c))) 0 coll)) 8 | 9 | (defn nth' 10 | "Like core nth, but when applied to a directly reduceable coll, does not 11 | force the collection to be realized fully in memory." 12 | ([coll n] 13 | (let [result (nth' coll n ::not-found)] 14 | (if (= result ::not-found) 15 | (throw (ex-info "Index out of bounds" {:n n})) 16 | result))) 17 | ([coll n not-found] 18 | (if (neg? n) 19 | not-found 20 | (transduce (drop n) (completing #(reduced %2)) not-found coll)))) 21 | 22 | (defn some' 23 | "Like core some, but when applied to a directly reduceable coll, does not 24 | force the collection to be realized fully in memory." 25 | [pred coll] 26 | (reduce (fn [_ x] 27 | (when (pred x) 28 | (reduced x))) 29 | nil 30 | coll)) 31 | 32 | (defn fixed-point [f x] 33 | (reduce #(if (= %1 %2) (reduced %1) %2) 34 | (iterate f x))) 35 | 36 | (defn map-vals [f m] 37 | (reduce-kv (fn [m k v] (assoc m k (f v))) {} m)) -------------------------------------------------------------------------------- /src/advent_2017/A17D05.m: -------------------------------------------------------------------------------- 1 | #import "A17D05.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D05 6 | 7 | - (NSArray *)data { 8 | NSMutableArray *rv = [[NSMutableArray alloc] init]; 9 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 10 | [rv addObject:@([line intValue])]; 11 | }]; 12 | return [rv copy]; 13 | } 14 | 15 | - (NSNumber *)solve:(NSNumber *(^)(NSNumber *))transform { 16 | NSUInteger counter = 0; 17 | NSUInteger ndx = 0; 18 | NSMutableArray *maze = [[self data] mutableCopy]; 19 | for (;;) { 20 | if (0 <= ndx && ndx < maze.count) { 21 | NSUInteger offset = maze[ndx].integerValue; 22 | maze[ndx] = transform(maze[ndx]); 23 | ndx += offset; 24 | } else { 25 | return @(counter); 26 | } 27 | counter++; 28 | } 29 | } 30 | 31 | - (nullable id)part1 { 32 | return [self solve:^NSNumber* (NSNumber *x) { 33 | return @(x.intValue + 1); 34 | }]; 35 | } 36 | 37 | - (nullable id)part2 { 38 | return [self solve:^NSNumber* (NSNumber *x) { 39 | return @(x.intValue >= 3 ? x.intValue - 1 : x.intValue + 1); 40 | }]; 41 | } 42 | 43 | @end 44 | 45 | NS_ASSUME_NONNULL_END 46 | -------------------------------------------------------------------------------- /src/advent_2018/day_08.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-08 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io])) 5 | 6 | (def input (-> "advent_2018/day_08/input" io/resource slurp)) 7 | 8 | (defn parse-license [input] 9 | (letfn [(parse-node [[child-count metadata-count & more]] 10 | (let [[children more] (nth (iterate parse-child [[] more]) child-count) 11 | [metadata more] (split-at metadata-count more)] 12 | [{:children children, :metadata metadata} more])) 13 | (parse-child [[children more]] 14 | (let [[child more] (parse-node more)] 15 | [(conj children child) more]))] 16 | (first (parse-node (map read-string (re-seq #"\d+" input)))))) 17 | 18 | (defn part-1 [] 19 | (->> (tree-seq (constantly true) :children (parse-license input)) 20 | (mapcat :metadata) 21 | (reduce +))) 22 | 23 | (defn part-2 [] 24 | (letfn [(node-value [{:keys [children metadata]}] 25 | (if (empty? children) 26 | (reduce + metadata) 27 | (->> metadata 28 | (keep #(get children (dec %))) 29 | (map node-value) 30 | (reduce +))))] 31 | (node-value (parse-license input)))) 32 | -------------------------------------------------------------------------------- /src/advent_2017/day_08.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-08 2 | (:refer-clojure :exclude [inc dec]) 3 | (:require 4 | #?(:cljs [planck.core :refer [eval read]]) 5 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 6 | [clojure.string :as str] 7 | [clojure.spec.alpha :as s]) 8 | #?(:clj (:import (java.io PushbackReader)))) 9 | 10 | (def input (->> "advent_2017/day_08/input" io/resource io/reader #?(:clj PushbackReader.))) 11 | 12 | (s/def ::instr (s/cat :tgt simple-symbol? :upd '#{inc dec} :val int? :if '#{if} :lhs simple-symbol? :cmp '#{< > <= >= != ==} :rhs int?)) 13 | 14 | (s/check-asserts true) 15 | (def data (->> (repeatedly #(read {:eof nil} input)) 16 | (take-while some?) 17 | (s/assert (s/* ::instr)) 18 | (s/conform (s/* ::instr)))) 19 | 20 | (def inc +) 21 | (def dec -) 22 | (def != not=) 23 | 24 | (def register-history 25 | (reductions (fn [acc {:keys [tgt upd val lhs cmp rhs]}] 26 | (eval `(cond-> '~acc (~cmp ('~acc '~lhs 0) ~rhs) (update '~tgt (fnil ~upd 0) ~val)))) 27 | (zipmap (map :lhs data) (repeat 0)) 28 | data)) 29 | 30 | (defn max-register-val [registers] 31 | (apply max (or (vals registers) [0]))) 32 | 33 | (defn part-1 [] 34 | (max-register-val (last register-history))) 35 | 36 | (defn part-2 [] 37 | (apply max (map max-register-val register-history))) 38 | -------------------------------------------------------------------------------- /src/advent_2017/A17D01.m: -------------------------------------------------------------------------------- 1 | #import "A17D01.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D01 6 | 7 | - (NSArray *)data { 8 | NSMutableArray *rv = [[NSMutableArray alloc] init]; 9 | [self.input enumerateSubstringsInRange:NSMakeRange(0, self.input.length - 1) 10 | options:NSStringEnumerationByComposedCharacterSequences 11 | usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 12 | [rv addObject:@(substring.integerValue)]; 13 | }]; 14 | return [rv copy]; 15 | } 16 | 17 | - (nullable id)solveWithData:(NSArray *)data offset:(NSUInteger)offset { 18 | __block NSInteger sum = 0; 19 | [data enumerateObjectsUsingBlock:^(NSNumber *number, NSUInteger idx, BOOL *stop) { 20 | if ([number isEqualToNumber:data[(idx + offset) % data.count]]) { 21 | sum += number.integerValue; 22 | } 23 | }]; 24 | return @(sum); 25 | } 26 | 27 | - (nullable id)part1 { 28 | NSArray *data = [self data]; 29 | return [self solveWithData:data offset:1]; 30 | } 31 | 32 | - (nullable id)part2 { 33 | NSArray *data = [self data]; 34 | return [self solveWithData:data offset:data.count / 2]; 35 | } 36 | 37 | @end 38 | 39 | NS_ASSUME_NONNULL_END 40 | -------------------------------------------------------------------------------- /src/advent_2017/day_15.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-15 2 | #?(:cljs (:require-macros [advent-2017.day-15 :refer [solve a-gen b-gen multiples]]))) 3 | 4 | #?(:clj (set! *unchecked-math* :warn-on-boxed)) 5 | 6 | (when #?(:clj true :cljs (re-matches #".*\$macros" (name (ns-name *ns*)))) 7 | 8 | (def ^:const input-a 512) 9 | (def ^:const input-b 191) 10 | 11 | (defmacro solve [max-count a a-gen b b-gen] 12 | `(loop [sum# 0 ~a input-a ~b input-b count# 0] 13 | (if (== count# ~max-count) 14 | sum# 15 | (let [next-a# (long ~a-gen) 16 | next-b# (long ~b-gen)] 17 | (if (== (bit-and next-a# 0xFFFF) (bit-and next-b# 0xFFFF)) 18 | (recur (inc sum#) next-a# next-b# (inc count#)) 19 | (recur sum# next-a# next-b# (inc count#))))))) 20 | 21 | (defmacro a-gen [a] `(rem (* ~a 16807) 2147483647)) 22 | (defmacro b-gen [b] `(rem (* ~b 48271) 2147483647)) 23 | 24 | (defmacro multiples [n gen m] 25 | `(loop [~n ~gen] 26 | (if (zero? (rem ~n ~m)) 27 | ~n 28 | (recur ~gen))))) 29 | 30 | (when #?(:clj true :cljs (not (re-matches #".*\$macros" (name (ns-name *ns*))))) 31 | 32 | (declare a b) 33 | 34 | (defn part-1 [] 35 | (solve 40e6 a (a-gen a) b (b-gen b))) 36 | 37 | (defn part-2 [] 38 | (solve 5e6 39 | a (multiples a (a-gen a) 4) 40 | b (multiples b (b-gen b) 8)))) 41 | -------------------------------------------------------------------------------- /src/advent_2017/day_19.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-19 2 | (:require 3 | [advent.util :refer [count']] 4 | #?(:cljs [planck.core :refer [slurp]]) 5 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 6 | [clojure.string :as str])) 7 | 8 | (def input (-> "advent_2017/day_19/input" io/resource slurp)) 9 | 10 | (def matrix (mapv vec (str/split-lines input))) 11 | 12 | (defn move [state] 13 | (apply update-in state 14 | (case (:dir state) 15 | :down [[:loc 0] inc] 16 | :up [[:loc 0] dec] 17 | :left [[:loc 1] dec] 18 | :right [[:loc 1] inc]))) 19 | 20 | (defn update-dir [state] 21 | (let [adjacent (fn [idx] (get-in matrix (update (:loc state) idx dec)))] 22 | (cond-> state (= (get-in matrix (:loc state)) \+) 23 | (assoc :dir (case (:dir state) 24 | (:down :up) (if (#{\space \|} (adjacent 1)) :right :left) 25 | (:right :left) (if (#{\space \-} (adjacent 0)) :down :up)))))) 26 | 27 | (def path (eduction 28 | (map :loc) 29 | (take-while #(not= \space (get-in matrix %))) 30 | (iterate (comp update-dir move) 31 | {:dir :down :loc [0 (.indexOf (matrix 0) \|)]}))) 32 | 33 | (defn part-1 [] 34 | (let [letters (set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")] 35 | (transduce (keep #(letters (get-in matrix %))) str path))) 36 | 37 | (defn part-2 [] 38 | (count' path)) 39 | -------------------------------------------------------------------------------- /src/advent_2018/day_03.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-03 2 | (:require 3 | #?(:cljs [planck.core :refer [line-seq read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io])) 5 | 6 | (def input-lines (->> "advent_2018/day_03/input" io/resource io/reader line-seq)) 7 | 8 | (defn read-claim [s] 9 | (->> (rest (re-find #"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)" s)) 10 | (map read-string) 11 | (zipmap [:id :left :top :width :height]))) 12 | 13 | (def claims (map read-claim input-lines)) 14 | 15 | (defn locs [claim] 16 | (for [row (range (:top claim) (+ (:top claim) (:height claim))) 17 | col (range (:left claim) (+ (:left claim) (:width claim)))] 18 | [row col])) 19 | 20 | (defn add-claim [fabric claim] 21 | (reduce 22 | (fn [fabric loc] 23 | (update fabric loc conj (:id claim))) 24 | fabric 25 | (locs claim))) 26 | 27 | (defn loc->claims-ids [claims] 28 | (reduce add-claim {} claims)) 29 | 30 | (defn overlapping? [loc-claims] 31 | (> (count loc-claims) 1)) 32 | 33 | (defn overlapping-loc-claims [claims] 34 | (filter overlapping? (vals (loc->claims-ids claims)))) 35 | 36 | (defn part-1 [] 37 | (count (overlapping-loc-claims claims))) 38 | 39 | (defn part-2 [] 40 | (let [overlapping-claim-ids (reduce into #{} (overlapping-loc-claims claims)) 41 | all-claim-ids (map :id claims)] 42 | (first (remove overlapping-claim-ids all-claim-ids)))) 43 | -------------------------------------------------------------------------------- /src/advent_2017/day_16.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-16 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (-> "advent_2017/day_16/input" io/resource slurp str/trim)) 8 | 9 | (defn parse-dance-move [s] 10 | (if (= "s" (subs s 0 1)) 11 | (let [arg1 (read-string (subs s 1))] 12 | (fn [xs] (let [c (count xs)] 13 | (vec (take c (drop (- c arg1) (cycle xs))))))) 14 | (let [slash-idx (str/index-of s \/) 15 | arg1 (read-string (subs s 1 slash-idx)) 16 | arg2 (read-string (subs s (inc slash-idx))) 17 | swap (fn [xs idx1 idx2] (assoc xs idx1 (xs idx2) idx2 (xs idx1)))] 18 | (case (subs s 0 1) 19 | "x" (fn [xs] (swap xs arg1 arg2)) 20 | "p" (fn [xs] (swap xs (.indexOf xs arg1) (.indexOf xs arg2))))))) 21 | 22 | (def dance (->> (str/split input #",") (map parse-dance-move) reverse (apply comp))) 23 | 24 | (def starting-position (mapv (comp symbol str) "abcdefghijklmnop")) 25 | 26 | (defn part-1 [] 27 | (apply str (dance starting-position))) 28 | 29 | (defn part-2 [] 30 | (let [final-positions (iterate dance starting-position) 31 | period (inc (count (take-while (complement #{starting-position}) (rest final-positions))))] 32 | (apply str (nth final-positions (rem 1000000000 period))))) 33 | -------------------------------------------------------------------------------- /src/advent_2017/day_20.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-20 2 | (:require 3 | #?(:cljs [planck.core :refer [line-seq read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (->> "advent_2017/day_20/input" io/resource io/reader line-seq)) 8 | 9 | (def data (->> input 10 | (map #(re-seq #"-?\d+" %)) 11 | (map #(mapv read-string %)))) 12 | 13 | (def indexed (map conj data (range))) 14 | 15 | (defn step [particle] 16 | (let [[p v a idx] (partition-all 3 particle) 17 | v (map + v a) 18 | p (map + p v)] 19 | (concat p v a idx))) 20 | 21 | (defn dist [particle] 22 | (transduce (comp (take 3) (map #(Math/abs ^long %))) + particle)) 23 | 24 | (def closest-particle-idx (eduction (map #(sort-by dist %)) (map first) (map last) 25 | (iterate #(map step %) indexed))) 26 | 27 | (defn part-1 [] 28 | (into [] (take 500) closest-particle-idx)) ; eyeball result and guess 29 | 30 | (defn remove-colliding [particles] 31 | (->> particles 32 | (group-by #(take 3 %)) 33 | vals 34 | (remove #(> (count %) 1)) 35 | (map first))) 36 | 37 | (def particle-count (eduction (map count) 38 | (iterate (comp remove-colliding #(map step %)) indexed))) 39 | 40 | (defn part-2 [] 41 | (into [] (take 500) particle-count)) ; eyeball result and guess 42 | -------------------------------------------------------------------------------- /src/advent_2017/day_14.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-14 2 | (:refer-clojure :exclude [find]) 3 | (:require 4 | [advent-2017.day-10 :refer [knot-hash-decimal]] 5 | [advent-2017.day-12 :refer [union find]])) 6 | 7 | (def input "ljoxqyyw") 8 | 9 | (defn knot-hash-matrix [input] 10 | (mapv knot-hash-decimal (map #(str input "-" %) (range 128)))) 11 | 12 | (defn part-1 [] 13 | (apply + (map #?(:clj #(Long/bitCount %) :cljs bit-count) (flatten (knot-hash-matrix input))))) 14 | 15 | (defn part-2 [] 16 | (let [matrix (knot-hash-matrix input) 17 | bit-set? (fn [[row col]] 18 | (bit-test ((matrix row) (quot col 8)) (- 7 (mod col 8)))) 19 | linear-ndx (fn [[row col]] 20 | (+ (* row 128) col)) 21 | union-find (reduce union 22 | {} 23 | (for [coord (for [row (range 128) 24 | col (range 128)] 25 | [row col]) :when (bit-set? coord) 26 | neigh (for [offset [[0 0] [-1 0] [1 0] [0 -1] [0 1]]] 27 | (map + coord offset)) :when (and (every? #(<= 0 % 127) neigh) 28 | (bit-set? neigh))] 29 | [(linear-ndx coord) (linear-ndx neigh)]))] 30 | (count (distinct (map #(second (find union-find %)) (keys union-find)))))) 31 | -------------------------------------------------------------------------------- /src/advent_2017/day_10.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-10 2 | (:require 3 | #?(:cljs [planck.core :refer [read-string]]) 4 | [clojure.string :as str] 5 | [clojure.pprint :refer [cl-format]])) 6 | 7 | (def input "129,154,49,198,200,133,97,254,41,6,2,1,255,0,191,108") 8 | 9 | (defn round [lengths [xs current-pos skip-size]] 10 | (reduce (fn [[xs current-pos skip-size] length] 11 | (let [[to-reverse all-after] (split-at length (->> xs cycle (drop current-pos))) 12 | reversed-and-after (concat (reverse to-reverse) (take (- 256 length) all-after))] 13 | [(->> reversed-and-after cycle (drop (- 256 current-pos)) (take 256)) 14 | (mod (+ current-pos length skip-size) 256) 15 | (mod (inc skip-size) 256)])) 16 | [xs current-pos skip-size] 17 | lengths)) 18 | 19 | (defn part-1 [] 20 | (->> [(range 256) 0 0] 21 | (round (map read-string (str/split input #","))) 22 | first 23 | (take 2) 24 | (apply *))) 25 | 26 | (defn knot-hash-decimal 27 | [s] 28 | (->> [(range 256) 0 0] 29 | (iterate (partial round 30 | (into (mapv #?(:clj int :cljs #(.charCodeAt % 0) ) s) 31 | [17, 31, 73, 47, 23]))) 32 | (drop 64) 33 | ffirst 34 | (partition 16) 35 | (mapv #(reduce bit-xor %)))) 36 | 37 | (defn knot-hash 38 | [s] 39 | (->> (knot-hash-decimal s) 40 | (map #(cl-format nil "~2,'0x" %)) 41 | (apply str))) 42 | 43 | (defn part-2 [] 44 | (knot-hash input)) 45 | -------------------------------------------------------------------------------- /src/advent_2018/day_14.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-14 2 | (:require 3 | [clojure.string :as string])) 4 | 5 | (def digits 6 | (merge 7 | (zipmap (range 10) (map vector (range 10))) 8 | (zipmap (map #(+ % 10) (range 9)) (map #(vector 1 %) (range 9))))) 9 | 10 | (defn solve-1 [n] 11 | (loop [idx-1 0 idx-2 1 recipes [3 7]] 12 | (let [recipe-1 (recipes idx-1) 13 | recipe-2 (recipes idx-2) 14 | sum (+ recipe-1 recipe-2) 15 | recipes (apply conj recipes (digits sum)) 16 | c (count recipes)] 17 | (if (> c (+ n 10)) 18 | (string/join (subvec recipes n (+ n 10))) 19 | (recur (mod (+ idx-1 recipe-1 1) c) (mod (+ idx-2 recipe-2 1) c) recipes))))) 20 | 21 | (defn part-1 [] 22 | (solve-1 825401)) 23 | 24 | (defn solve-2 [desired] 25 | (loop [idx-1 0 idx-2 1 recipes [3 7]] 26 | (let [recipe-1 (recipes idx-1) 27 | recipe-2 (recipes idx-2) 28 | sum (+ recipe-1 recipe-2) 29 | recipes (apply conj recipes (digits sum)) 30 | c (count recipes)] 31 | (cond 32 | (< c 10) 33 | (recur (mod (+ idx-1 recipe-1 1) c) (mod (+ idx-2 recipe-2 1) c) recipes) 34 | 35 | (= desired (subvec recipes (- c (count desired) 1) (dec c))) 36 | (- c (count desired) 1) 37 | 38 | (= desired (subvec recipes (- c (count desired)))) 39 | (- c (count desired)) 40 | 41 | :else 42 | (recur (mod (+ idx-1 recipe-1 1) c) (mod (+ idx-2 recipe-2 1) c) recipes))))) 43 | 44 | (defn part-2 [] 45 | (solve-2 [8 2 5 4 0 1])) 46 | -------------------------------------------------------------------------------- /src/advent_2017/day_03.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-03 2 | (:require 3 | [advent.util :refer [nth']])) 4 | 5 | (def data 368078) 6 | 7 | (defn candidate-locations [[x y]] 8 | (map (fn [[dx dy]] 9 | [(+ x dx) (+ y dy)]) 10 | [[0 -1] [-1 0] [0 1] [1 0]])) 11 | 12 | (defn step [[location used-locations]] 13 | (let [next-location (->> location 14 | candidate-locations 15 | (map (fn [candidate-location] 16 | [candidate-location (contains? used-locations candidate-location)])) 17 | cycle 18 | (drop-while (complement second)) 19 | (drop-while second) 20 | ffirst)] 21 | [next-location (conj used-locations next-location)])) 22 | 23 | (def spiral (eduction (map first) (iterate step [[1 0] #{[0 0] [1 0]}]))) 24 | 25 | (defn location [square] 26 | (nth' spiral (- square 2))) 27 | 28 | (defn distance [[x y]] 29 | (+ (Math/abs x) (Math/abs y))) 30 | 31 | (defn part-1 [] 32 | (distance (location data))) 33 | 34 | (defn adjacent-locations [[x y]] 35 | (map (fn [[dx dy]] 36 | [(+ x dx) (+ y dy)]) 37 | [[-1 1] [0 1] [1 1] 38 | [-1 0] [1 0] 39 | [-1 -1] [0 -1] [1 -1]])) 40 | 41 | (defn part-2 [] 42 | (reduce (fn [acc location] 43 | (let [value (apply + (map #(acc % 0) (adjacent-locations location)))] 44 | (if (< data value) 45 | (reduced value) 46 | (assoc acc location value)))) 47 | {[0 0] 1} 48 | spiral)) 49 | -------------------------------------------------------------------------------- /src/advent/ADProblem.m: -------------------------------------------------------------------------------- 1 | #import "ADProblem.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation ADProblem { 6 | NSString *_input; 7 | NSArray *_inputLines; 8 | } 9 | 10 | - (nullable instancetype)initWithInputPath:(nullable NSString *)inputPath { 11 | self = [super init]; 12 | if (self) { 13 | _inputPath = [inputPath copy]; 14 | _input = nil; 15 | _inputLines = nil; 16 | } 17 | return self; 18 | } 19 | 20 | - (NSString *)input { 21 | if (!_input) { 22 | NSString *fullInputPath = _inputPath ? [NSString stringWithFormat:@"resources/%@/input", _inputPath] : nil; 23 | _input = [NSString stringWithContentsOfFile:fullInputPath encoding:NSUTF8StringEncoding error:nil]; 24 | if (!_input) { 25 | _input = @""; 26 | } 27 | } 28 | return _input; 29 | } 30 | 31 | - (nullable id)part1 { 32 | return nil; 33 | } 34 | 35 | - (nullable id)part2 { 36 | return nil; 37 | } 38 | 39 | - (void)solve { 40 | NSDate *startTime = [NSDate date]; 41 | id part1Result = [self part1]; 42 | NSDate *endTime = [NSDate date]; 43 | NSTimeInterval elapsedTime = [endTime timeIntervalSinceDate:startTime]; 44 | NSLog(@"%@ part1: %@ (%.3f ms)", [self class], part1Result, 1000 * elapsedTime); 45 | 46 | startTime = [NSDate date]; 47 | id part2Result = [self part2]; 48 | endTime = [NSDate date]; 49 | elapsedTime = [endTime timeIntervalSinceDate:startTime]; 50 | NSLog(@"%@ part2: %@ (%.3f ms)", [self class], part2Result, 1000 * elapsedTime); 51 | } 52 | 53 | @end 54 | 55 | NS_ASSUME_NONNULL_END 56 | -------------------------------------------------------------------------------- /src/advent_2018/day_11.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-11 2 | (:require 3 | [clojure.string :as string])) 4 | 5 | (def input 8868) 6 | 7 | (defn max-by [f xs] 8 | (let [[ndx max] (reduce-kv (fn [[ndx max] k v] 9 | (if (> (f v) (f max)) 10 | [k v] 11 | [ndx max])) 12 | [-1 (nth xs 0)] 13 | (vec (rest xs)))] 14 | [(+ ndx 2) max])) 15 | 16 | (defn hundreds-digit [x] 17 | (mod (quot x 100) 10)) 18 | 19 | (defn power-level [serial-number x y] 20 | (let [rack-id (+ x 10)] 21 | (-> rack-id 22 | (* y) 23 | (+ serial-number) 24 | (* rack-id) 25 | hundreds-digit 26 | (- 5)))) 27 | 28 | (defn power-levels [serial-number] 29 | (for [y (range 1 301)] 30 | (for [x (range 1 301)] 31 | (power-level serial-number x y)))) 32 | 33 | (defn solve [power-levels square-size] 34 | (let [[y [x m]] (->> power-levels 35 | (partition square-size 1) 36 | (map #(->> % 37 | (apply map +) 38 | (partition square-size 1) 39 | (map (partial apply +)) 40 | (max-by identity))) 41 | (max-by second))] 42 | [m x y])) 43 | 44 | (defn part-1 [] 45 | (string/join "," (rest (solve (power-levels input) 3)))) 46 | 47 | (def pmap' #?(:clj pmap :cljs map)) 48 | 49 | (defn part-2 [] 50 | (let [[ndx [_ x y]] (->> (range 1 301) 51 | (pmap' (partial solve (power-levels input))) 52 | (max-by first))] 53 | (string/join "," [x y ndx]))) 54 | -------------------------------------------------------------------------------- /src/advent_2017/day_21.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-21 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (-> "advent_2017/day_21/input" io/resource slurp)) 8 | 9 | (def transpose (partial apply map vector)) 10 | 11 | (defn dihedral [pattern] 12 | (let [rotations (take 4 (iterate (comp reverse transpose) (map seq pattern)))] 13 | (concat rotations (map reverse rotations)))) 14 | 15 | (defn make-pattern-map [input] 16 | (transduce (comp (mapcat #(str/split % #" => ")) 17 | (map #(str/split % #"/")) 18 | (partition-all 2) 19 | (map (fn [[input output]] 20 | (zipmap (dihedral input) (repeat 8 (map seq output)))))) 21 | merge 22 | (str/split-lines input))) 23 | 24 | (defn split-xf [size] 25 | (comp (map #(partition size %)) 26 | (partition-all size) 27 | (map transpose))) 28 | 29 | (def join-xf (comp (mapcat transpose) 30 | (map flatten))) 31 | 32 | (defn step* [pattern-map input] 33 | (let [size (if (even? (count (first input))) 34 | 2 35 | 3)] 36 | (eduction (split-xf size) 37 | (map #(map pattern-map %)) 38 | join-xf 39 | input))) 40 | 41 | (defn solve [iterations] 42 | (let [step (partial step* (make-pattern-map input))] 43 | (->> iterations 44 | (nth (iterate step [".#." "..#" "###"])) 45 | (transduce (comp (map #(filter #{\#} %)) 46 | (map count)) 47 | +)))) 48 | 49 | (defn part-1 [] 50 | (solve 5)) 51 | 52 | (defn part-2 [] 53 | (solve 18)) 54 | -------------------------------------------------------------------------------- /src/advent_2018/day_04.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-04 2 | (:require 3 | [advent.util :refer [map-vals]] 4 | #?(:cljs [planck.core :refer [line-seq read-string]]) 5 | [#?(:clj clojure.java.io :cljs planck.io) :as io])) 6 | 7 | (def input-lines (->> "advent_2018/day_04/input" io/resource io/reader line-seq)) 8 | 9 | (defn parse-int [s] 10 | #?(:clj (Integer/parseInt s) :cljs (js/parseInt s))) 11 | 12 | (defn mode [coll] 13 | (apply max-key val (frequencies coll))) 14 | 15 | (defn nap-log [input-lines] 16 | (:log (reduce (fn [acc input-line] 17 | (let [minute (parse-int (subs input-line 15 17))] 18 | (case (subs input-line 19 24) 19 | "Guard" (assoc acc :guard-id (read-string (subs input-line 26))) 20 | "falls" (assoc acc :start minute) 21 | "wakes" (update acc :log conj (-> (select-keys acc [:guard-id :start]) 22 | (assoc :end minute)))))) 23 | {:log []} (sort input-lines)))) 24 | 25 | (defn add-minutes [guard-id->minutes log-entry] 26 | (update guard-id->minutes (:guard-id log-entry) 27 | into (range (:start log-entry) (:end log-entry)))) 28 | 29 | (defn part-1 [] 30 | (let [guard-id->minutes (->> input-lines nap-log (reduce add-minutes {})) 31 | [guard-id minutes] (apply max-key (comp count val) guard-id->minutes) 32 | [minute] (mode minutes)] 33 | (* guard-id minute))) 34 | 35 | (defn part-2 [] 36 | (let [guard-id->minutes (->> input-lines nap-log (reduce add-minutes {})) 37 | guard-id->mode (map-vals mode guard-id->minutes) 38 | [guard-id [minute]] (apply max-key (comp val val) guard-id->mode)] 39 | (* guard-id minute))) 40 | -------------------------------------------------------------------------------- /src/advent_2018/day_09.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-09 2 | (:require 3 | [advent.util :refer [nth']])) 4 | 5 | (defn create-game [players] 6 | {:players players 7 | :player 1 8 | :index 1.0 9 | :marble 1 10 | :circle (sorted-map 0.0 0 1.0 1) 11 | :scores (zipmap (map inc (range players)) (repeat 0))}) 12 | 13 | (defn place-index [circle index] 14 | (let [[lb ub] (keys (rest (concat (subseq circle >= index) (cycle circle))))] 15 | (if (< lb ub) 16 | (/ (+ lb ub) 2.0) 17 | (inc (ffirst (rseq circle)))))) 18 | 19 | (defn counter-clockwise-index [circle index n] 20 | (-> (keys (concat (rsubseq circle <= index) (cycle (rseq circle)))) 21 | (nth n))) 22 | 23 | (defn advance [game] 24 | (let [{:keys [players player index marble circle scores]} game 25 | marble (inc marble) 26 | player (inc (mod player players)) 27 | game (assoc game :marble marble :player player)] 28 | (if (zero? (mod marble 23)) 29 | (let [remove-index (counter-clockwise-index circle index 7) 30 | scores (update scores player + marble (circle remove-index)) 31 | circle (dissoc circle remove-index) 32 | index (ffirst (or (subseq circle > remove-index) circle))] 33 | (assoc game :index index :circle circle :scores scores)) 34 | (let [index (place-index circle index) 35 | circle (assoc circle index marble)] 36 | (assoc game :index index :circle circle))))) 37 | 38 | (defn solve [players last-marble] 39 | (let [game (-> (iterate advance (create-game players)) 40 | (nth' (dec last-marble)))] 41 | (apply max (vals (:scores game))))) 42 | 43 | (defn part-1 [] 44 | (solve 486 70833)) 45 | 46 | (defn part-2 [] 47 | (solve 486 (* 100 70833))) 48 | -------------------------------------------------------------------------------- /src/advent_2017/A17D02.m: -------------------------------------------------------------------------------- 1 | #import "A17D02.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D02 6 | 7 | - (NSArray *> *)data { 8 | NSMutableArray *> *rv = [[NSMutableArray alloc] init]; 9 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 10 | NSArray *strings = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 11 | NSMutableArray *numbers = [[NSMutableArray alloc] initWithCapacity:strings.count]; 12 | for (NSString *string in strings) { 13 | [numbers addObject:@([string intValue])]; 14 | } 15 | [rv addObject:[numbers copy]]; 16 | }]; 17 | return [rv copy]; 18 | } 19 | 20 | - (id)solve:(int (^)(NSArray *))block { 21 | __block int sum = 0; 22 | [[self data] enumerateObjectsUsingBlock:^(NSArray *arr, NSUInteger idx, BOOL *stop) { 23 | sum += block(arr); 24 | }]; 25 | return @(sum); 26 | } 27 | 28 | - (nullable id)part1 { 29 | return [self solve:^int(NSArray *arr) { 30 | NSNumber *maxNumber = [arr valueForKeyPath:@"@max.self"]; 31 | NSNumber *minNumber = [arr valueForKeyPath:@"@min.self"]; 32 | return maxNumber.intValue - minNumber.intValue; 33 | }]; 34 | } 35 | 36 | - (nullable id)part2 { 37 | return [self solve:^int(NSArray *arr) { 38 | for (NSNumber *x in arr) { 39 | for (NSNumber *y in arr) { 40 | if (![y isEqualToNumber:x] && !(y.intValue % x.intValue)) { 41 | return y.intValue / x.intValue; 42 | } 43 | } 44 | } 45 | return 0; 46 | }]; 47 | } 48 | 49 | @end 50 | 51 | NS_ASSUME_NONNULL_END 52 | -------------------------------------------------------------------------------- /src/advent_2017/day_12.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-12 2 | (:refer-clojure :exclude [find]) 3 | (:require 4 | #?(:cljs [planck.core :refer [read]]) 5 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 6 | [clojure.spec.alpha :as s]) 7 | #?(:clj (:import (java.io PushbackReader)))) 8 | 9 | (def input (->> "advent_2017/day_12/input" io/resource io/reader #?(:clj PushbackReader.))) 10 | 11 | (s/def ::line (s/cat :node int? :conn '#{<->} :nodes (s/* int?))) 12 | 13 | (s/check-asserts true) 14 | (def data (->> (repeatedly #(read {:eof nil} input)) 15 | (take-while some?) 16 | (s/assert (s/* ::line)) 17 | (s/conform (s/* ::line)))) 18 | 19 | (defn find [uf x] 20 | (let [parent (get-in uf [x :parent])] 21 | (cond 22 | (nil? parent) [(assoc-in uf [x :parent] x) x] 23 | (== x parent) [uf parent] 24 | :else (let [[uf root] (find uf parent)] 25 | [(assoc-in uf [x :parent] root) root])))) 26 | 27 | (defn union [uf [x y]] 28 | (let [[uf x-root] (find uf x) 29 | [uf y-root] (find uf y)] 30 | (if (== x-root y-root) 31 | uf 32 | (let [cmp (compare (get-in uf [x-root :rank] 0) (get-in uf [y-root :rank] 0))] 33 | (if (neg? cmp) 34 | (assoc-in uf [x-root :parent] y-root) 35 | (cond-> (assoc-in uf [y-root :parent] x-root) 36 | (zero? cmp) 37 | (update-in [x-root :rank] (fnil inc 0)))))))) 38 | 39 | (def uf (reduce (fn [uf {:keys [node nodes]}] 40 | (reduce #(union %1 [node %2]) uf nodes)) 41 | {} 42 | data)) 43 | 44 | (def root (partial (comp second find) uf)) 45 | 46 | (defn part-1 [] 47 | (count (filter #(== (root 0) (root %)) (keys uf)))) 48 | 49 | (defn part-2 [] 50 | (count (distinct (map root (keys uf))))) 51 | -------------------------------------------------------------------------------- /src/advent_2018/day_12.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-12 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as string])) 6 | 7 | (def input (-> "advent_2018/day_12/input" io/resource slurp)) 8 | 9 | (defn parse-note [note] 10 | (when (= "#" (subs note 9)) 11 | (map #{\#} (subs note 0 5)))) 12 | 13 | (defn parse-state [state] 14 | (into (sorted-set) (keep-indexed (fn [idx pot] 15 | (when (= \# pot) idx)) 16 | state))) 17 | 18 | (defn parse-input [input] 19 | (let [[initial-state-line _ & notes] (string/split-lines input)] 20 | [(parse-state (subs initial-state-line 15)) 21 | (into #{} (keep parse-note notes))])) 22 | 23 | (defn generation [notes state] 24 | (reduce (fn [state' idxs] 25 | (let [grow? (notes (map #(when (state %) \#) idxs))] 26 | (cond-> state' grow? (conj (nth idxs 2))))) 27 | (sorted-set) 28 | (partition 5 1 29 | (range (- (first state) 5) (inc (+ (first (rseq state)) 5)))))) 30 | 31 | (defn states [input] 32 | (let [[state notes] (parse-input input)] 33 | (iterate (partial generation notes) state))) 34 | 35 | (defn solve [states n] 36 | (reduce + (nth states n))) 37 | 38 | (defn solve-extrapolating [states n] 39 | (let [shifted? #(apply = (apply map - %)) 40 | idx (first (keep-indexed #(when (shifted? %2) %1) 41 | (map vector states (rest states)))) 42 | base (solve states idx) 43 | next (solve states (inc idx))] 44 | (+ base (* (- next base) (- n idx))))) 45 | 46 | (defn part-1 [] 47 | (solve (states input) 20)) 48 | 49 | (defn part-2 [] 50 | (solve-extrapolating (states input) 50000000000)) 51 | -------------------------------------------------------------------------------- /src/advent_2018/day_10.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-10 2 | (:require 3 | [advent.util :refer [count' nth']] 4 | #?(:cljs [planck.core :refer [line-seq read-string]]) 5 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 6 | [clojure.string :as string])) 7 | 8 | (def input-lines (->> "advent_2018/day_10/input" io/resource io/reader line-seq)) 9 | 10 | (defn read-point [s] 11 | (zipmap [:p-x :p-y :v-x :v-y] (map read-string (re-seq #"-?\d+" s)))) 12 | 13 | (defn update-point [point] 14 | (-> point 15 | (update :p-x + (:v-x point)) 16 | (update :p-y + (:v-y point)))) 17 | 18 | (defn raster-geometry [points] 19 | (let [min-max (juxt #(apply min %) #(apply max %)) 20 | [min-x max-x] (min-max (map :p-x points)) 21 | [min-y max-y] (min-max (map :p-y points))] 22 | [min-x min-y (- max-x min-x) (- max-y min-y)])) 23 | 24 | (defn big? [points] 25 | (let [[_ _ width height] (raster-geometry points)] 26 | (or (> width 80) (> height 10)))) 27 | 28 | (defn rasterize [points] 29 | (let [[origin-x origin-y width height] (raster-geometry points)] 30 | (reduce (fn [raster {:keys [p-x p-y]}] 31 | (assoc-in raster [(- p-y origin-y) (- p-x origin-x)] "#")) 32 | (vec (repeat (inc height) (vec (repeat (inc width) ".")))) 33 | points))) 34 | 35 | (defn print-raster [raster] 36 | (println (string/join \newline (map string/join raster)))) 37 | 38 | (defn part-1 [] 39 | (let [initial-points (map read-point input-lines) 40 | points (nth' (eduction (drop-while big?) (iterate #(map update-point %) initial-points)) 0)] 41 | (print-raster (rasterize points)))) 42 | 43 | (defn part-2 [] 44 | (let [initial-points (map read-point input-lines)] 45 | (count' (eduction (take-while big?) (iterate #(map update-point %) initial-points))))) 46 | -------------------------------------------------------------------------------- /src/advent_2018/day_07.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-07 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as string])) 6 | 7 | (def input (-> "advent_2018/day_07/input" io/resource slurp)) 8 | 9 | (defn parse-line [s] 10 | [(nth s 36) (nth s 5)]) 11 | 12 | (defn input->dep-map [input] 13 | (->> (string/split-lines input) 14 | (map parse-line) 15 | (reduce (fn [m [k v]] 16 | (update m k (fnil conj #{}) v)) 17 | {}))) 18 | 19 | (defn eliminate [dep-map completed] 20 | (reduce-kv (fn [m k v] 21 | (if-some [v' (not-empty (reduce disj v completed))] 22 | (assoc m k v') 23 | m)) 24 | {} 25 | dep-map)) 26 | 27 | (defn decrement [remaining in-process] 28 | (reduce-kv (fn [m k v] 29 | (if (some #{k} in-process) 30 | (if (== 1 v) 31 | m 32 | (assoc m k (dec v))) 33 | (assoc m k v))) 34 | {} 35 | remaining)) 36 | 37 | (defn solve [workers f init] 38 | (loop [acc init 39 | dep-map (input->dep-map input) 40 | remaining (zipmap "ABCDEFGHIJKLMNOPQRSTUVWXYZ" (iterate inc 61))] 41 | (if (seq remaining) 42 | (let [available (reduce disj (set (keys remaining)) (keys dep-map)) 43 | in-process (take workers (sort available))] 44 | (let [completed (filter #(== 1 (remaining %)) in-process)] 45 | (recur 46 | (f acc completed) 47 | (eliminate dep-map completed) 48 | (decrement remaining in-process)))) 49 | acc))) 50 | 51 | (defn part-1 [] 52 | (solve 1 (partial apply str) "")) 53 | 54 | (defn part-2 [] 55 | (solve 5 (fn [seconds _] (inc seconds)) 0)) 56 | -------------------------------------------------------------------------------- /resources/advent_2017/day_25/input: -------------------------------------------------------------------------------- 1 | Begin in state A. 2 | Perform a diagnostic checksum after 12425180 steps. 3 | 4 | In state A: 5 | If the current value is 0: 6 | - Write the value 1. 7 | - Move one slot to the right. 8 | - Continue with state B. 9 | If the current value is 1: 10 | - Write the value 0. 11 | - Move one slot to the right. 12 | - Continue with state F. 13 | 14 | In state B: 15 | If the current value is 0: 16 | - Write the value 0. 17 | - Move one slot to the left. 18 | - Continue with state B. 19 | If the current value is 1: 20 | - Write the value 1. 21 | - Move one slot to the left. 22 | - Continue with state C. 23 | 24 | In state C: 25 | If the current value is 0: 26 | - Write the value 1. 27 | - Move one slot to the left. 28 | - Continue with state D. 29 | If the current value is 1: 30 | - Write the value 0. 31 | - Move one slot to the right. 32 | - Continue with state C. 33 | 34 | In state D: 35 | If the current value is 0: 36 | - Write the value 1. 37 | - Move one slot to the left. 38 | - Continue with state E. 39 | If the current value is 1: 40 | - Write the value 1. 41 | - Move one slot to the right. 42 | - Continue with state A. 43 | 44 | In state E: 45 | If the current value is 0: 46 | - Write the value 1. 47 | - Move one slot to the left. 48 | - Continue with state F. 49 | If the current value is 1: 50 | - Write the value 0. 51 | - Move one slot to the left. 52 | - Continue with state D. 53 | 54 | In state F: 55 | If the current value is 0: 56 | - Write the value 1. 57 | - Move one slot to the right. 58 | - Continue with state A. 59 | If the current value is 1: 60 | - Write the value 0. 61 | - Move one slot to the left. 62 | - Continue with state E. 63 | -------------------------------------------------------------------------------- /src/advent_2017/A17D13.m: -------------------------------------------------------------------------------- 1 | #import "A17D13.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @interface A17D13 () 6 | 7 | @property (nonatomic, copy) NSArray *> *data; 8 | 9 | @end 10 | 11 | @implementation A17D13 { 12 | NSArray *> *_data; 13 | } 14 | 15 | - (NSArray *> *)data { 16 | if (!_data) { 17 | NSMutableArray *> *tmp = [[NSMutableArray alloc] init]; 18 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 19 | NSArray *components = [line componentsSeparatedByString:@": "]; 20 | [tmp addObject:@[@(components[0].integerValue), @(components[1].integerValue)]]; 21 | }]; 22 | _data = [tmp copy]; 23 | } 24 | return _data; 25 | } 26 | 27 | - (BOOL)isCaughtDepth:(NSInteger)depth range:(NSInteger)range { 28 | return !(depth % (2 * (range - 1))); 29 | } 30 | 31 | - (nullable id)part1 { 32 | NSInteger sum = 0; 33 | for (NSArray *arr in self.data) { 34 | NSInteger depth = arr[0].integerValue; 35 | NSInteger range = arr[1].integerValue; 36 | if ([self isCaughtDepth:depth range:range]) { 37 | sum += depth * range; 38 | } 39 | } 40 | return @(sum); 41 | } 42 | 43 | - (nullable id)part2 { 44 | NSInteger delay = 0; 45 | for (;;) { 46 | BOOL caught = NO; 47 | for (NSArray *arr in self.data) { 48 | NSInteger depth = arr[0].integerValue; 49 | NSInteger range = arr[1].integerValue; 50 | if ([self isCaughtDepth:(depth + delay) range:range]) { 51 | caught = YES; 52 | break; 53 | } 54 | } 55 | if (!caught) { 56 | return @(delay); 57 | } 58 | delay++; 59 | } 60 | } 61 | 62 | @end 63 | 64 | NS_ASSUME_NONNULL_END 65 | -------------------------------------------------------------------------------- /src/advent_2017/A17D17.m: -------------------------------------------------------------------------------- 1 | #import "A17D17.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @interface A17D17 () 6 | 7 | @property (nonatomic, assign, readonly) NSInteger data; 8 | 9 | @end 10 | 11 | @implementation A17D17 12 | 13 | - (NSArray *)spinStep:(NSInteger)step currentPos:(NSInteger)currentPos value:(NSInteger)value buffer:(NSArray *)buffer { 14 | NSInteger newPos = ((currentPos + step) % value) + 1; 15 | 16 | NSMutableArray *result = [[NSMutableArray alloc] init]; 17 | [result addObjectsFromArray:[buffer subarrayWithRange:NSMakeRange(0, newPos)]]; 18 | [result addObject:@(value)]; 19 | [result addObjectsFromArray:[buffer subarrayWithRange:NSMakeRange(newPos, buffer.count - newPos)]]; 20 | return @[@(newPos), result]; 21 | } 22 | 23 | - (NSInteger)data { 24 | return 345; 25 | } 26 | 27 | - (nullable id)part1 { 28 | NSInteger currentPos = 0; 29 | NSArray *buffer = @[@0]; 30 | for (NSUInteger idx = 0; idx < 2017; idx++) { 31 | NSArray *result = [self spinStep:self.data currentPos:currentPos value:idx + 1 buffer:buffer]; 32 | currentPos = ((NSNumber *)result[0]).integerValue; 33 | buffer = result[1]; 34 | } 35 | return buffer[currentPos + 1]; 36 | } 37 | 38 | - (NSArray *)spinPrimeStep:(NSInteger)step currentPos:(NSInteger)currentPos value:(NSInteger)value afterZero:(NSInteger)afterZero { 39 | NSInteger newPos = ((currentPos + step) % value) + 1; 40 | return @[@(newPos), @(newPos == 1 ? value : afterZero)]; 41 | } 42 | 43 | - (nullable id)part2 { 44 | NSInteger currentPos = 0; 45 | NSInteger afterZero = -1; 46 | for (NSUInteger idx = 0; idx < 50000000; idx++) { 47 | NSInteger value = idx + 1; 48 | currentPos = ((currentPos + self.data) % value) + 1; 49 | afterZero = (currentPos == 1 ? value : afterZero); 50 | } 51 | return @(afterZero); 52 | } 53 | 54 | @end 55 | 56 | NS_ASSUME_NONNULL_END 57 | -------------------------------------------------------------------------------- /src/advent_2017/day_07.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-07 2 | (:require 3 | #?(:cljs [planck.core :refer [read]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str] 6 | [clojure.spec.alpha :as s] 7 | [clojure.set :as set]) 8 | #?(:clj (:import (java.io PushbackReader)))) 9 | 10 | (def input (->> "advent_2017/day_07/input" io/resource io/reader #?(:clj PushbackReader.))) 11 | 12 | (s/def ::weight (s/coll-of int? :kind list? :min-count 1 :max-count 1)) 13 | (s/def ::program (s/alt ::solo (s/cat :name symbol? :weight ::weight) 14 | ::deps (s/cat :name symbol? :weight ::weight ::arrow #{'->} :held (s/* symbol?)))) 15 | 16 | (s/check-asserts true) 17 | (def data (->> (repeatedly #(read {:eof nil} input)) 18 | (take-while some?) 19 | (s/assert (s/* ::program)) 20 | (s/conform (s/* ::program)) 21 | (map second) 22 | (map #(update % :weight first)))) 23 | 24 | (defn part-1 [] 25 | (first (set/difference (into #{} (map :name data)) (into #{} (flatten (keep :held data)))))) 26 | 27 | (let [index (set/index data [:name])] 28 | (defn lookup [name] 29 | (first (index {:name name})))) 30 | 31 | (defn weight [name] 32 | (let [program (lookup name)] 33 | (apply + (:weight program) (map weight (:held program))))) 34 | 35 | (defn part-2 [] 36 | (some (fn [program] 37 | (when-let [held (:held program)] 38 | (let [weights (map weight held)] 39 | (when-not (apply == weights) 40 | #_(prn program weights) 41 | (let [unique-weight ((set/map-invert (frequencies weights)) 1)] 42 | (prn unique-weight (lookup (nth held (.indexOf weights unique-weight)))) 43 | (+ (:weight (lookup (nth held (.indexOf weights unique-weight)))) 44 | (- (apply min weights) (apply max weights)))))))) 45 | data)) 46 | -------------------------------------------------------------------------------- /src/advent_2017/A17D06.m: -------------------------------------------------------------------------------- 1 | #import "A17D06.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D06 6 | 7 | - (NSArray *)data { 8 | NSMutableArray *rv = [[NSMutableArray alloc] init]; 9 | [self.input enumerateSubstringsInRange:NSMakeRange(0, self.input.length) 10 | options:NSStringEnumerationByWords 11 | usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 12 | [rv addObject:@([substring intValue])]; 13 | }]; 14 | return [rv copy]; 15 | } 16 | 17 | - (NSArray *)redistribute:(NSArray *)banks { 18 | NSUInteger count = banks.count; 19 | NSInteger maxValue = [[banks valueForKeyPath:@"@max.self"] integerValue]; 20 | NSInteger maxIndex = [banks indexOfObject:@(maxValue)]; 21 | 22 | NSMutableArray *result = [banks mutableCopy]; 23 | result[maxIndex] = @0; 24 | 25 | for (NSInteger i = 0; i < maxValue; i++) { 26 | NSInteger targetIndex = (maxIndex + 1 + i) % count; 27 | result[targetIndex] = @(result[targetIndex].integerValue + 1); 28 | } 29 | 30 | return [result copy]; 31 | } 32 | 33 | - (NSArray *)solve:(NSArray *)banks { 34 | NSMutableDictionary *lastSeen = [[NSMutableDictionary alloc] init]; 35 | NSUInteger steps = 0; 36 | 37 | while (true) { 38 | NSString *banksKey = [banks componentsJoinedByString:@","]; 39 | NSNumber *s = lastSeen[banksKey]; 40 | if (s) { 41 | return @[@(steps), s]; 42 | } 43 | lastSeen[banksKey] = @(steps); 44 | banks = [self redistribute:banks]; 45 | steps++; 46 | } 47 | } 48 | 49 | - (nullable id)part1 { 50 | return [self solve:[self data]][0]; 51 | } 52 | 53 | - (nullable id)part2 { 54 | NSArray *arr = [self solve:[self data]]; 55 | return @(arr[0].integerValue - arr[1].integerValue); 56 | } 57 | 58 | @end 59 | 60 | NS_ASSUME_NONNULL_END 61 | -------------------------------------------------------------------------------- /resources/advent_2017/day_01/input: -------------------------------------------------------------------------------- 1 | 21752342814933766938172121674976879111362417653261522357855816893656462449168377359285244818489723869987861247912289729579296691684761143544956991583942215236568961875851755854977946147178746464675227699149925227227137557479769948569788884399379821111382536722699575759474473273939756348992714667963596189765734743169489599125771443348193383566159843593541134749392569865481578359825844394454173219857919349341442148282229689541561169341622222354651397342928678496478671339383923769856425795211323673389723181967933933832711545885653952861879231537976292517866354812943192728263269524735698423336673735158993853556148833861327959262254756647827739145283577793481526768156921138428318939361859721778556264519643435871835744859243167227889562738712953651128317624673985213525897522378259178625416722152155728615936587369515254936828668564857283226439881266871945998796488472249182538883354186573925183152663862683995449671663285775397453876262722567452435914777363522817594741946638986571793655889466419895996924122915777224499481496837343194149123735355268151941712871245863553836953349887831949788869852929147849489265325843934669999391846286319268686789372513976522282587526866148166337215961493536262851512218794139272361292811529888161198799297966893366553115353639298256788819385272471187213579185523521341651117947676785341146235441411441813242514813227821843819424619974979886871646621918865274574538951761567855845681272364646138584716333599843835167373525248547542442942583122624534494442516259616973235858469131159773167334953658673271599748942956981954699444528689628848694446818825465485122869742839711471129862632128635779658365756362863627135983617613332849756371986376967117549251566281992964573929655589313871976556784849231916513831538254812347116253949818633527185174221565279775766742262687713114114344843534958833372634182176866315441583887177759222598853735114191874277711434653854816841589229914164681364497429324463193669337827467661773833517841763711156376147664749175267212562321567728575765844893232718971471289841171642868948852136818661741238178676857381583155547755219837116125995361896562498721571413742 2 | -------------------------------------------------------------------------------- /src/advent_2017/day_25.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-25 2 | (:require 3 | [advent.util :refer [nth']] 4 | #?(:cljs [planck.core :refer [slurp read-string]]) 5 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 6 | [clojure.string :as str])) 7 | 8 | (def input (-> "advent_2017/day_25/input" io/resource slurp)) 9 | 10 | (def header-re #"Begin in state ([A-Z]).\nPerform a diagnostic checksum after (\d+) steps.") 11 | (def instruction-re #"In state ([A-Z]):\n If the current value is 0:\n - Write the value (\d).\n - Move one slot to the (right|left).\n - Continue with state ([A-Z]).\n If the current value is 1:\n - Write the value (\d).\n - Move one slot to the (right|left).\n - Continue with state ([A-Z]).") 12 | 13 | (defn parse-header [input] 14 | (let [[_ state iterations] (first (re-seq header-re input))] 15 | [{:state (keyword state) 16 | :loc 0 17 | :one-locs #{}} 18 | (read-string iterations)])) 19 | 20 | (defn parse-program [input] 21 | (into {} (for [[_ state 22 | write-0 move-0 continue-0 23 | write-1 move-1 continue-1] (re-seq instruction-re input)] 24 | [(keyword state) {0 {:write (read-string write-0) 25 | :move (keyword move-0) 26 | :continue (keyword continue-0)} 27 | 1 {:write (read-string write-1) 28 | :move (keyword move-1) 29 | :continue (keyword continue-1)}}]))) 30 | 31 | (defn step* [program state] 32 | (let [current-val (if (contains? (:one-locs state) (:loc state)) 1 0) 33 | {:keys [write move continue]} (get-in program [(:state state) current-val])] 34 | (-> state 35 | (update :one-locs (case write 1 conj 0 disj) (:loc state)) 36 | (update :loc (case move :left dec :right inc)) 37 | (assoc :state continue)))) 38 | 39 | (defn part-1 [] 40 | (let [step (partial step* (parse-program input)) 41 | [state iterations] (parse-header input)] 42 | (count (:one-locs (nth' (iterate step state) iterations))))) 43 | -------------------------------------------------------------------------------- /src/advent_2017/A17D15.m: -------------------------------------------------------------------------------- 1 | #import "A17D15.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | static const NSInteger INPUT_A = 512; 6 | static const NSInteger INPUT_B = 191; 7 | 8 | @interface A17D15 () 9 | 10 | @property (nonatomic, copy) NSInteger (^genA)(NSInteger); 11 | @property (nonatomic, copy) NSInteger (^genB)(NSInteger); 12 | 13 | @end 14 | 15 | @implementation A17D15 16 | 17 | + (NSInteger (^)(NSInteger))makeGenMultiplier:(NSInteger)multiplier { 18 | return ^(NSInteger a) { 19 | return (a * multiplier) % 2147483647; 20 | }; 21 | } 22 | 23 | - (nullable instancetype)initWithInputPath:(nullable NSString *)inputPath { 24 | self = [super initWithInputPath:inputPath]; 25 | if (self) { 26 | _genA = [A17D15 makeGenMultiplier:16807]; 27 | _genB = [A17D15 makeGenMultiplier:48271]; 28 | } 29 | return self; 30 | } 31 | 32 | + (NSInteger (^)(NSInteger))makeGenBaseGen:(NSInteger (^)(NSInteger))baseGen multiples:(NSInteger)multiples { 33 | return ^(NSInteger x) { 34 | NSInteger result = baseGen(x); 35 | while (result % multiples != 0) { 36 | result = baseGen(result); 37 | } 38 | return result; 39 | }; 40 | } 41 | 42 | - (NSInteger)solveMaxCount:(NSUInteger)maxCount genA:(NSInteger (^)(NSInteger))genA genB:(NSInteger (^)(NSInteger))genB { 43 | NSUInteger count = 0; 44 | NSInteger sum = 0; 45 | NSInteger a = INPUT_A; 46 | NSInteger b = INPUT_B; 47 | for (;;) { 48 | if (count == maxCount) { 49 | return sum; 50 | } else { 51 | a = genA(a); 52 | b = genB(b); 53 | if ((a & 0xFFFF) == (b & 0xFFFF)) { 54 | sum++; 55 | } 56 | count++; 57 | } 58 | } 59 | } 60 | 61 | - (nullable id)part1 { 62 | return @([self solveMaxCount:40e6 genA:self.genA genB:self.genB]); 63 | } 64 | 65 | - (nullable id)part2 { 66 | return @([self solveMaxCount:5e6 67 | genA:[A17D15 makeGenBaseGen:_genA multiples:4] 68 | genB:[A17D15 makeGenBaseGen:_genB multiples:8]]); 69 | } 70 | 71 | @end 72 | 73 | NS_ASSUME_NONNULL_END 74 | -------------------------------------------------------------------------------- /src/advent_2017/A17D04.m: -------------------------------------------------------------------------------- 1 | #import "A17D04.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D04 6 | 7 | - (BOOL)areAllElementsDistinct:(NSArray *)array { 8 | NSSet *uniqueElements = [NSSet setWithArray:array]; 9 | return [uniqueElements count] == [array count]; 10 | } 11 | 12 | - (BOOL)isValidPassphrase:(NSString *)passphrase withTransform:(NSString* (^)(NSString *))transformBlock { 13 | NSMutableArray *components = [[NSMutableArray alloc] init]; 14 | [passphrase enumerateSubstringsInRange:NSMakeRange(0, passphrase.length) 15 | options:NSStringEnumerationByWords 16 | usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 17 | [components addObject:transformBlock ? transformBlock(substring) : substring]; 18 | }]; 19 | return [self areAllElementsDistinct:components]; 20 | } 21 | 22 | - (NSUInteger)solveWithTransform:(NSString* (^)(NSString *))transformBlock { 23 | __block NSUInteger count = 0; 24 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 25 | if ([self isValidPassphrase:line withTransform:transformBlock]) { 26 | count++; 27 | } 28 | }]; 29 | return count; 30 | } 31 | 32 | - (nullable id)part1 { 33 | return @([self solveWithTransform:^NSString* (NSString *input) { 34 | return input; 35 | }]); 36 | } 37 | 38 | - (nullable id)part2 { 39 | return @([self solveWithTransform:^NSString* (NSString *input) { 40 | NSMutableArray *characters = [NSMutableArray arrayWithCapacity:[input length]]; 41 | [input enumerateSubstringsInRange:NSMakeRange(0, input.length) 42 | options:NSStringEnumerationByComposedCharacterSequences 43 | usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 44 | [characters addObject:substring]; 45 | }]; 46 | NSArray *sortedCharacters = [characters sortedArrayUsingSelector:@selector(compare:)]; 47 | return [sortedCharacters componentsJoinedByString:@""]; 48 | }]); 49 | } 50 | 51 | @end 52 | 53 | NS_ASSUME_NONNULL_END 54 | -------------------------------------------------------------------------------- /src/advent_2017/A17D09.m: -------------------------------------------------------------------------------- 1 | #import "A17D09.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | typedef NS_ENUM(NSUInteger, A17D09State) { 6 | A17D09StateCancel, 7 | A17D09StateGarbage, 8 | A17D09StateGroup, 9 | }; 10 | 11 | @implementation A17D09 12 | 13 | - (NSDictionary *)solution { 14 | __block A17D09State state = A17D09StateGroup; 15 | __block NSUInteger level = 1; 16 | __block NSUInteger score = 0; 17 | __block NSUInteger count = 0; 18 | 19 | [self.input enumerateSubstringsInRange:NSMakeRange(0, self.input.length) 20 | options:NSStringEnumerationByComposedCharacterSequences 21 | usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 22 | unichar ch = [substring characterAtIndex:0]; 23 | switch (state) { 24 | case A17D09StateCancel: 25 | state = A17D09StateGarbage; 26 | break; 27 | case A17D09StateGarbage: 28 | switch (ch) { 29 | case '>': 30 | state = A17D09StateGroup; 31 | break; 32 | case '!': 33 | state = A17D09StateCancel; 34 | break; 35 | default: 36 | count++; 37 | } 38 | break; 39 | case A17D09StateGroup: 40 | switch (ch) { 41 | case '<': 42 | state = A17D09StateGarbage; 43 | break; 44 | case '{': 45 | level++; 46 | break; 47 | case '}': 48 | level--; 49 | score += level; 50 | break; 51 | } 52 | break; 53 | } 54 | }]; 55 | return @{@"score": @(score), @"count": @(count)}; 56 | } 57 | 58 | - (nullable id)part1 { 59 | return [self solution][@"score"]; 60 | } 61 | 62 | - (nullable id)part2 { 63 | return [self solution][@"count"]; 64 | } 65 | 66 | @end 67 | 68 | NS_ASSUME_NONNULL_END 69 | -------------------------------------------------------------------------------- /src/advent_2017/day_22.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-22 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as str])) 6 | 7 | (def input (-> "advent_2017/day_22/input" io/resource slurp)) 8 | 9 | (defn make-initial-state [input] 10 | (let [infected-locs (->> input 11 | str/split-lines 12 | (map-indexed (fn [idx line] 13 | (map vector 14 | (keep-indexed #(when (= \# %2) %1) line) 15 | (repeat idx)))) 16 | (apply concat))] 17 | {:loc [12 12] 18 | :dir [0 -1] 19 | :node-states (zipmap infected-locs (repeat :infected)) 20 | :infected-count 0})) 21 | 22 | (defn burst* [update-node-state {:keys [node-states loc dir] :as state}] 23 | (let [node-state (node-states loc :clean) 24 | new-node-state (update-node-state node-state) 25 | new-dir (let [[dx dy] dir] 26 | (case node-state 27 | :clean [ dy (- dx)] ; turn left 28 | :weakened [ dx dy ] ; unchanged 29 | :infected [(- dy) dx ] ; turn right 30 | :flagged [(- dx) (- dy)] ; reverse 31 | ))] 32 | (-> state 33 | (assoc :dir new-dir 34 | :loc (mapv + loc new-dir)) 35 | (update :node-states assoc loc new-node-state) 36 | (cond-> (= :infected new-node-state) (update :infected-count inc))))) 37 | 38 | (defn nth' [coll n] 39 | (transduce (drop n) (completing #(reduced %2)) nil coll)) 40 | 41 | (defn solve [node-state-cycle initial-state iterations] 42 | (let [burst (partial burst* (zipmap node-state-cycle (rest (cycle node-state-cycle))))] 43 | (-> (iterate burst initial-state) 44 | (nth' iterations) 45 | :infected-count))) 46 | 47 | (defn part-1 [] 48 | (solve [:clean :infected] 49 | (make-initial-state input) 10000)) 50 | 51 | (defn part-2 [] 52 | (solve [:clean :weakened :infected :flagged] 53 | (make-initial-state input) 10000000)) 54 | -------------------------------------------------------------------------------- /src/advent_2018/day_17.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-17 2 | (:require 3 | #?(:cljs [planck.core :refer [slurp read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.string :as string])) 6 | 7 | (def input (-> "advent_2018/day_17/input" io/resource slurp)) 8 | 9 | (defn parse-vein [s] 10 | (let [[a b1 b2] (map read-string (re-seq #"\d+" s))] 11 | (map (if (= (first s) \x) 12 | #(vector a %) 13 | #(vector % a)) 14 | (range b1 (inc b2))))) 15 | 16 | (defn flow [clay water max-y coords] 17 | (let [down (fn [[x y]] [x (inc y)]) 18 | left (fn [[x y]] [(dec x) y]) 19 | right (fn [[x y]] [(inc x) y]) 20 | sand? (fn [coords] 21 | (and (not (clay coords)) 22 | (not (water coords)))) 23 | holds? (fn holds? [dir coords] 24 | (and (or (clay (down coords)) 25 | (water (down coords))) 26 | (or (clay (dir coords)) 27 | (holds? dir (dir coords))))) 28 | fill (fn fill [water dir coords] 29 | (-> (cond-> water 30 | (not (clay (dir coords))) 31 | (fill dir (dir coords))) 32 | (assoc coords \~))) 33 | spill (fn [water dir coords] 34 | (if (and (sand? (dir coords)) 35 | (or (clay (down coords)) 36 | (= \~ (water (down coords))))) 37 | (flow clay water max-y (dir coords)) 38 | water)) 39 | water (assoc water coords \|)] 40 | (cond 41 | (= (second coords) max-y) 42 | water 43 | 44 | (sand? (down coords)) 45 | (let [water (flow clay water max-y (down coords))] 46 | (recur clay water max-y coords)) 47 | 48 | (and (holds? left coords) 49 | (holds? right coords)) 50 | (-> water 51 | (fill left coords) 52 | (fill right coords)) 53 | 54 | :else 55 | (-> water 56 | (spill right coords) 57 | (spill left coords))))) 58 | 59 | (defn final-water [input] 60 | (let [clay (into #{} (mapcat parse-vein (string/split-lines input))) 61 | min-y (apply min (map second clay)) 62 | max-y (apply max (map second clay))] 63 | (flow clay {} max-y [500 min-y]))) 64 | 65 | (defn part-1 [] 66 | (count (final-water input))) 67 | 68 | (defn part-2 [] 69 | (count (filter (fn [[_ y]] (= y \~)) (final-water input)))) 70 | -------------------------------------------------------------------------------- /src/advent/main.m: -------------------------------------------------------------------------------- 1 | #import 2 | #import "A17D01.h" 3 | #import "A17D02.h" 4 | #import "A17D03.h" 5 | #import "A17D04.h" 6 | #import "A17D05.h" 7 | #import "A17D06.h" 8 | #import "A17D07.h" 9 | #import "A17D08.h" 10 | #import "A17D09.h" 11 | #import "A17D10.h" 12 | #import "A17D11.h" 13 | #import "A17D12.h" 14 | #import "A17D13.h" 15 | #import "A17D14.h" 16 | #import "A17D15.h" 17 | #import "A17D16.h" 18 | #import "A17D17.h" 19 | #import "A17D18.h" 20 | #import "A17D19.h" 21 | #import "A17D20.h" 22 | #import "A17D20.h" 23 | #import "A17D21.h" 24 | #import "A17D22.h" 25 | #import "A17D23.h" 26 | #import "A17D24.h" 27 | #import "A17D25.h" 28 | 29 | int main(int argc, const char * argv[]) { 30 | @autoreleasepool { 31 | 32 | NSArray *problems = @[ 33 | //[[A17D01 alloc] initWithInputPath:@"advent_2017/day_01"], 34 | //[[A17D02 alloc] initWithInputPath:@"advent_2017/day_02"], 35 | //[[A17D03 alloc] initWithInputPath:nil], 36 | //[[A17D04 alloc] initWithInputPath:@"advent_2017/day_04"], 37 | //[[A17D05 alloc] initWithInputPath:@"advent_2017/day_05"], 38 | //[[A17D06 alloc] initWithInputPath:@"advent_2017/day_06"], 39 | //[[A17D07 alloc] initWithInputPath:@"advent_2017/day_07"], 40 | //[[A17D08 alloc] initWithInputPath:@"advent_2017/day_08"], 41 | //[[A17D09 alloc] initWithInputPath:@"advent_2017/day_09"], 42 | //[[A17D10 alloc] initWithInputPath:nil], 43 | //[[A17D11 alloc] initWithInputPath:@"advent_2017/day_11"], 44 | //[[A17D12 alloc] initWithInputPath:@"advent_2017/day_12"], 45 | //[[A17D13 alloc] initWithInputPath:@"advent_2017/day_13"], 46 | //[[A17D14 alloc] initWithInputPath:nil], 47 | //[[A17D15 alloc] initWithInputPath:nil], 48 | //[[A17D16 alloc] initWithInputPath:@"advent_2017/day_16"], 49 | //[[A17D17 alloc] initWithInputPath:nil], 50 | //[[A17D18 alloc] initWithInputPath:@"advent_2017/day_18"], 51 | [[A17D19 alloc] initWithInputPath:@"advent_2017/day_19"], 52 | //[[A17D20 alloc] initWithInputPath:@"advent_2017/day_20"], 53 | //[[A17D21 alloc] initWithInputPath:@"advent_2017/day_21"], 54 | //[[A17D22 alloc] initWithInputPath:@"advent_2017/day_22"], 55 | //[[A17D23 alloc] initWithInputPath:@"advent_2017/day_23"], 56 | //[[A17D24 alloc] initWithInputPath:@"advent_2017/day_24"], 57 | //[[A17D25 alloc] initWithInputPath:@"advent_2017/day_25"], 58 | ]; 59 | 60 | [problems makeObjectsPerformSelector:@selector(solve)]; 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /src/advent_2017/A17D14.m: -------------------------------------------------------------------------------- 1 | #import "A17D14.h" 2 | #import "A17D10.h" 3 | #import "A17D12.h" 4 | 5 | NS_ASSUME_NONNULL_BEGIN 6 | 7 | @implementation A17D14 8 | 9 | - (NSString *)input { 10 | return @"ljoxqyyw"; 11 | } 12 | 13 | - (NSUInteger)countOneBits:(NSInteger)number { 14 | NSUInteger count = 0; 15 | while (number) { 16 | count += number & 1; 17 | number >>= 1; 18 | } 19 | return count; 20 | } 21 | 22 | - (NSArray *> *)knotHashMatrix:(NSString *)input { 23 | NSMutableArray *> *result = [[NSMutableArray alloc] init]; 24 | A17D10 *a17d10 = [[A17D10 alloc] initWithInputPath:nil]; 25 | for (NSUInteger x = 0; x < 128; x++) { 26 | [result addObject:[a17d10 knotHashDecimal:[NSString stringWithFormat:@"%@-%lu", input, (unsigned long)x]]]; 27 | } 28 | return [result copy]; 29 | } 30 | 31 | - (nullable id)part1 { 32 | NSUInteger result = 0; 33 | NSArray *> *matrix = [self knotHashMatrix:self.input]; 34 | for (NSArray *arr in matrix) { 35 | for (NSNumber *number in arr) { 36 | result += [self countOneBits:number.integerValue]; 37 | } 38 | } 39 | return @(result); 40 | } 41 | 42 | - (BOOL)bitTestValue:(NSUInteger)x bitPos:(NSUInteger)n { 43 | NSUInteger mask = 1; 44 | while (n--) { 45 | mask <<= 1; 46 | } 47 | return x & mask; 48 | } 49 | 50 | - (BOOL)bitTestMatrix:(NSArray *> *)matrix row:(NSUInteger)row col:(NSUInteger)col { 51 | return [self bitTestValue:matrix[row][col / 8].integerValue bitPos:7 - (col % 8)]; 52 | } 53 | 54 | - (NSInteger)linearIndexRow:(NSInteger)row col:(NSInteger)col { 55 | return row * 128 + col; 56 | } 57 | 58 | - (nullable id)part2 { 59 | NSArray *> *matrix = [self knotHashMatrix:self.input]; 60 | UFStructure *uf = [[UFStructure alloc] init]; 61 | for (NSUInteger row = 0; row < 128; row++) { 62 | for (NSUInteger col = 0; col < 128; col++) { 63 | if (![self bitTestMatrix:matrix row:row col:col]) { 64 | continue; 65 | } 66 | for (NSArray *offset in @[@[@0, @0], @[@-1, @0], @[@1, @0], @[@0, @-1], @[@0, @1]]) { 67 | NSInteger neighRow = row + offset[0].integerValue; 68 | NSInteger neighCol = col + offset[1].integerValue; 69 | if (!(0 <= neighRow && neighRow < 128 && 70 | 0 <= neighCol && neighCol < 128 && 71 | [self bitTestMatrix:matrix row:neighRow col:neighCol])) { 72 | continue; 73 | } 74 | [uf union:[self linearIndexRow:row col:col] with:[self linearIndexRow:neighRow col:neighCol]]; 75 | } 76 | } 77 | } 78 | return @([uf distinctSetCount]); 79 | } 80 | 81 | @end 82 | 83 | NS_ASSUME_NONNULL_END 84 | -------------------------------------------------------------------------------- /src/advent_2017/day_23.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-23 2 | (:require 3 | #?(:cljs [planck.core :refer [read]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.spec.alpha :as s]) 6 | #?(:clj (:import (java.io PushbackReader)))) 7 | 8 | (def input (->> "advent_2017/day_23/input" io/resource io/reader #?(:clj PushbackReader.))) 9 | 10 | (s/def ::arg (s/or :sym simple-symbol? :val int?)) 11 | (s/def ::instr (s/cat :op '#{set add sub mul mod jnz} :arg1 ::arg :arg2 ::arg)) 12 | 13 | (s/check-asserts true) 14 | (def data (->> (repeatedly #(read {:eof nil} input)) 15 | (take-while some?) 16 | (s/assert (s/* ::instr)) 17 | (s/conform (s/* ::instr)))) 18 | 19 | (defn step* [program {:keys [ip regs] :as state}] 20 | (if (contains? program ip) 21 | (let [{:keys [op arg1 arg2]} (program ip) ; fetch 22 | state (update state :ip inc) 23 | eval (fn [[kind x]] 24 | (cond-> x (= :sym kind) (regs 0))) 25 | mutate (fn [f] 26 | (let [reg (second arg1)] 27 | (-> state 28 | (update-in [:regs reg] (fnil f 0) (eval arg2)) 29 | (cond-> (= reg (:break-write state)) (assoc :break state)))))] 30 | (-> (case op 31 | set (mutate (fn [a b] b)) 32 | add (mutate +) 33 | sub (mutate -) 34 | mul (mutate *) 35 | jnz (cond-> state (not (zero? (eval arg1))) (assoc :ip (+ ip (eval arg2))))) 36 | (cond-> (= (:count-op state) op) (update-in [:regs 'h] (fnil inc 0))))) 37 | (throw (ex-info "jumped out" {:state state})))) 38 | 39 | (defn part-1 [] 40 | (let [step (partial step* data)] 41 | ('h (->> (iterate step {:ip 0 42 | :regs {} 43 | :break-write 'h 44 | :count-op 'mul}) 45 | (some :break) 46 | :regs)))) 47 | 48 | (comment 49 | ;; Observe b set to 108100 and c to 125100 for this chunk of code 50 | (try 51 | (let [step (partial step* (subvec data 0 8))] 52 | (doall (iterate step {:ip 0 53 | :regs {'a 1}}))) 54 | (catch #?(:clj Throwable :cljs :default) e 55 | (ex-data e))) 56 | 57 | ;; Observe f set to 1 iff b is prime for this chunk of code 58 | (try 59 | (let [step (partial step* (subvec data 8 24))] 60 | (doall (iterate step {:ip 0 61 | :regs {'b 19}}))) 62 | (catch #?(:clj Throwable :cljs :default) e 63 | (ex-data e)))) 64 | 65 | ;; We could optimize code, or just calculate what it would do. Choose latter. 66 | 67 | (defn prime? [n] 68 | (and (odd? n) 69 | (let [root (int (Math/sqrt n))] 70 | (loop [i 3] 71 | (or (> i root) 72 | (and (not (zero? (mod n i))) 73 | (recur (+ i 2)))))))) 74 | 75 | (defn part-2 [] 76 | (count (remove prime? (range 108100 125101 17)))) -------------------------------------------------------------------------------- /src/advent_2018/day_06.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-06 2 | (:require 3 | #?(:cljs [planck.core :refer [line-seq read-string]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io])) 5 | 6 | (def input-lines (->> "advent_2018/day_06/input" io/resource io/reader line-seq)) 7 | 8 | (defn read-coordinates [s] 9 | (mapv read-string (re-seq #"\d+" s))) 10 | 11 | (defn add-vectors [& xs] 12 | (apply mapv + xs)) 13 | 14 | (defn unique-min-key 15 | [k & xs] 16 | (first (reduce (fn [[min-x min dup?] x] 17 | (let [k-x (k x)] 18 | (cond 19 | (< k-x min) [x k-x false] 20 | (== k-x min) [nil k-x true] 21 | :else [min-x min dup?]))) 22 | [nil ##Inf false] xs))) 23 | 24 | (defn manhattan-distance [a b] 25 | (reduce + (map (comp #(Math/abs ^long %) -) a b))) 26 | 27 | (defn boundary [center extent] 28 | (let [size (* 2 extent)] 29 | (into #{(add-vectors center [extent extent])} 30 | (mapcat (fn [n] 31 | (map #(add-vectors center [(- extent) (- extent)] %) 32 | [[0 n] [size n] [n 0] [n size]])) 33 | (range 0 size))))) 34 | 35 | (defn coordinate-stats [coordinates] 36 | (let [xs (map first coordinates) 37 | ys (map second coordinates) 38 | min-x (apply min xs) 39 | min-y (apply min ys) 40 | max-x (apply max xs) 41 | max-y (apply max ys) 42 | mid (fn [min max] (quot (+ min max) 2)) 43 | span (fn [min max] (- max min))] 44 | [[(mid min-x max-x) (mid min-y max-y)] 45 | [(span min-x max-x) (span min-y max-y)]])) 46 | 47 | (defn part-1 [] 48 | (let [coordinates (map read-coordinates input-lines) 49 | [midpoint span] (coordinate-stats coordinates) 50 | terminal-extent (+ 2 (quot (apply max span) 2)) 51 | closest-coord (fn [coord] 52 | (apply unique-min-key #(manhattan-distance coord %) coordinates))] 53 | (reduce 54 | (fn [coord->area extent] 55 | (let [closest-coords (map closest-coord (boundary midpoint extent)) 56 | coord->area' (merge-with + coord->area (frequencies closest-coords))] 57 | (if (== terminal-extent extent) 58 | (reduced (apply max (vals (apply dissoc coord->area' closest-coords)))) 59 | coord->area'))) 60 | {} 61 | (range)))) 62 | 63 | (defn part-2 [] 64 | (let [coordinates (map read-coordinates input-lines) 65 | [midpoint] (coordinate-stats coordinates) 66 | distance-sum (fn [coord] 67 | (reduce + (map #(manhattan-distance coord %) coordinates))) 68 | within-region? (fn [coord] 69 | (< (distance-sum coord) 10000))] 70 | (reduce (fn [area extent] 71 | (let [additional-area (count (filter within-region? (boundary midpoint extent)))] 72 | (if (zero? additional-area) 73 | (reduced area) 74 | (+ area additional-area)))) 75 | 0 76 | (range)))) 77 | -------------------------------------------------------------------------------- /src/advent_2017/A17D16.m: -------------------------------------------------------------------------------- 1 | #import "A17D16.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @interface A17D16 () 6 | 7 | @property (nonatomic, readonly) NSArray *danceMoves; 8 | 9 | @end 10 | 11 | @implementation A17D16 { 12 | NSArray *_danceMoves; 13 | } 14 | 15 | - (id)parseDanceMove:(NSString *)s { 16 | NSString *suffix = [s substringFromIndex:1]; 17 | if ([s hasPrefix:@"s"]) { 18 | NSInteger arg1 = suffix.integerValue; 19 | return ^(NSMutableArray *xs) { 20 | NSArray *copy = [xs copy]; 21 | NSUInteger c = xs.count; 22 | for (NSUInteger idx = 0; idx < c; idx++) { 23 | xs[idx] = copy[(idx + (c - arg1)) % c]; 24 | } 25 | }; 26 | } else if ([s hasPrefix:@"x"]) { 27 | NSUInteger slashIndex = [suffix rangeOfString:@"/"].location; 28 | NSInteger arg1 = [suffix substringToIndex:slashIndex].integerValue; 29 | NSInteger arg2 = [suffix substringFromIndex:slashIndex + 1].integerValue; 30 | return ^(NSMutableArray *xs) { 31 | id tmp = xs[arg1]; 32 | xs[arg1] = xs[arg2]; 33 | xs[arg2] = tmp; 34 | }; 35 | } else { 36 | NSString *s1 = [suffix substringToIndex:1]; 37 | NSString *s2 = [suffix substringFromIndex:2]; 38 | return ^(NSMutableArray *xs) { 39 | NSInteger arg1 = [xs indexOfObject:s1]; 40 | NSInteger arg2 = [xs indexOfObject:s2]; 41 | id tmp = xs[arg1]; 42 | xs[arg1] = xs[arg2]; 43 | xs[arg2] = tmp; 44 | }; 45 | } 46 | } 47 | 48 | - (NSArray *)danceMoves { 49 | if (!_danceMoves) { 50 | NSMutableArray *moves = [[NSMutableArray alloc] init]; 51 | for (NSString *move in [self.input componentsSeparatedByString:@","]) { 52 | [moves addObject:[self parseDanceMove:move]]; 53 | } 54 | _danceMoves = [moves copy]; 55 | } 56 | return _danceMoves; 57 | } 58 | 59 | - (NSArray *)dance:(NSArray *)xs { 60 | NSMutableArray *arr = [xs mutableCopy]; 61 | for (void (^move)(NSMutableArray *) in self.danceMoves) { 62 | move(arr); 63 | } 64 | return arr; 65 | } 66 | 67 | - (NSArray *)startingPositions { 68 | return @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p"]; 69 | } 70 | 71 | - (nullable id)part1 { 72 | return [[self dance:[self startingPositions]] componentsJoinedByString:@""]; 73 | } 74 | 75 | - (nullable id)part2 { 76 | NSArray *positions = [self startingPositions]; 77 | NSArray *positions0 = positions; 78 | NSUInteger period = 0; 79 | for (;;) { 80 | positions = [self dance:positions]; 81 | period++; 82 | if ([positions isEqual:positions0]) { 83 | break; 84 | } 85 | } 86 | 87 | positions = [self startingPositions]; 88 | for (NSUInteger n = 0; n < 1000000000 % period; n++) { 89 | positions = [self dance:positions]; 90 | } 91 | 92 | return [positions componentsJoinedByString:@""]; 93 | } 94 | 95 | @end 96 | 97 | NS_ASSUME_NONNULL_END 98 | -------------------------------------------------------------------------------- /advent.xcodeproj/xcshareddata/xcschemes/advent.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 45 | 47 | 53 | 54 | 55 | 56 | 62 | 64 | 70 | 71 | 72 | 73 | 75 | 76 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /src/advent_2018/day_13.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-13 2 | (:require 3 | [advent.util :refer [some']] 4 | #?(:cljs [planck.core :refer [slurp]]) 5 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 6 | [clojure.string :as string])) 7 | 8 | (def input (-> "advent_2018/day_13/input" io/resource slurp)) 9 | 10 | (def tracks #{\/ \\ \+}) 11 | 12 | (def dirs #{\v \^ \< \>}) 13 | 14 | (defn parse-lines [kind lines] 15 | (into {} (mapcat (fn [y line] 16 | (keep-indexed (fn [x c] 17 | (when (kind c) 18 | [[x y] c])) 19 | line)) 20 | (range) lines))) 21 | 22 | (def turns (cycle [:left :straight :right])) 23 | 24 | (defn init-carts [loc->dir] 25 | (reduce-kv (fn [m loc dir] 26 | (assoc m loc [dir turns])) 27 | (sorted-map) 28 | loc->dir)) 29 | 30 | (defn update-loc [[x y] dir] 31 | (case dir 32 | \< [(dec x) y] 33 | \> [(inc x) y] 34 | \^ [x (dec y)] 35 | \v [x (inc y)])) 36 | 37 | (defn move-cart [[_ [dir] :as cart]] 38 | (update cart 0 update-loc dir)) 39 | 40 | (defn rotate [dir turn] 41 | (case [dir turn] 42 | [\< :left] \v 43 | [\v :left] \> 44 | [\> :left] \^ 45 | [\^ :left] \< 46 | [\< :right] \^ 47 | [\v :right] \< 48 | [\> :right] \v 49 | [\^ :right] \> 50 | dir)) 51 | 52 | (defn update-dir+turns [[dir [turn & more :as turns]] track] 53 | (case [dir track] 54 | ([\> \\] [\< \/]) [\v turns] 55 | ([\> \/] [\< \\]) [\^ turns] 56 | ([\^ \\] [\v \/]) [\< turns] 57 | ([\^ \/] [\v \\]) [\> turns] 58 | ([\> \+] [\< \+] [\^ \+] [\v \+]) [(rotate dir turn) more] 59 | [dir turns])) 60 | 61 | (defn turn-cart [loc->track [loc :as cart]] 62 | (update cart 1 update-dir+turns (loc->track loc))) 63 | 64 | (defn update-carts-1 [loc->track carts] 65 | (reduce (fn [carts [old-loc :as cart]] 66 | (let [[new-loc :as cart] (turn-cart loc->track (move-cart cart))] 67 | (if (contains? carts new-loc) 68 | (reduced new-loc) 69 | (-> carts 70 | (conj cart) 71 | (dissoc old-loc))))) 72 | carts 73 | carts)) 74 | 75 | (defn solve [extract some-pred advance] 76 | (let [loc->track (parse-lines tracks (string/split-lines input)) 77 | carts (init-carts (parse-lines dirs (string/split-lines input)))] 78 | (string/join "," (extract (some' some-pred (iterate (partial advance loc->track) carts)))))) 79 | 80 | (defn part-1 [] 81 | (solve identity #(when (vector? %) %) update-carts-1)) 82 | 83 | (defn update-carts-2 [loc->track carts] 84 | (second (reduce (fn [[removed carts] [old-loc :as cart]] 85 | (let [[new-loc :as cart] (turn-cart loc->track (move-cart cart))] 86 | (cond 87 | (removed old-loc) 88 | [removed carts] 89 | 90 | (contains? carts new-loc) 91 | [(conj removed new-loc) 92 | (-> carts 93 | (dissoc new-loc) 94 | (dissoc old-loc))] 95 | 96 | :else 97 | [removed 98 | (-> carts 99 | (conj cart) 100 | (dissoc old-loc))]))) 101 | [#{} carts] 102 | carts))) 103 | 104 | (defn part-2 [] 105 | (solve ffirst #(when (= 1 (count %)) %) update-carts-2)) 106 | -------------------------------------------------------------------------------- /src/advent_2017/day_18.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2017.day-18 2 | (:require 3 | #?(:cljs [planck.core :refer [read]]) 4 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 5 | [clojure.spec.alpha :as s]) 6 | #?(:clj (:import (java.io PushbackReader)))) 7 | 8 | (def input (->> "advent_2017/day_18/input" io/resource io/reader #?(:clj PushbackReader.))) 9 | 10 | (s/def ::arg (s/or :sym simple-symbol? :val int?)) 11 | (s/def ::instr (s/alt :unary (s/cat :op '#{snd rcv} :arg1 ::arg) 12 | :binary (s/cat :op '#{set add mul mod jgz} :arg1 ::arg :arg2 ::arg))) 13 | 14 | (s/check-asserts true) 15 | (def data (->> (repeatedly #(read {:eof nil} input)) 16 | (take-while some?) 17 | (s/assert (s/* ::instr)) 18 | (s/conform (s/* ::instr)) 19 | (map val) 20 | vec)) 21 | 22 | (defn step* [snd-fn rcv-fn {:keys [ip regs] :as state}] 23 | (let [{:keys [op arg1 arg2]} (data ip) ; fetch 24 | state (update state :ip inc) 25 | eval (fn [[kind x]] 26 | (cond-> x (= :sym kind) (regs 0))) 27 | mutate (fn [f] 28 | (update-in state [:regs (second arg1)] (fnil f 0) (eval arg2)))] 29 | (case op 30 | snd (snd-fn state (eval arg1)) 31 | set (mutate (fn [a b] b)) 32 | add (mutate +) 33 | mul (mutate *) 34 | mod (mutate mod) 35 | rcv (rcv-fn state (eval arg1) (second arg1)) 36 | jgz (cond-> state (pos? (eval arg1)) (assoc :ip (+ ip (eval arg2))))))) 37 | 38 | (defn part-1 [] 39 | (let [snd-fn (fn [state v] 40 | (assoc-in state [:flags :last-snd] v)) 41 | rcv-fn (fn [state prior-val reg] 42 | (if (zero? prior-val) 43 | state 44 | (let [last-snd (get-in state [:flags :last-snd])] 45 | (-> state 46 | (assoc-in [:regs reg] last-snd) 47 | (assoc-in [:flags :rcv] last-snd))))) 48 | step (partial step* snd-fn rcv-fn)] 49 | (->> (iterate step {:ip 0 :regs {}}) 50 | (some (fn [state] (get-in state [:flags :rcv])))))) 51 | 52 | (defn part-2 [] 53 | (let [queue-0 (volatile! #?(:clj clojure.lang.PersistentQueue/EMPTY :cljs #queue [])) 54 | queue-1 (volatile! #?(:clj clojure.lang.PersistentQueue/EMPTY :cljs #queue [])) 55 | snd-fn (fn [{:keys [snd-queue] :as state} v] 56 | (vswap! snd-queue conj v) 57 | (update state :snd-count (fnil inc 0))) 58 | rcv-fn (fn [{:keys [rcv-queue] :as state} prior-val reg] 59 | (if-let [val (peek @rcv-queue)] 60 | (do (vswap! rcv-queue pop) 61 | (-> state 62 | (assoc-in [:regs reg] val) 63 | (assoc :need-input? false))) 64 | (-> state 65 | (update :ip dec) 66 | (assoc :need-input? true)))) 67 | step (partial step* snd-fn rcv-fn) 68 | blocked? (fn [state] 69 | (and (:need-input? state) (empty? @(:rcv-queue state)))) 70 | deadlocked? (fn [& states] 71 | (every? blocked? states))] 72 | (loop [state-0 {:ip 0 :regs {'p 0} :snd-queue queue-1 :rcv-queue queue-0} 73 | state-1 {:ip 0 :regs {'p 1} :snd-queue queue-0 :rcv-queue queue-1}] 74 | (cond 75 | (deadlocked? state-0 state-1) (:snd-count state-1) 76 | (blocked? state-0) (recur state-0 (step state-1)) 77 | :else (recur (step state-0) state-1))))) 78 | -------------------------------------------------------------------------------- /src/advent_2018/day_16.cljc: -------------------------------------------------------------------------------- 1 | (ns advent-2018.day-16 2 | (:refer-clojure :exclude [defmacro]) 3 | (:require 4 | [advent.util :refer [map-vals fixed-point]] 5 | #?(:cljs [planck.core :refer [slurp read-string]]) 6 | [#?(:clj clojure.java.io :cljs planck.io) :as io] 7 | [chivorcam.core :refer [defmacro]] 8 | [clojure.set :as set] 9 | [clojure.string :as string])) 10 | 11 | (def input (-> "advent_2018/day_16/input" io/resource slurp)) 12 | 13 | (defn oprr [op registers a b c] 14 | (assoc registers c (op (registers a) (registers b)))) 15 | 16 | (defn opri [op registers a b c] 17 | (assoc registers c (op (registers a) b))) 18 | 19 | (defn opir [op registers a b c] 20 | (assoc registers c (op a (registers b)))) 21 | 22 | (defmacro defmicrocode [opcode n op] 23 | `(defmethod ~'microcode ~opcode ~'[_ registers a b c] 24 | (~(case (take-last n (name opcode)) 25 | [\r] `oprr 26 | [\i] `opri 27 | [\r \r] `oprr 28 | [\r \i] `opri 29 | [\i \r] `opir) 30 | ~op ~'registers ~'a ~'b ~'c))) 31 | 32 | (defmulti microcode (fn [opcode registers a b c] opcode)) 33 | 34 | (defn bool->num [b] 35 | (if b 1 0)) 36 | 37 | (defmicrocode :addr 1 +) 38 | (defmicrocode :addi 1 +) 39 | (defmicrocode :mulr 1 *) 40 | (defmicrocode :muli 1 *) 41 | (defmicrocode :banr 1 bit-and) 42 | (defmicrocode :bani 1 bit-and) 43 | (defmicrocode :borr 1 bit-or) 44 | (defmicrocode :bori 1 bit-or) 45 | (defmicrocode :setr 1 (fn [a b] a)) 46 | (defmicrocode :setir 2 (fn [a b] a)) 47 | (defmicrocode :gtir 2 (comp bool->num >)) 48 | (defmicrocode :gtri 2 (comp bool->num >)) 49 | (defmicrocode :gtrr 2 (comp bool->num >)) 50 | (defmicrocode :eqir 2 (comp bool->num =)) 51 | (defmicrocode :eqri 2 (comp bool->num =)) 52 | (defmicrocode :eqrr 2 (comp bool->num =)) 53 | 54 | (def opcodes (keys (methods microcode))) 55 | 56 | (defn parse-examples [input] 57 | (->> (re-seq #"(?m)Before: \[(\d), (\d), (\d), (\d)\]\n(\d+) (\d) (\d) (\d)\nAfter: \[(\d), (\d), (\d), (\d)\]\n" input) 58 | (map #(map read-string (rest %))) 59 | (map #(zipmap [:before :instr :after] (partition 4 %))))) 60 | 61 | (defn opcode-num+matching-opcodes [{:keys [before instr after]}] 62 | (let [[opcode-num a b c] instr] 63 | [opcode-num (set (filter (fn [opcode] 64 | (= after (microcode opcode (vec before) a b c))) 65 | opcodes))])) 66 | 67 | (defn part-1 [] 68 | (->> (parse-examples input) 69 | (map opcode-num+matching-opcodes) 70 | (filter #(<= 3 (count (second %)))) 71 | count)) 72 | 73 | (defn subtract-singletons [m] 74 | (let [singleton? #(= 1 (count %)) 75 | singleton-vals (into #{} (map first (filter singleton? (vals m))))] 76 | (reduce-kv (fn [m k v] 77 | (if (singleton? v) 78 | (assoc m k v) 79 | (assoc m k (set/difference v singleton-vals)))) 80 | {} 81 | m))) 82 | 83 | (defn part-2 [] 84 | (let [examples (parse-examples input) 85 | opcode-num->opcodes (->> examples 86 | (map opcode-num+matching-opcodes) 87 | (map #(apply hash-map %)) 88 | (apply merge-with set/intersection)) 89 | opcode-num->opcode (->> opcode-num->opcodes 90 | (fixed-point subtract-singletons) 91 | (map-vals first)) 92 | program (->> (string/split-lines input) 93 | (drop (+ (* 4 (count examples)) 2)) 94 | (map #(map read-string (re-seq #"\d+" %))))] 95 | (first (reduce (fn [registers [numeric-opcode a b c]] 96 | (microcode (opcode-num->opcode numeric-opcode) registers a b c)) 97 | [0 0 0 0] 98 | program)))) 99 | -------------------------------------------------------------------------------- /src/advent_2017/A17D10.m: -------------------------------------------------------------------------------- 1 | #import "A17D10.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @implementation A17D10 6 | 7 | - (NSString *)input { 8 | return @"129,154,49,198,200,133,97,254,41,6,2,1,255,0,191,108"; 9 | } 10 | 11 | - (NSArray *)roundWithLengths:(NSArray *)lengths 12 | xs:(NSMutableArray *)xs 13 | currentPos:(NSUInteger)currentPos 14 | skipSize:(NSUInteger)skipSize { 15 | for (NSNumber *length in lengths) { 16 | NSMutableArray *arr = [[NSMutableArray alloc] init]; 17 | for (NSUInteger ndx = 0; ndx < 256; ndx++) { 18 | NSNumber *x = xs[(ndx + currentPos) % 256]; 19 | if (ndx < length.integerValue) { 20 | [arr insertObject:x atIndex:0]; 21 | } else { 22 | [arr addObject:x]; 23 | } 24 | } 25 | [xs removeAllObjects]; 26 | for (NSUInteger ndx = 0; ndx < 256; ndx++) { 27 | [xs addObject:arr[(ndx + 256 - currentPos) % 256]]; 28 | } 29 | currentPos = (currentPos + length.integerValue + skipSize) % 256; 30 | skipSize = (skipSize + 1) % 256; 31 | } 32 | return @[xs, @(currentPos), @(skipSize)]; 33 | } 34 | 35 | - (NSArray *)getInitialXs { 36 | NSMutableArray *xs = [[NSMutableArray alloc] init]; 37 | for (NSUInteger ndx = 0; ndx < 256; ndx++) { 38 | [xs addObject:@(ndx)]; 39 | } 40 | return [xs copy]; 41 | } 42 | 43 | - (nullable id)part1 { 44 | NSMutableArray *xs = [[self getInitialXs] mutableCopy]; 45 | NSMutableArray *lengths = [[NSMutableArray alloc] init]; 46 | for (NSString *s in [self.input componentsSeparatedByString:@","]) { 47 | [lengths addObject:@(s.integerValue)]; 48 | } 49 | NSArray *result = [self roundWithLengths:lengths 50 | xs:xs 51 | currentPos:0 52 | skipSize:0]; 53 | xs = result[0]; 54 | return @(xs[0].integerValue * xs[1].integerValue); 55 | } 56 | 57 | - (NSArray *)knotHashDecimal:(NSString *)s { 58 | NSMutableArray *lengths = [[NSMutableArray alloc] init]; 59 | [s enumerateSubstringsInRange:NSMakeRange(0, s.length) options:NSStringEnumerationByComposedCharacterSequences 60 | usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 61 | [lengths addObject:@([substring characterAtIndex:0])]; 62 | }]; 63 | [lengths addObjectsFromArray:@[@17, @31, @73, @47, @23]]; 64 | NSMutableArray *xs = [[self getInitialXs] mutableCopy]; 65 | NSUInteger currentPos = 0; 66 | NSUInteger skipSize = 0; 67 | for (NSUInteger x = 0; x < 64; x++) { 68 | NSArray *result = [self roundWithLengths:lengths 69 | xs:xs 70 | currentPos:currentPos 71 | skipSize:skipSize]; 72 | xs = result[0]; 73 | currentPos = ((NSNumber *)result[1]).integerValue; 74 | skipSize = ((NSNumber *)result[2]).integerValue; 75 | } 76 | NSMutableArray *result = [[NSMutableArray alloc] init]; 77 | NSUInteger reduced = 0; 78 | for (NSUInteger idx = 0; idx < xs.count; idx++) { 79 | reduced ^= xs[idx].integerValue; 80 | if ((idx + 1) % 16 == 0) { 81 | [result addObject:@(reduced)]; 82 | reduced = 0; 83 | } 84 | } 85 | return [result copy]; 86 | } 87 | 88 | - (NSString *)knotHash:(NSString *)s { 89 | NSMutableString *result = [[NSMutableString alloc] init]; 90 | for (NSNumber *number in [self knotHashDecimal:s]) { 91 | [result appendFormat:@"%02lx", (long)number.integerValue]; 92 | } 93 | return [result copy]; 94 | } 95 | 96 | - (nullable id)part2 { 97 | return [self knotHash:self.input]; 98 | } 99 | 100 | @end 101 | 102 | NS_ASSUME_NONNULL_END 103 | -------------------------------------------------------------------------------- /resources/advent_2017/day_21/input: -------------------------------------------------------------------------------- 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 | .../###/... => ##.#/..##/...#/..## 38 | #../###/... => ..##/####/..#./.### 39 | .#./###/... => #.##/#.##/.##./..## 40 | ##./###/... => #.../.#.#/####/..## 41 | #.#/###/... => #.../.###/..../.### 42 | ###/###/... => .##./####/##../..#. 43 | ..#/.../#.. => #..#/.###/.#.#/##.# 44 | #.#/.../#.. => ###./.##./.##./##.. 45 | .##/.../#.. => .###/.#../...#/.#.# 46 | ###/.../#.. => ###./..##/..##/.#.# 47 | .##/#../#.. => ##.#/...#/####/#.## 48 | ###/#../#.. => .#.#/...#/.###/#..# 49 | ..#/.#./#.. => #.#./.###/##../#... 50 | #.#/.#./#.. => ####/..#./.###/##.. 51 | .##/.#./#.. => #.#./##../..../#.#. 52 | ###/.#./#.. => .#.#/#.#./#.../#.#. 53 | .##/##./#.. => ##../.#../...#/..#. 54 | ###/##./#.. => ##../#.../.###/..#. 55 | #../..#/#.. => ##../####/##.#/#.## 56 | .#./..#/#.. => #..#/..../..#./#... 57 | ##./..#/#.. => ..#./..##/#.##/#.## 58 | #.#/..#/#.. => #.##/..#./.#.#/.#.. 59 | .##/..#/#.. => ###./##../.#.#/##.. 60 | ###/..#/#.. => #.#./.#.#/.#.#/#..# 61 | #../#.#/#.. => #..#/.#.#/####/.#.# 62 | .#./#.#/#.. => #.../#.##/#.../#.#. 63 | ##./#.#/#.. => .##./.#../.#.#/..#. 64 | ..#/#.#/#.. => ##.#/.###/#..#/#... 65 | #.#/#.#/#.. => .#.#/.###/#..#/.#.. 66 | .##/#.#/#.. => ..#./####/.#../...# 67 | ###/#.#/#.. => .###/.#../.##./.#.# 68 | #../.##/#.. => ..##/##.#/#.#./.### 69 | .#./.##/#.. => ####/.##./..../.##. 70 | ##./.##/#.. => ...#/##../..##/..## 71 | #.#/.##/#.. => .###/##.#/.###/..#. 72 | .##/.##/#.. => ..#./##../..##/...# 73 | ###/.##/#.. => ###./.#.#/.###/.### 74 | #../###/#.. => .##./##.#/##.#/..#. 75 | .#./###/#.. => ...#/...#/##.#/#.## 76 | ##./###/#.. => .#../.#.#/.#.#/..#. 77 | ..#/###/#.. => ####/.#.#/..../##.# 78 | #.#/###/#.. => ..../.###/.##./#.#. 79 | .##/###/#.. => #.#./..##/.##./##.. 80 | ###/###/#.. => .###/##.#/#.#./#.## 81 | .#./#.#/.#. => ...#/###./..../#### 82 | ##./#.#/.#. => ..../###./#.##/..## 83 | #.#/#.#/.#. => #.../###./##.#/#... 84 | ###/#.#/.#. => #.../##../..#./..#. 85 | .#./###/.#. => ###./..../.#.#/..#. 86 | ##./###/.#. => ##.#/..../.##./###. 87 | #.#/###/.#. => #.##/##../...#/.... 88 | ###/###/.#. => .##./####/##../.#.. 89 | #.#/..#/##. => .#.#/#.#./##.#/#.## 90 | ###/..#/##. => ####/##../..##/#### 91 | .##/#.#/##. => .#.#/#..#/####/##.. 92 | ###/#.#/##. => #.##/.#../.###/.#.. 93 | #.#/.##/##. => ...#/.#.#/#.#./.... 94 | ###/.##/##. => ..#./#.#./.###/###. 95 | .##/###/##. => .###/.###/.##./.#.. 96 | ###/###/##. => #.../#.../#.##/.#.. 97 | #.#/.../#.# => ..#./..../##../#.## 98 | ###/.../#.# => ..#./#.##/####/...# 99 | ###/#../#.# => #.../###./#.../...# 100 | #.#/.#./#.# => ..##/#.##/.#.#/.#.. 101 | ###/.#./#.# => #.../.#.#/#.#./##.. 102 | ###/##./#.# => ##../.###/.#../...# 103 | #.#/#.#/#.# => ..##/#.#./#.##/##.. 104 | ###/#.#/#.# => .###/..##/..#./.### 105 | #.#/###/#.# => ##.#/.###/..../.### 106 | ###/###/#.# => ##.#/#.##/##../..#. 107 | ###/#.#/### => ##../.#../#.#./##.# 108 | ###/###/### => .##./##../..#./.### 109 | -------------------------------------------------------------------------------- /src/advent_2017/A17D03.m: -------------------------------------------------------------------------------- 1 | #import "A17D03.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @interface Location : NSObject 6 | 7 | @property (nonatomic, assign, readonly) int x; 8 | @property (nonatomic, assign, readonly) int y; 9 | @property (nonatomic, readonly) int distance; 10 | 11 | - (instancetype)init NS_UNAVAILABLE; 12 | - (instancetype)initWithX:(int)x y:(int)y NS_DESIGNATED_INITIALIZER; 13 | 14 | @end 15 | 16 | @implementation Location 17 | 18 | @dynamic distance; 19 | 20 | - (instancetype)initWithX:(int)x y:(int)y { 21 | self = [super init]; 22 | if (self) { 23 | _x = x; 24 | _y = y; 25 | } 26 | return self; 27 | } 28 | 29 | - (BOOL)isEqual:(id)object { 30 | if (self == object) return YES; 31 | if ([self class] != [object class]) return NO; 32 | 33 | Location *otherLocation = (Location *)object; 34 | return _x == otherLocation.x && _y == otherLocation.y; 35 | } 36 | 37 | - (NSUInteger)hash { 38 | return 71993 * (_x + 13752) + 933199 * _y; 39 | } 40 | 41 | - (Location *)add:(Location *)other { 42 | return [[Location alloc] initWithX:_x + other.x y:_y + other.y]; 43 | } 44 | 45 | - (NSArray *)addLocations:(NSArray *)others { 46 | NSMutableArray *rv = [[NSMutableArray alloc] initWithCapacity:others.count]; 47 | [others enumerateObjectsUsingBlock:^(Location *other, NSUInteger idx, BOOL *stop) { 48 | [rv addObject:[self add:other]]; 49 | }]; 50 | return [rv copy]; 51 | } 52 | 53 | - (int)distance { 54 | return abs(_x) + abs(_y); 55 | } 56 | 57 | - (NSString *)description { 58 | return [NSString stringWithFormat:@"[%d %d]", _x, _y]; 59 | } 60 | 61 | - (id)copyWithZone:(nullable NSZone *)zone { 62 | return self; 63 | } 64 | 65 | @end 66 | 67 | @implementation A17D03 68 | 69 | - (int)data { 70 | return 368078; 71 | } 72 | 73 | - (NSArray *)candidateLocations:(Location *)location { 74 | return [location addLocations:@[ 75 | [[Location alloc] initWithX:0 y:-1], 76 | [[Location alloc] initWithX:-1 y:0], 77 | [[Location alloc] initWithX:0 y:1], 78 | [[Location alloc] initWithX:1 y:0] 79 | ]]; 80 | } 81 | 82 | - (Location *)nthSpiral:(int)n { 83 | Location *location = [[Location alloc] initWithX:1 y:0]; 84 | 85 | NSMutableSet *usedLocations = [[NSMutableSet alloc] init]; 86 | [usedLocations addObject:[[Location alloc] initWithX:0 y:0]]; 87 | [usedLocations addObject:[[Location alloc] initWithX:1 y:0]]; 88 | 89 | for (int i = 0; i < n; i++) { 90 | NSArray *candidateLocations = [self candidateLocations:location]; 91 | NSUInteger idx = 0; 92 | while (![usedLocations containsObject:candidateLocations[idx]]) { 93 | idx++; 94 | idx %= candidateLocations.count; 95 | } 96 | while ([usedLocations containsObject:candidateLocations[idx]]) { 97 | idx++; 98 | idx %= candidateLocations.count; 99 | } 100 | location = candidateLocations[idx]; 101 | [usedLocations addObject:location]; 102 | } 103 | 104 | return location; 105 | } 106 | 107 | - (Location *)locationForSquare:(int)square { 108 | return [self nthSpiral:square - 2]; 109 | } 110 | 111 | - (nullable id)part1 { 112 | return @([self locationForSquare:[self data]].distance); 113 | } 114 | 115 | - (NSArray *)adjacentLocations:(Location *)location { 116 | return [location addLocations:@[ 117 | [[Location alloc] initWithX:-1 y:1], 118 | [[Location alloc] initWithX:0 y:1], 119 | [[Location alloc] initWithX:1 y:1], 120 | [[Location alloc] initWithX:-1 y:0], 121 | [[Location alloc] initWithX:1 y:0], 122 | [[Location alloc] initWithX:-1 y:-1], 123 | [[Location alloc] initWithX:0 y:-1], 124 | [[Location alloc] initWithX:1 y:-1] 125 | ]]; 126 | } 127 | 128 | - (nullable id)part2 { 129 | NSMutableDictionary *acc = [[NSMutableDictionary alloc] init]; 130 | [acc setObject:@1 forKey:[[Location alloc] initWithX:0 y:0]]; 131 | 132 | int n = 0; 133 | for (;;) { 134 | Location *location = [self nthSpiral:n++]; 135 | int sum = 0; 136 | for (Location *adjacentLocation in [self adjacentLocations:location]) { 137 | sum += acc[adjacentLocation].intValue; 138 | } 139 | if ([self data] < sum) { 140 | return @(sum); 141 | } else { 142 | acc[location] = @(sum); 143 | } 144 | } 145 | return nil; 146 | } 147 | 148 | @end 149 | 150 | NS_ASSUME_NONNULL_END 151 | -------------------------------------------------------------------------------- /src/advent_2017/A17D19.m: -------------------------------------------------------------------------------- 1 | #import "A17D19.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | typedef NS_ENUM(NSUInteger, A17D17Dir) { 6 | A17D17DirDown, 7 | A17D17DirUp, 8 | A17D17DirLeft, 9 | A17D17DirRight, 10 | }; 11 | 12 | @interface A17D19State : NSObject 13 | 14 | @property (nonatomic, assign) A17D17Dir dir; 15 | @property (nonatomic, assign) NSUInteger row; 16 | @property (nonatomic, assign) NSUInteger col; 17 | 18 | @end 19 | 20 | @implementation A17D19State 21 | 22 | - (void)move { 23 | switch (_dir) { 24 | case A17D17DirDown: 25 | _row++; 26 | break; 27 | case A17D17DirUp: 28 | _row--; 29 | break; 30 | case A17D17DirLeft: 31 | _col--; 32 | break; 33 | case A17D17DirRight: 34 | _col++; 35 | break; 36 | } 37 | } 38 | 39 | - (void)updateDir:(NSArray *> *)matrix { 40 | if ([matrix[_row][_col] isEqualToString:@"+"]) { 41 | switch (_dir) { 42 | case A17D17DirDown: 43 | case A17D17DirUp: { 44 | NSString *adjacent = matrix[_row][_col - 1]; 45 | if ([adjacent isEqualToString:@"|"] || [adjacent isEqualToString:@" "]) { 46 | _dir = A17D17DirRight; 47 | } else { 48 | _dir = A17D17DirLeft; 49 | } 50 | break; 51 | } 52 | case A17D17DirLeft: 53 | case A17D17DirRight: { 54 | NSString *adjacent = matrix[_row - 1][_col]; 55 | if ([adjacent isEqualToString:@"-"] || [adjacent isEqualToString:@" "]) { 56 | _dir = A17D17DirDown; 57 | } else { 58 | _dir = A17D17DirUp; 59 | } 60 | break; 61 | } 62 | } 63 | } 64 | } 65 | 66 | @end 67 | 68 | @interface A17D19 () 69 | 70 | @property (nonatomic, copy, readonly) NSArray *> *matrix; 71 | 72 | @end 73 | 74 | @implementation A17D19 { 75 | NSArray *> *_matrix; 76 | } 77 | 78 | - (NSArray *> *)matrix { 79 | if (!_matrix) { 80 | NSMutableArray *tmp = [[NSMutableArray alloc] init]; 81 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 82 | NSMutableArray *tmp2 = [[NSMutableArray alloc] init]; 83 | [line enumerateSubstringsInRange:NSMakeRange(0, line.length) 84 | options:NSStringEnumerationByComposedCharacterSequences 85 | usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 86 | [tmp2 addObject:substring]; 87 | }]; 88 | [tmp addObject:[tmp2 copy]]; 89 | }]; 90 | _matrix = [tmp copy]; 91 | } 92 | return _matrix; 93 | } 94 | 95 | - (NSArray *)path { 96 | A17D19State *state = [[A17D19State alloc] init]; 97 | state.dir = A17D17DirDown; 98 | state.row = 0; 99 | [self.matrix[0] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) { 100 | if ([obj isEqualToString:@"|"]) { 101 | state.col = idx; 102 | *stop = YES; 103 | } 104 | }]; 105 | 106 | NSMutableArray *result = [[NSMutableArray alloc] init]; 107 | for (;;) { 108 | [state move]; 109 | [state updateDir:self.matrix]; 110 | [result addObject:@[@(state.row), @(state.col)]]; 111 | if ([self.matrix[state.row][state.col] isEqualToString:@" "]) { 112 | break; 113 | } 114 | } 115 | return result; 116 | } 117 | 118 | - (nullable id)part1 { 119 | NSMutableSet *letters = [[NSMutableSet alloc] init]; 120 | [@"ABCDEFGHIJKLMNOPQRSTUVWXYZ" enumerateSubstringsInRange:NSMakeRange(0, 26) 121 | options:NSStringEnumerationByComposedCharacterSequences 122 | usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 123 | [letters addObject:substring]; 124 | }]; 125 | NSMutableString *result = [[NSMutableString alloc] init]; 126 | for (NSArray *coords in [self path]) { 127 | NSString *val = self.matrix[coords[0].integerValue][coords[1].integerValue]; 128 | if ([letters containsObject:val]) { 129 | [result appendString:val]; 130 | } 131 | } 132 | return result; 133 | } 134 | 135 | - (nullable id)part2 { 136 | return @([self path].count); 137 | } 138 | 139 | @end 140 | 141 | NS_ASSUME_NONNULL_END 142 | -------------------------------------------------------------------------------- /src/advent_2017/A17D07.m: -------------------------------------------------------------------------------- 1 | #import "A17D07.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @interface Item : NSObject 6 | 7 | @property (nonatomic, readonly, copy) NSString *name; 8 | @property (nonatomic, readonly, assign) NSUInteger weight; 9 | @property (nonatomic, readonly, copy) NSArray *held; 10 | 11 | - (instancetype)init NS_UNAVAILABLE; 12 | - (instancetype)initWithName:(NSString *)name weight:(NSUInteger)weight held:(NSArray *)held NS_DESIGNATED_INITIALIZER; 13 | 14 | @end 15 | 16 | @implementation Item 17 | 18 | - (instancetype)initWithName:(NSString *)name weight:(NSUInteger)weight held:(NSArray *)held { 19 | self = [super init]; 20 | if (self) { 21 | _name = [name copy]; 22 | _weight = weight; 23 | _held = [held copy]; 24 | } 25 | return self; 26 | } 27 | 28 | - (NSString *)description { 29 | return [NSString stringWithFormat:@"Name: %@, weight: %lu, held: %@", _name, _weight, _held]; 30 | } 31 | 32 | @end 33 | 34 | @implementation A17D07 { 35 | NSArray *_data; 36 | NSDictionary *_index; 37 | } 38 | 39 | - (NSArray *)data { 40 | if (!_data) { 41 | NSMutableArray *result = [[NSMutableArray alloc] init]; 42 | NSArray *emptyHeld = [[NSArray alloc] init]; 43 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 44 | NSArray *parts = [line componentsSeparatedByString:@" -> "]; 45 | NSArray *nameAndWeight = [parts[0] componentsSeparatedByString:@" ("]; 46 | [result addObject:[[Item alloc] initWithName:nameAndWeight[0] 47 | weight:nameAndWeight[1].integerValue 48 | held:(parts.count == 2 ? [parts[1] componentsSeparatedByString:@", "] : emptyHeld)]]; 49 | }]; 50 | _data = [result copy]; 51 | } 52 | return _data; 53 | } 54 | 55 | - (nullable id)part1 { 56 | NSMutableSet *names = [[NSMutableSet alloc] init]; 57 | NSMutableSet *held = [[NSMutableSet alloc] init]; 58 | 59 | [[self data] enumerateObjectsUsingBlock:^(Item *item, NSUInteger idx, BOOL *stop) { 60 | [names addObject:item.name]; 61 | [held addObjectsFromArray:item.held]; 62 | }]; 63 | 64 | [names minusSet:held]; 65 | 66 | return [names anyObject]; 67 | } 68 | 69 | - (Item *)lookupByName:(NSString *)name { 70 | if (!_index) { 71 | NSMutableDictionary *tmp = [[NSMutableDictionary alloc] init]; 72 | [[self data] enumerateObjectsUsingBlock:^(Item *item, NSUInteger idx, BOOL *stop) { 73 | tmp[item.name] = item; 74 | }]; 75 | _index = [tmp copy]; 76 | } 77 | return _index[name]; 78 | } 79 | 80 | - (NSUInteger)weight:(NSString *)name { 81 | Item *item = [self lookupByName:name]; 82 | __block NSUInteger result = item.weight; 83 | [item.held enumerateObjectsUsingBlock:^(NSString *held, NSUInteger idx, BOOL *stop) { 84 | result += [self weight:held]; 85 | }]; 86 | return result; 87 | } 88 | 89 | - (nullable id)part2 { 90 | __block NSNumber *result; 91 | [[self data] enumerateObjectsUsingBlock:^(Item *item, NSUInteger idx, BOOL *stop) { 92 | if (item.held.count) { 93 | NSMutableArray *weights = [[NSMutableArray alloc] initWithCapacity:item.held.count]; 94 | for (NSString *held in item.held) { 95 | [weights addObject:@([self weight:held])]; 96 | } 97 | if ([[NSSet alloc] initWithArray:weights].count > 1) { 98 | NSMutableDictionary *weightFrequencies = [[NSMutableDictionary alloc] init]; 99 | for (NSNumber *weight in weights) { 100 | weightFrequencies[weight] = @(weightFrequencies[weight].integerValue + 1); 101 | } 102 | __block NSNumber *uniqueWeight; 103 | [weightFrequencies enumerateKeysAndObjectsUsingBlock:^(NSNumber *weight, NSNumber *count, BOOL *stop) { 104 | if (count.integerValue == 1) { 105 | uniqueWeight = weight; 106 | *stop = YES; 107 | } 108 | }]; 109 | result = @([self lookupByName:item.held[[weights indexOfObject:uniqueWeight]]].weight + 110 | (((NSNumber *)[weights valueForKeyPath:@"@min.self"]).integerValue - 111 | ((NSNumber *)[weights valueForKeyPath:@"@max.self"]).integerValue)); 112 | *stop = YES; 113 | } 114 | } 115 | }]; 116 | return result; 117 | } 118 | 119 | @end 120 | 121 | NS_ASSUME_NONNULL_END 122 | -------------------------------------------------------------------------------- /src/advent_2017/A17D12.m: -------------------------------------------------------------------------------- 1 | #import "A17D12.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @interface A17D12Node : NSObject 6 | 7 | @property (nonatomic, readonly, assign) NSUInteger identifier; 8 | @property (nonatomic, readonly, copy) NSArray *related; 9 | 10 | - (instancetype)init NS_UNAVAILABLE; 11 | - (instancetype)initWithIdentifier:(NSUInteger)identifier related:(NSArray *)related NS_DESIGNATED_INITIALIZER; 12 | 13 | @end 14 | 15 | @implementation A17D12Node 16 | 17 | - (instancetype)initWithIdentifier:(NSUInteger)identifier related:(NSArray *)related { 18 | self = [super init]; 19 | if (self) { 20 | _identifier = identifier; 21 | _related = [related copy]; 22 | } 23 | return self; 24 | } 25 | 26 | - (NSString *)description { 27 | return [NSString stringWithFormat:@"<%@: id=%lu related=(%@)>", [self className], (unsigned long)_identifier, [_related componentsJoinedByString:@", "]]; 28 | } 29 | 30 | @end 31 | 32 | @interface UFValue : NSObject 33 | 34 | @property (nonatomic, assign) NSUInteger parent; 35 | @property (nonatomic, assign) NSUInteger rank; 36 | 37 | @end 38 | 39 | @implementation UFValue 40 | 41 | - (NSString *)description { 42 | return [NSString stringWithFormat:@"%lu %lu", (unsigned long)_parent, (unsigned long)_rank]; 43 | } 44 | 45 | @end 46 | 47 | @interface UFStructure () 48 | 49 | @property (nonatomic) NSMutableDictionary *uf; 50 | 51 | @end 52 | 53 | @implementation UFStructure 54 | 55 | - (instancetype)init { 56 | self = [super init]; 57 | if (self) { 58 | _uf = [[NSMutableDictionary alloc] init]; 59 | } 60 | return self; 61 | } 62 | 63 | - (NSString *)description { 64 | return _uf.description; 65 | } 66 | 67 | - (NSUInteger)find:(NSUInteger)x { 68 | UFValue *v = _uf[@(x)]; 69 | if (!v) { 70 | v = [[UFValue alloc] init]; 71 | v.parent = x; 72 | _uf[@(x)] = v; 73 | return x; 74 | } else if (v.parent == x) { 75 | return x; 76 | } else { 77 | NSUInteger xRoot = [self find:v.parent]; 78 | v.parent = xRoot; 79 | return xRoot; 80 | } 81 | } 82 | 83 | - (void)union:(NSUInteger)x with:(NSUInteger)y { 84 | NSUInteger xRoot = [self find:x]; 85 | NSUInteger yRoot = [self find:y]; 86 | UFValue *xValue = _uf[@(xRoot)]; 87 | UFValue *yValue = _uf[@(yRoot)]; 88 | if (xRoot != yRoot) { 89 | if (xValue.rank < yValue.rank) { 90 | xValue.parent = yRoot; 91 | } else { 92 | yValue.parent = xRoot; 93 | if (xRoot == yRoot) { 94 | xValue.rank++; 95 | } 96 | } 97 | } 98 | } 99 | 100 | - (NSSet *)elementsWith:(NSUInteger)x 101 | { 102 | NSMutableSet *result = [[NSMutableSet alloc] init]; 103 | NSUInteger xRoot = [self find:x]; 104 | for (NSNumber *identifier in [_uf allKeys]) { 105 | if ([self find:identifier.integerValue] == xRoot) { 106 | [result addObject:identifier]; 107 | } 108 | } 109 | return [result copy]; 110 | } 111 | 112 | - (NSUInteger)distinctSetCount { 113 | NSMutableSet *set = [[NSMutableSet alloc] init]; 114 | for (NSNumber *identifier in [_uf allKeys]) { 115 | [set addObject:@([self find:identifier.integerValue])]; 116 | } 117 | return set.count; 118 | } 119 | 120 | @end 121 | 122 | @interface A17D12 () 123 | 124 | @property (nonatomic, readonly, copy) NSArray *data; 125 | @property (nonatomic, readonly, copy) UFStructure *uf; 126 | 127 | @end 128 | 129 | @implementation A17D12 { 130 | NSArray *_data; 131 | UFStructure *_uf; 132 | } 133 | 134 | - (NSArray *)data { 135 | if (!_data) { 136 | NSMutableArray *tmp = [[NSMutableArray alloc] init]; 137 | 138 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 139 | NSArray *components = [line componentsSeparatedByString:@" <-> "]; 140 | NSArray *relatedComponents = [components[1] componentsSeparatedByString:@","]; 141 | 142 | NSMutableArray *related = [[NSMutableArray alloc] init]; 143 | for (NSString *relatedComponent in relatedComponents) { 144 | [related addObject:@(relatedComponent.integerValue)]; 145 | } 146 | 147 | [tmp addObject:[[A17D12Node alloc] initWithIdentifier:components[0].integerValue related:related]]; 148 | }]; 149 | _data = [tmp copy]; 150 | } 151 | return _data; 152 | } 153 | 154 | - (UFStructure *)uf { 155 | if (!_uf) { 156 | _uf = [[UFStructure alloc] init]; 157 | for (A17D12Node *node in self.data) { 158 | for (NSNumber *related in node.related) { 159 | [_uf union:node.identifier with:related.integerValue]; 160 | } 161 | } 162 | } 163 | return _uf; 164 | } 165 | 166 | - (nullable id)part1 { 167 | return @([self.uf elementsWith:0].count); 168 | } 169 | 170 | - (nullable id)part2 { 171 | return @([self.uf distinctSetCount]); 172 | } 173 | 174 | @end 175 | 176 | NS_ASSUME_NONNULL_END 177 | -------------------------------------------------------------------------------- /src/advent_2017/A17D08.m: -------------------------------------------------------------------------------- 1 | #import "A17D08.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | @interface Instruction : NSObject 6 | 7 | @property (nonatomic, readonly, copy) NSString *target; 8 | @property (nonatomic, readonly, copy) NSNumber *(^op)(NSNumber *); 9 | @property (nonatomic, readonly, copy) NSString *lhs; 10 | @property (nonatomic, readonly, copy) BOOL (^cmp)(NSNumber *); 11 | 12 | - (instancetype)init NS_UNAVAILABLE; 13 | - (instancetype)initWithTarget:(NSString *)target 14 | op:(NSNumber *(^)(NSNumber *))op 15 | lhs:(NSString *)lhs 16 | cmp:(BOOL (^)(NSNumber *))cmp NS_DESIGNATED_INITIALIZER; 17 | 18 | @end 19 | 20 | @implementation Instruction 21 | 22 | - (instancetype)initWithTarget:(NSString *)target 23 | op:(NSNumber *(^)(NSNumber *))op 24 | lhs:(NSString *)lhs 25 | cmp:(BOOL (^)(NSNumber *))cmp { 26 | self = [super init]; 27 | if (self) { 28 | _target = [target copy]; 29 | _op = [op copy]; 30 | _lhs = [lhs copy]; 31 | _cmp = [cmp copy]; 32 | } 33 | return self; 34 | } 35 | 36 | @end 37 | 38 | @implementation A17D08 { 39 | NSArray *_data; 40 | } 41 | 42 | - (NSArray *)data { 43 | if (!_data) { 44 | NSMutableArray *tmp = [[NSMutableArray alloc] init]; 45 | 46 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 47 | NSArray *components = [line componentsSeparatedByString:@" "]; 48 | 49 | NSString *target = components[0]; 50 | NSString *opStr = components[1]; 51 | NSNumber *value = @(components[2].integerValue); 52 | NSString *lhs = components[4]; 53 | NSString *cmpStr = components[5]; 54 | NSNumber *rhs = @(components[6].integerValue); 55 | 56 | NSDictionary *opBlocks = @{ 57 | @"inc": ^NSNumber *(NSNumber *registerValue) { 58 | return @(registerValue.integerValue + value.integerValue); 59 | }, 60 | @"dec": ^NSNumber *(NSNumber *registerValue) { 61 | return @(registerValue.integerValue - value.integerValue); 62 | }, 63 | }; 64 | 65 | NSDictionary *comparisonBlocks = @{ 66 | @"==": ^BOOL (NSNumber *lhs) { 67 | return [lhs isEqualToNumber:rhs]; 68 | }, 69 | @"!=": ^BOOL (NSNumber *lhs) { 70 | return ![lhs isEqualToNumber:rhs]; 71 | }, 72 | @"<": ^BOOL (NSNumber *lhs) { 73 | return [lhs compare:rhs] == NSOrderedAscending; 74 | }, 75 | @"<=": ^BOOL (NSNumber *lhs) { 76 | return [lhs compare:rhs] != NSOrderedDescending; 77 | }, 78 | @">": ^BOOL (NSNumber *lhs) { 79 | return [lhs compare:rhs] == NSOrderedDescending; 80 | }, 81 | @">=": ^BOOL (NSNumber *lhs) { 82 | return [lhs compare:rhs] != NSOrderedAscending; 83 | } 84 | }; 85 | 86 | Instruction *instruction = [[Instruction alloc] initWithTarget:target 87 | op:opBlocks[opStr] 88 | lhs:lhs 89 | cmp:comparisonBlocks[cmpStr]]; 90 | [tmp addObject:instruction]; 91 | }]; 92 | _data = [tmp copy]; 93 | } 94 | 95 | return _data; 96 | } 97 | 98 | - (NSDictionary *)applyInstruction:(Instruction *)instruction toRegisters:(NSDictionary *)registers { 99 | NSMutableDictionary *result = [registers mutableCopy]; 100 | if (instruction.cmp(registers[instruction.lhs] ?: @0)) { 101 | result[instruction.target] = instruction.op(registers[instruction.target] ?: @0); 102 | } 103 | return [result copy]; 104 | } 105 | 106 | - (nullable id)part1 { 107 | __block NSDictionary *registers = @{}; 108 | [[self data] enumerateObjectsUsingBlock:^(Instruction *instruction, NSUInteger idx, BOOL *stop) { 109 | registers = [self applyInstruction:instruction toRegisters:registers]; 110 | }]; 111 | return [registers.allValues valueForKeyPath:@"@max.self"]; 112 | } 113 | 114 | - (nullable id)part2 { 115 | __block NSUInteger overallMax = 0; 116 | __block NSDictionary *registers = @{}; 117 | [[self data] enumerateObjectsUsingBlock:^(Instruction *instruction, NSUInteger idx, BOOL *stop) { 118 | registers = [self applyInstruction:instruction toRegisters:registers]; 119 | overallMax = MAX(overallMax, ((NSNumber *)[registers.allValues valueForKeyPath:@"@max.self"]).integerValue); 120 | }]; 121 | return @(overallMax); 122 | } 123 | 124 | @end 125 | 126 | NS_ASSUME_NONNULL_END 127 | -------------------------------------------------------------------------------- /resources/advent_2018/day_07/input: -------------------------------------------------------------------------------- 1 | Step F must be finished before step Q can begin. 2 | Step A must be finished before step K can begin. 3 | Step K must be finished before step R can begin. 4 | Step D must be finished before step X can begin. 5 | Step L must be finished before step T can begin. 6 | Step V must be finished before step W can begin. 7 | Step J must be finished before step N can begin. 8 | Step B must be finished before step W can begin. 9 | Step X must be finished before step C can begin. 10 | Step W must be finished before step I can begin. 11 | Step Q must be finished before step P can begin. 12 | Step E must be finished before step M can begin. 13 | Step C must be finished before step N can begin. 14 | Step U must be finished before step O can begin. 15 | Step O must be finished before step R can begin. 16 | Step N must be finished before step Z can begin. 17 | Step R must be finished before step I can begin. 18 | Step G must be finished before step H can begin. 19 | Step T must be finished before step H can begin. 20 | Step M must be finished before step P can begin. 21 | Step Y must be finished before step I can begin. 22 | Step S must be finished before step Z can begin. 23 | Step I must be finished before step H can begin. 24 | Step H must be finished before step P can begin. 25 | Step P must be finished before step Z can begin. 26 | Step Y must be finished before step P can begin. 27 | Step A must be finished before step O can begin. 28 | Step V must be finished before step O can begin. 29 | Step G must be finished before step Y can begin. 30 | Step K must be finished before step B can begin. 31 | Step I must be finished before step P can begin. 32 | Step D must be finished before step L can begin. 33 | Step A must be finished before step P can begin. 34 | Step O must be finished before step T can begin. 35 | Step F must be finished before step C can begin. 36 | Step M must be finished before step S can begin. 37 | Step V must be finished before step Q can begin. 38 | Step G must be finished before step I can begin. 39 | Step O must be finished before step I can begin. 40 | Step N must be finished before step I can begin. 41 | Step E must be finished before step O can begin. 42 | Step N must be finished before step S can begin. 43 | Step J must be finished before step H can begin. 44 | Step C must be finished before step P can begin. 45 | Step E must be finished before step N can begin. 46 | Step T must be finished before step P can begin. 47 | Step A must be finished before step G can begin. 48 | Step A must be finished before step V can begin. 49 | Step C must be finished before step H can begin. 50 | Step A must be finished before step Y can begin. 51 | Step E must be finished before step U can begin. 52 | Step T must be finished before step Y can begin. 53 | Step Q must be finished before step S can begin. 54 | Step Y must be finished before step S can begin. 55 | Step E must be finished before step P can begin. 56 | Step N must be finished before step T can begin. 57 | Step T must be finished before step M can begin. 58 | Step Q must be finished before step M can begin. 59 | Step H must be finished before step Z can begin. 60 | Step D must be finished before step Y can begin. 61 | Step J must be finished before step R can begin. 62 | Step U must be finished before step R can begin. 63 | Step K must be finished before step N can begin. 64 | Step A must be finished before step W can begin. 65 | Step A must be finished before step H can begin. 66 | Step X must be finished before step G can begin. 67 | Step V must be finished before step J can begin. 68 | Step W must be finished before step C can begin. 69 | Step I must be finished before step Z can begin. 70 | Step V must be finished before step H can begin. 71 | Step R must be finished before step H can begin. 72 | Step U must be finished before step N can begin. 73 | Step O must be finished before step Z can begin. 74 | Step X must be finished before step S can begin. 75 | Step E must be finished before step G can begin. 76 | Step W must be finished before step U can begin. 77 | Step U must be finished before step G can begin. 78 | Step D must be finished before step Z can begin. 79 | Step E must be finished before step R can begin. 80 | Step L must be finished before step B can begin. 81 | Step B must be finished before step R can begin. 82 | Step G must be finished before step T can begin. 83 | Step F must be finished before step K can begin. 84 | Step R must be finished before step S can begin. 85 | Step J must be finished before step Z can begin. 86 | Step Q must be finished before step U can begin. 87 | Step X must be finished before step O can begin. 88 | Step F must be finished before step I can begin. 89 | Step W must be finished before step R can begin. 90 | Step W must be finished before step Y can begin. 91 | Step M must be finished before step Y can begin. 92 | Step S must be finished before step I can begin. 93 | Step F must be finished before step O can begin. 94 | Step C must be finished before step Y can begin. 95 | Step N must be finished before step G can begin. 96 | Step O must be finished before step S can begin. 97 | Step Q must be finished before step O can begin. 98 | Step K must be finished before step T can begin. 99 | Step X must be finished before step Z can begin. 100 | Step L must be finished before step N can begin. 101 | Step S must be finished before step P can begin. 102 | -------------------------------------------------------------------------------- /src/advent_2017/A17D18.m: -------------------------------------------------------------------------------- 1 | #import "A17D18.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | typedef void (^A17D18Snd)(NSNumber *val); 6 | typedef BOOL (^A17D18Rcv)(NSMutableDictionary *regs, NSNumber *priorVal, NSString *reg); 7 | 8 | @interface A17D18Processor : NSObject 9 | 10 | @property (nonatomic, assign) NSUInteger ip; 11 | @property (nonatomic, strong) NSMutableDictionary *registers; 12 | @property (nonatomic, copy) A17D18Snd snd; 13 | @property (nonatomic, copy) A17D18Rcv rcv; 14 | 15 | - (instancetype)init NS_UNAVAILABLE; 16 | - (instancetype)initWithSnd:(A17D18Snd)snd Rcv:(A17D18Rcv)rcv NS_DESIGNATED_INITIALIZER; 17 | 18 | @end 19 | 20 | @implementation A17D18Processor 21 | 22 | - (instancetype)initWithSnd:(A17D18Snd)snd Rcv:(A17D18Rcv)rcv { 23 | self = [super init]; 24 | if (self) { 25 | _ip = 0; 26 | _registers = [[NSMutableDictionary alloc] init]; 27 | _snd = snd; 28 | _rcv = rcv; 29 | } 30 | return self; 31 | } 32 | 33 | - (NSNumber *)evaluate:(id)arg { 34 | return [arg isKindOfClass:[NSString class]] ? _registers[arg] : (NSNumber *)arg; 35 | } 36 | 37 | - (void)step:(NSArray *)program { 38 | NSArray *fetched = program[_ip]; 39 | NSString *op = fetched[0]; 40 | id arg1 = fetched[1]; 41 | id arg2 = fetched.count > 2 ? fetched[2] : nil; 42 | 43 | _ip++; 44 | 45 | if ([op isEqualToString:@"snd"]) { 46 | _snd([self evaluate:arg1]); 47 | } else if ([op isEqualToString:@"rcv"]) { 48 | BOOL blocked = _rcv(_registers, [self evaluate:arg1], arg1); 49 | if (blocked) _ip--; 50 | } else if ([op isEqualToString:@"set"]) { 51 | _registers[arg1] = [self evaluate:arg2]; 52 | } else if ([op isEqualToString:@"jgz"]) { 53 | if ([self evaluate:arg1].integerValue > 0) { 54 | _ip--; 55 | _ip += [self evaluate:arg2].integerValue; 56 | } 57 | } else { 58 | NSInteger lhs = [self evaluate:arg1].integerValue; 59 | NSInteger rhs = [self evaluate:arg2].integerValue; 60 | NSInteger result; 61 | if ([op isEqualToString:@"add"]) { 62 | result = lhs + rhs; 63 | } else if ([op isEqualToString:@"mul"]) { 64 | result = lhs * rhs; 65 | } else if ([op isEqualToString:@"mod"]) { 66 | result = lhs % rhs; 67 | } else { 68 | @throw [NSError errorWithDomain:@"A17D18Processor" 69 | code:1 70 | userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Unknown operation: %@", op]}]; 71 | } 72 | _registers[arg1] = @(result); 73 | } 74 | } 75 | 76 | @end 77 | 78 | @interface A17D18 () 79 | 80 | @property (nonatomic, copy, readonly) NSArray *data; 81 | 82 | @end 83 | 84 | @implementation A17D18 { 85 | NSArray *_data; 86 | } 87 | 88 | + (id)regOrNum:(NSString *)v { 89 | NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 90 | return [formatter numberFromString:v] ?: v; 91 | } 92 | 93 | - (NSArray *)data { 94 | if (!_data) { 95 | NSMutableArray *tmp = [[NSMutableArray alloc] init]; 96 | [self.input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { 97 | NSArray *tokens = [line componentsSeparatedByString:@" "]; 98 | [tmp addObject:[[NSArray alloc] initWithObjects:tokens[0], [A17D18 regOrNum:tokens[1]], 99 | tokens.count > 2 ? [A17D18 regOrNum:tokens[2]] : nil, nil]]; 100 | }]; 101 | _data = [tmp copy]; 102 | } 103 | return _data; 104 | } 105 | 106 | - (nullable id)part1 { 107 | __block NSNumber *lastSnd = nil; 108 | __block BOOL done = NO; 109 | 110 | A17D18Processor *processor = [[A17D18Processor alloc] initWithSnd:^(NSNumber *val) { 111 | lastSnd = val; 112 | } Rcv:^(NSMutableDictionary *regs, NSNumber *priorVal, NSString *reg) { 113 | if (priorVal.integerValue != 0) { 114 | regs[reg] = lastSnd; 115 | done = YES; 116 | } 117 | return NO; 118 | }]; 119 | 120 | while (!done) { 121 | [processor step:self.data]; 122 | } 123 | 124 | return lastSnd; 125 | } 126 | 127 | - (nullable id)part2 { 128 | NSMutableArray *queue0 = [[NSMutableArray alloc] init]; 129 | NSMutableArray *queue1 = [[NSMutableArray alloc] init]; 130 | __block BOOL needInput0 = NO; 131 | __block BOOL needInput1 = NO; 132 | __block NSUInteger sndCount1 = 0; 133 | 134 | A17D18Processor *processor0 = [[A17D18Processor alloc] initWithSnd:^(NSNumber *val) { 135 | [queue1 addObject:val]; 136 | } Rcv:^(NSMutableDictionary *regs, NSNumber *priorVal, NSString *reg) { 137 | if (queue0.count) { 138 | regs[reg] = queue0[0]; 139 | [queue0 removeObjectAtIndex:0]; 140 | needInput0 = NO; 141 | } else { 142 | needInput0 = YES; 143 | } 144 | return needInput0; 145 | }]; 146 | 147 | A17D18Processor *processor1 = [[A17D18Processor alloc] initWithSnd:^(NSNumber *val) { 148 | [queue0 addObject:val]; 149 | sndCount1++; 150 | } Rcv:^(NSMutableDictionary *regs, NSNumber *priorVal, NSString *reg) { 151 | if (queue1.count) { 152 | regs[reg] = queue1[0]; 153 | [queue1 removeObjectAtIndex:0]; 154 | needInput1 = NO; 155 | } else { 156 | needInput1 = YES; 157 | } 158 | return needInput1; 159 | }]; 160 | 161 | processor1.registers[@"p"] = @1; 162 | 163 | for (;;) { 164 | BOOL blocked0 = needInput0 && !queue0.count; 165 | BOOL blocked1 = needInput1 && !queue1.count; 166 | if (blocked0 && blocked1) { 167 | return @(sndCount1); 168 | } 169 | if (blocked0) { 170 | [processor1 step:self.data]; 171 | } else { 172 | [processor0 step:self.data]; 173 | } 174 | } 175 | } 176 | 177 | @end 178 | 179 | NS_ASSUME_NONNULL_END 180 | -------------------------------------------------------------------------------- /src/advent_2017/A17D11.m: -------------------------------------------------------------------------------- 1 | #import "A17D11.h" 2 | 3 | NS_ASSUME_NONNULL_BEGIN 4 | 5 | typedef NS_ENUM(NSUInteger, AD17D11Dir) { 6 | AD17D11DirN, 7 | AD17D11DirNE, 8 | AD17D11DirE, 9 | AD17D11DirSE, 10 | AD17D11DirS, 11 | AD17D11DirSW, 12 | AD17D11DirW, 13 | AD17D11DirNW 14 | }; 15 | 16 | NSString *NSStringFromAD17D11Dir(AD17D11Dir dir) { 17 | switch (dir) { 18 | case AD17D11DirN: 19 | return @"N"; 20 | case AD17D11DirNE: 21 | return @"NE"; 22 | case AD17D11DirE: 23 | return @"E"; 24 | case AD17D11DirSE: 25 | return @"SE"; 26 | case AD17D11DirS: 27 | return @"S"; 28 | case AD17D11DirSW: 29 | return @"SW"; 30 | case AD17D11DirW: 31 | return @"W"; 32 | case AD17D11DirNW: 33 | return @"NW"; 34 | default: 35 | return @"Unknown"; 36 | } 37 | } 38 | 39 | @interface NSValue (AD17D11Dir) 40 | 41 | + (instancetype)valueWithAD17D11Dir:(AD17D11Dir)dir; 42 | - (AD17D11Dir)AD17D11DirValue; 43 | 44 | @end 45 | 46 | @implementation NSValue (AD17D11Dir) 47 | 48 | + (instancetype)valueWithAD17D11Dir:(AD17D11Dir)dir { 49 | return [NSValue value:&dir withObjCType:@encode(AD17D11Dir)]; 50 | } 51 | 52 | - (AD17D11Dir)AD17D11DirValue { 53 | AD17D11Dir dir; 54 | [self getValue:&dir]; 55 | return dir; 56 | } 57 | 58 | - (NSString *)description { 59 | return NSStringFromAD17D11Dir([self AD17D11DirValue]); 60 | } 61 | 62 | @end 63 | 64 | @interface AD17D11Location : NSObject 65 | 66 | @property (nonatomic, assign, readonly) int x; 67 | @property (nonatomic, assign, readonly) int y; 68 | @property (nonatomic, assign, readonly) int z; 69 | @property (nonatomic, readonly) int distance; 70 | 71 | - (instancetype)init NS_UNAVAILABLE; 72 | - (instancetype)initWithX:(int)x y:(int)y z:(int)z NS_DESIGNATED_INITIALIZER; 73 | 74 | @end 75 | 76 | @implementation AD17D11Location 77 | 78 | @dynamic distance; 79 | 80 | - (instancetype)initWithX:(int)x y:(int)y z:(int)z { 81 | self = [super init]; 82 | if (self) { 83 | _x = x; 84 | _y = y; 85 | _z = z; 86 | } 87 | return self; 88 | } 89 | 90 | - (BOOL)isEqual:(id)object { 91 | if (self == object) return YES; 92 | if ([self class] != [object class]) return NO; 93 | 94 | AD17D11Location *otherLocation = (AD17D11Location *)object; 95 | return _x == otherLocation.x && _y == otherLocation.y && _z == otherLocation.z; 96 | } 97 | 98 | - (NSUInteger)hash { 99 | return 71993 * (_x + 13752) + 933199 * _y + 3212 * _z; 100 | } 101 | 102 | - (AD17D11Location *)add:(AD17D11Location *)other { 103 | return [[AD17D11Location alloc] initWithX:_x + other.x y:_y + other.y z:_z + other.z]; 104 | } 105 | 106 | - (AD17D11Location *)move:(AD17D11Dir)dir { 107 | switch (dir) { 108 | case AD17D11DirN: 109 | return [self add:[[AD17D11Location alloc] initWithX:0 y:1 z:-1]]; 110 | case AD17D11DirNE: 111 | return [self add:[[AD17D11Location alloc] initWithX:1 y:0 z:-1]]; 112 | case AD17D11DirSE: 113 | return [self add:[[AD17D11Location alloc] initWithX:1 y:-1 z:0]]; 114 | case AD17D11DirS: 115 | return [self add:[[AD17D11Location alloc] initWithX:0 y:-1 z:1]]; 116 | case AD17D11DirSW: 117 | return [self add:[[AD17D11Location alloc] initWithX:-1 y:0 z:1]]; 118 | case AD17D11DirNW: 119 | return [self add:[[AD17D11Location alloc] initWithX:-1 y:1 z:0]]; 120 | default: 121 | return nil; 122 | } 123 | } 124 | 125 | - (int)distance { 126 | return (abs(_x) + abs(_y) + abs(_z)) / 2; 127 | } 128 | 129 | - (NSString *)description { 130 | return [NSString stringWithFormat:@"[%d %d %d]", _x, _y, _z]; 131 | } 132 | 133 | - (id)copyWithZone:(nullable NSZone *)zone { 134 | return self; 135 | } 136 | 137 | @end 138 | 139 | @implementation A17D11 { 140 | NSArray *_data; 141 | } 142 | 143 | - (NSArray *)data { 144 | if (_data == nil) { 145 | NSMutableArray *tmp = [[NSMutableArray alloc] init]; 146 | NSString *inputWithoutNewline = [self.input substringToIndex:self.input.length - 1]; 147 | for (NSString *dir in [inputWithoutNewline componentsSeparatedByString:@","]) { 148 | if ([dir isEqualToString:@"n"]) { 149 | [tmp addObject:[NSValue valueWithAD17D11Dir:AD17D11DirN]]; 150 | } else if ([dir isEqualToString:@"ne"]) { 151 | [tmp addObject:[NSValue valueWithAD17D11Dir:AD17D11DirNE]]; 152 | } else if ([dir isEqualToString:@"e"]) { 153 | [tmp addObject:[NSValue valueWithAD17D11Dir:AD17D11DirE]]; 154 | } else if ([dir isEqualToString:@"se"]) { 155 | [tmp addObject:[NSValue valueWithAD17D11Dir:AD17D11DirSE]]; 156 | } else if ([dir isEqualToString:@"s"]) { 157 | [tmp addObject:[NSValue valueWithAD17D11Dir:AD17D11DirS]]; 158 | } else if ([dir isEqualToString:@"sw"]) { 159 | [tmp addObject:[NSValue valueWithAD17D11Dir:AD17D11DirSW]]; 160 | } else if ([dir isEqualToString:@"w"]) { 161 | [tmp addObject:[NSValue valueWithAD17D11Dir:AD17D11DirW]]; 162 | } else if ([dir isEqualToString:@"nw"]) { 163 | [tmp addObject:[NSValue valueWithAD17D11Dir:AD17D11DirNW]]; 164 | } else { 165 | NSLog(@"Unexpected dir: [%@]", dir); 166 | } 167 | } 168 | _data = [tmp copy]; 169 | } 170 | return _data; 171 | } 172 | 173 | - (nullable id)part1 { 174 | AD17D11Location *loc = [[AD17D11Location alloc] initWithX:0 y:0 z:0]; 175 | for (NSValue *dir in [self data]) { 176 | loc = [loc move:[dir AD17D11DirValue]]; 177 | } 178 | return @(loc.distance); 179 | } 180 | 181 | - (nullable id)part2 { 182 | NSUInteger maxDistance = 0; 183 | AD17D11Location *loc = [[AD17D11Location alloc] initWithX:0 y:0 z:0]; 184 | for (NSValue *dir in [self data]) { 185 | loc = [loc move:[dir AD17D11DirValue]]; 186 | maxDistance = MAX(maxDistance, loc.distance); 187 | } 188 | return @(maxDistance); 189 | } 190 | 191 | @end 192 | 193 | NS_ASSUME_NONNULL_END 194 | -------------------------------------------------------------------------------- /resources/advent_2018/day_02/input: -------------------------------------------------------------------------------- 1 | bvhfawknyoqsudzrpgslecmtkj 2 | bpufawcnyoqxldzrpgsleimtkj 3 | bvhfawcnyoqxqdzrplsleimtkf 4 | bvhoagcnyoqxudzrpgsleixtkj 5 | bxvfgwcnyoqxudzrpgsleimtkj 6 | bvqfawcngoqxudzrpgsleiktkj 7 | bvhfawcnmoqxuyzrpgsleimtkp 8 | bvheawcnyomxsdzrpgsleimtkj 9 | bcdfawcnyoqxudzrpgsyeimtkj 10 | bvhpawcnyoqxudzrpgsteimtkz 11 | bxhfawcnyozxudzrpgsleimtoj 12 | bvhfdwcnyozxudzrposleimtkj 13 | bvpfawcnyotxudzrpgsleimtkq 14 | bvhfpwccyoqxudzrpgslkimtkj 15 | bvhfawcnyoqxudirpgsreimtsj 16 | bvhfawcnyoqxudzppgbleemtkj 17 | bvhzawcnyoqxudqrpgslvimtkj 18 | bvhfawclyoqxudirpgsleimtka 19 | bvhgawfnyoqxudzrpguleimtkj 20 | bvhfazcnytqxudzrpgslvimtkj 21 | bvhfawcnygxxudzrpgjleimtkj 22 | bxhfawcnyoqxudzipgsleimtxj 23 | bvhptwcnyoqxudzrpgsleimtmj 24 | bzhfawcgyooxudzrpgsleimtkj 25 | bvhjlwcnyokxudzrpgsleimtkj 26 | bvhfawcnyoqxudbrmgslesmtkj 27 | bvhfawcnysixudzwpgsleimtkj 28 | bvhflwcnymqxxdzrpgsleimtkj 29 | bvifawcnyoyxudzrpgsleimtvj 30 | bvhfawcnyofxudlrpgsheimtkj 31 | bvhbawcmyoqxudzrpggleimtkj 32 | bhhxgwcnyoqxudzrpgsleimtkj 33 | bvhfawgnyoqxbdzrpgsleimfkj 34 | bvhfawcnyoqxudcrngsleimykj 35 | bvhfawcnyofxudzrpgslebgtkj 36 | bvhfaocnybqxudzapgsleimtkj 37 | bvhxawcnyodxudzrpfsleimtkj 38 | bchfawcnyoqxudrrtgsleimtkj 39 | bvhfawcqyoqxudzdpgsltimtkj 40 | bvhfawknyoqxudzrpnsleimtbj 41 | cihfawcnyoqxudirpgsleimtkj 42 | bvlfawpnyoqxudzrpgslgimtkj 43 | bulfawcnyoqbudzrpgsleimtkj 44 | bvhfajcnyoqkudzrpgsoeimtkj 45 | bvhrakcnyoqxudzrpgsleimjkj 46 | bvbftwcnyoqxuvzrpgsleimtkj 47 | bvhfhwcnyoqxudzrpgslelmtbj 48 | bvhyawcntoqxudzrpgsleimtuj 49 | xvhuawcnyoqxuqzrpgsleimtkj 50 | pvhfawcnyoqxudzdpglleimtkj 51 | bvhfawsnyoqxudzrpgvlefmtkj 52 | bvhfawcnyoqxudzrpgepeiwtkj 53 | bvhfawcnyoqxudzrphsleittkr 54 | dvhfawcnyoqxudzrpkslzimtkj 55 | bvhfawpnyoqxudzrpgmlcimtkj 56 | bvhsawcnyzqxudzrpgsaeimtkj 57 | bdhfawcnyoqxudzrpasleiwtkj 58 | bvhfawbnyoqxpdbrpgsleimtkj 59 | mvhfawwnyoqxujzrpgsleimtkj 60 | bvafawcnyoyxudzrpgsleidtkj 61 | bvhyawcnyoqxudztpgzleimtkj 62 | besfawcnyoqxudzrpgsleimdkj 63 | bvhfawcnyoqxudrrpgsjeimjkj 64 | xvhfkwcnyoqxudzcpgsleimtkj 65 | bvhfawcnyeqdudzrpgzleimtkj 66 | bvhfuwcnybqxudzrpgsleimttj 67 | lvhfawcnyoqhudzdpgsleimtkj 68 | bvhfawcnyoqxudzrpgslevwtnj 69 | bvhfadcnzoqxxdzrpgsleimtkj 70 | bvsfawcnyoqxpdzrpgileimtkj 71 | bzhfaycnyoqxudzrpgsxeimtkj 72 | bwhfdwcnyoqxudzrpgsleimtkz 73 | bvhfawcnyoqxudzrpgsjlimtkm 74 | bvhfawcnyoqxudsrwgsleimtlj 75 | bbhfalynyoqxudzrpgsleimtkj 76 | bvhfawcnyeqxudzrpglleimtkr 77 | bvhfawnnboqxurzrpgsleimtkj 78 | yvhfawcnyoqxudzrpgslzimtpj 79 | bvhfjwcnyoqxqdxrpgsleimtkj 80 | bthfawcnyoqfudzrpgslhimtkj 81 | bvhfawchuoqxudzqpgsleimtkj 82 | bvhfawcndoqxudzrugsleimrkj 83 | bvhfawcnnoqxjdzrpgsleidtkj 84 | bvhpawcnyoqkudzrpgsleimtzj 85 | bvhfaiinyoqxudzopgsleimtkj 86 | bvhfawcnyxqxuizrigsleimtkj 87 | bvnfawcnyoqxudzqpgsleimbkj 88 | bvnfawcnyoeyudzrpgsleimtkj 89 | bvhfawcnyoqxudarpgsieimtoj 90 | bthcawcnyoqxudlrpgsleimtkj 91 | bvhfnwcnyozxudzrpgsleomtkj 92 | bpwfawcnyoqxudzrpgskeimtkj 93 | bvhfapcnyoqxudnrpgsxeimtkj 94 | bvhfdwcnyoqxubzrxgsleimtkj 95 | fvhfawcnyoqxjdzrpgsleirtkj 96 | bvhfawcneoqxudzrvzsleimtkj 97 | bvhaawcnyoqxudzrpgsleimtex 98 | bvhfawcnyojxudvrpgsleimckj 99 | bvlfawcnyoqxddzrpgsleimtko 100 | bvhfawclfoqxudzrpgsleiktkj 101 | bvhfawciyobxudzrpgkleimtkj 102 | bvhfpwcnyoqxudzrpgsqeimtkd 103 | bvhyawcnyyqxudzrkgsleimtkj 104 | bvhfawcncoqxudzrphsaeimtkj 105 | bvhfawmnyoqxudzrpgifeimtkj 106 | bvhfawcjyoqxudzjpgszeimtkj 107 | bohfawcnwoqxudzrpgsleimwkj 108 | bvhfaucnyoqxudzrpgfluimtkj 109 | bvhfawlnyoqgudzrpgwleimtkj 110 | bmhfawcnyoqxndzrpgsleymtkj 111 | bvhfawcngoqxudzrpzxleimtkj 112 | bihfawcnyoqxudrrpgsleimokj 113 | lvhfawcnylqxudzrpgsleintkj 114 | bvhfawcnyoqvugzrqgsleimtkj 115 | bvhfawcnyoqxudzgpgslqimtij 116 | bvhfawcnyoqludzrpgslnimtcj 117 | hvhfawcnyolxudzrpgsmeimtkj 118 | nvhfawcdkoqxudzrpgsleimtkj 119 | bvhfawcnyoqxkdzrggsneimtkj 120 | bvhfawnnyoqxudzrpgqleibtkj 121 | bvhfawyuyoqxudzrhgsleimtkj 122 | wvhfbwcnyoqxtdzrpgsleimtkj 123 | bvhfawcnyoqxedzzpgoleimtkj 124 | bvhfawcnioqxunzrpgsleimtnj 125 | bvhfawctyoqxudzrpgsldkmtkj 126 | bvhfawcnyonxudzrpgsleitpkj 127 | bvefawcnyoqaudzhpgsleimtkj 128 | bvhfawcnyxqxudzrpgslelmbkj 129 | bvhfamrnyoqxudzrpgsleimgkj 130 | bvhfaqcnyoqxudzrpgsaeimekj 131 | bvhfawcnyoqcidzrpgsleimvkj 132 | bvhfawcnnorxudzrpgsmeimtkj 133 | bvroawcnyoqxudzrpgsleiwtkj 134 | bvhfwwcnyoqxudzrpaslewmtkj 135 | bvsfawcnyoqxudzcpgszeimtkj 136 | bkhfmwcnyoqjudzrpgsleimtkj 137 | bvtfawcnyoqxudzrcgslecmtkj 138 | bvhfawcnypzxudzrpgsleimtkv 139 | bvhfawcnyoqzudzrfgtleimtkj 140 | bvhpawcnyoqxudhrpgsleimtko 141 | tvhfawcnyoqxudzxpfsleimtkj 142 | bvhfawccyofxudzrpqsleimtkj 143 | bvnfawtnyoqxuzzrpgsleimtkj 144 | bvhfamcnuwqxudzrpgsleimtkj 145 | bvhfawcfyoqxudjrpgsleimrkj 146 | bvhpalcnyoqxudzrpgslexmtkj 147 | bvhfawcnjsqxudzlpgsleimtkj 148 | bvhfafcnioqxydzrpgsleimtkj 149 | bvzfawcnyxqxudzgpgsleimtkj 150 | bvhzawcnyoqxudzrpgslewctkj 151 | bvhiawcnhoqrudzrpgsleimtkj 152 | bvhfawcnyoqxuszrggslenmtkj 153 | bvhfowcnyoqxudzrptseeimtkj 154 | behfawfnyoqxudzrpgsleimlkj 155 | lvhfawcnyoqxudsrpgvleimtkj 156 | bvhfawnnyaqxudzrpgsqeimtkj 157 | lvhfawcnfoqxvdzrpgsleimtkj 158 | svhxawcnyoqxudzrpqsleimtkj 159 | bvhfawqnfoqxudzrpgsleimkkj 160 | bvhfafcnyoqcudzrpgsleimtcj 161 | bvhfyfcntoqxudzrpgsleimtkj 162 | bvhfpwcnyoqxudzrpgsleimumj 163 | bvhfawccyoqxudzrqgrleimtkj 164 | bvhfawqnyoqxudzbpgsleimkkj 165 | bvhflwcnyoqxudzrpxsleemtkj 166 | bvhfawcnyoqxuezrpgslehrtkj 167 | bvhfawceyoqxudzrpgsleimswj 168 | bvhfawcncohgudzrpgsleimtkj 169 | bahfawcnyoqxgdzrpgsleamtkj 170 | yvhfawcnyoqxudzrppslrimtkj 171 | fvhfawcmyoqxudzrpgskeimtkj 172 | bvylawsnyoqxudzrpgsleimtkj 173 | bvhfswcnyyqxedzrpgsleimtkj 174 | fvrfawcnyoqxudzrpgzleimtkj 175 | bvhfawcnyoqxuvzrpgslermtks 176 | bvhkawccyoqxudzcpgsleimtkj 177 | bvhfaobnyoqxudzrprsleimtkj 178 | bvbfawcnyoqxudirpgsleimhkj 179 | bvhfawcnyoqxudzvpgsueimtgj 180 | bvhxawcnyoqxudzrpgsleimtgi 181 | svhfawcjyoqxuszrpgsleimtkj 182 | bvnfawcnyoeyudzrpgsldimtkj 183 | bvhfawcnyoqxuhzrpgsleimcki 184 | bvhfvwcnyoqxudzizgsleimtkj 185 | bvhfapznyohxudzrpgsleimtkj 186 | bvhfaelnyosxudzrpgsleimtkj 187 | xvhfawcnmoqxuhzrpgsleimtkj 188 | bjhfawcnyaqxutzrpgsleimtkj 189 | bvhfawcnyohxudzrpgslgnmtkj 190 | bvhfawcnyoqxudzrppsreimtkx 191 | fvhfapcnyoqyudzrpgsleimtkj 192 | qvhfafcnyoqxudorpgsleimtkj 193 | bvhfawcnyoqxedzrwgsleimtvj 194 | bvhfawgnyoqxudzupgqleimtkj 195 | bvhfowctyoqxudzrpgbleimtkj 196 | bvhwawcnyoqxudzapgslvimtkj 197 | bvhfadcnyoqxudzrugsleimtuj 198 | bvhfawcnyosxudzlpgsleamtkj 199 | bvhfawcnywqxuqzrpgsloimtkj 200 | bvhfawcnyoqxumzrpgvlfimtkj 201 | bvhfawcgyoqxbdzrpgsleomtkj 202 | bvhfahcnyoqwudzrfgsleimtkj 203 | gvbfawcnyrqxudzrpgsleimtkj 204 | svhfawcnyoqxudlrpgsleimtkx 205 | avhfafcnyoqxuhzrpgsleimtkj 206 | bvhfawcsyoqxuazrpgsleimtej 207 | bvofawcnyoqxudzrpgsteimtkf 208 | bvhfajcnyoqxudzqpgszeimtkj 209 | bvhfawcsyoqxudzrmgsleiktkj 210 | mvhfawcnyoqxudzrpgkluimtkj 211 | bvhfawcnhoqxudzrpgslwhmtkj 212 | bmhaawsnyoqxudzrpgsleimtkj 213 | bvhfawcnyoqxudzhpgsleimhyj 214 | bvhfxwcnyoqxsdzypgsleimtkj 215 | bvhpawcyyoqxuczrpgsleimtkj 216 | bvomawcnyovxudzrpgsleimtkj 217 | bvhfawcnjvqxudzrpgsleimtkt 218 | nvhfawcnyqqxudzrpgsleittkj 219 | bvhiawcnyzqxudzrpysleimtkj 220 | bvhdawcnyoqxukzrpgsleimtuj 221 | bvhfawcnyyxxudzrpgslzimtkj 222 | hvhfawcnyoqxudzupgslemmtkj 223 | byhfawknyoqxudzrpgsleimtkb 224 | bvhfawcnyoqxudzrpasleihakj 225 | bvafahcnyaqxudzrpgsleimtkj 226 | bkhfawcnyoqxudzrpgllepmtkj 227 | bghfawcnycqxuzzrpgsleimtkj 228 | bvhfawcnyoqxudzrbgeleimtkl 229 | bvhfascnyoqgudzrpgsveimtkj 230 | bvhfawnnyoqxudzrpgsleimtdl 231 | bvhqawcnyoqxudzrpgsleimgrj 232 | bvhsawdwyoqxudzrpgsleimtkj 233 | bvhfawcnyoqxudzrpgaleipttj 234 | bvhfawcnrlqxudzrbgsleimtkj 235 | bvhfdwcnyoqxudzqpcsleimtkj 236 | bvhfawcnyoqxudzopgslexmokj 237 | bvhfawcoyoqxudzrpghlewmtkj 238 | bvhfozcnykqxudzrpgsleimtkj 239 | bvhfawcnyoqxuvzrpgslrimtkr 240 | bvhfrncnyoqrudzrpgsleimtkj 241 | bvhfawcnyocxuizrpgslefmtkj 242 | bvhfawywyoqxudzrpgsleimxkj 243 | bvhfawcnyoqxugzrpgslrimtij 244 | bvtfawcnyoqxudzcpgsleimtfj 245 | bvhfawcnyoqxuzzspgsleimtkz 246 | bvhfawcnzoqxvdzrpgslsimtkj 247 | bvhfzwcnyoqxudzrpgslenmhkj 248 | bvhfkccnyoqxudzrpgzleimtkj 249 | bvhfawcnyoqzudzrpgslhimwkj 250 | bzhfawvnyooxudzrpgsleimtkj 251 | -------------------------------------------------------------------------------- /resources/advent_2018/day_01/input: -------------------------------------------------------------------------------- 1 | -6 2 | -7 3 | +2 4 | -4 5 | -10 6 | +2 7 | -6 8 | -11 9 | -19 10 | -1 11 | -14 12 | +2 13 | -12 14 | +5 15 | -2 16 | +8 17 | -9 18 | +7 19 | -10 20 | -14 21 | +3 22 | -1 23 | +19 24 | +17 25 | -4 26 | +15 27 | -17 28 | +20 29 | +14 30 | +15 31 | -19 32 | +17 33 | -6 34 | +19 35 | +19 36 | +13 37 | -11 38 | -13 39 | +17 40 | +16 41 | +11 42 | -4 43 | -3 44 | +17 45 | +10 46 | -5 47 | -16 48 | -2 49 | -15 50 | +10 51 | +10 52 | -1 53 | +17 54 | -6 55 | +18 56 | -3 57 | -13 58 | -5 59 | +19 60 | +7 61 | +11 62 | +6 63 | -7 64 | -9 65 | +17 66 | +11 67 | +7 68 | +17 69 | +4 70 | +17 71 | -6 72 | -4 73 | -18 74 | +8 75 | -4 76 | +12 77 | +7 78 | +1 79 | -15 80 | -15 81 | -14 82 | -5 83 | -13 84 | -18 85 | +13 86 | -6 87 | +10 88 | -9 89 | -14 90 | -13 91 | -1 92 | -18 93 | +11 94 | +10 95 | +8 96 | +7 97 | +11 98 | +6 99 | +9 100 | +8 101 | +5 102 | -18 103 | -7 104 | -6 105 | -7 106 | +15 107 | +2 108 | -15 109 | +3 110 | -12 111 | -10 112 | -3 113 | -19 114 | +1 115 | +5 116 | -19 117 | -8 118 | +10 119 | -13 120 | +7 121 | +20 122 | -13 123 | -9 124 | +10 125 | +7 126 | +12 127 | -1 128 | +9 129 | +19 130 | -9 131 | -35 132 | -21 133 | +1 134 | -19 135 | -19 136 | -19 137 | +11 138 | +15 139 | -3 140 | +9 141 | +10 142 | +3 143 | -12 144 | -17 145 | -15 146 | +1 147 | +7 148 | +1 149 | +14 150 | -16 151 | +10 152 | +2 153 | -5 154 | -16 155 | +4 156 | -19 157 | -23 158 | +1 159 | +9 160 | +15 161 | +12 162 | +7 163 | -15 164 | -12 165 | -15 166 | -7 167 | +18 168 | -8 169 | +19 170 | +12 171 | +9 172 | -14 173 | +4 174 | +16 175 | +18 176 | -17 177 | -25 178 | -20 179 | -21 180 | +6 181 | +1 182 | +11 183 | -6 184 | -10 185 | -3 186 | -8 187 | -4 188 | -6 189 | -11 190 | -16 191 | +1 192 | +7 193 | -17 194 | +5 195 | +14 196 | -16 197 | -6 198 | +11 199 | +6 200 | -13 201 | +19 202 | +18 203 | -7 204 | -8 205 | -2 206 | +12 207 | -5 208 | +1 209 | +11 210 | +19 211 | -1 212 | -19 213 | -23 214 | +16 215 | -19 216 | +5 217 | -3 218 | -4 219 | -5 220 | -10 221 | -1 222 | -16 223 | -1 224 | -9 225 | +8 226 | +3 227 | -7 228 | +12 229 | +2 230 | +4 231 | -2 232 | +5 233 | -2 234 | -8 235 | +18 236 | +2 237 | +18 238 | +15 239 | +15 240 | -5 241 | +13 242 | -7 243 | -29 244 | +3 245 | +3 246 | +14 247 | +1 248 | -4 249 | +2 250 | -9 251 | +17 252 | -16 253 | +14 254 | +1 255 | +6 256 | +12 257 | +7 258 | +16 259 | -6 260 | -13 261 | +27 262 | +46 263 | +9 264 | +6 265 | -1 266 | -11 267 | +13 268 | +11 269 | -3 270 | -12 271 | +21 272 | +8 273 | -16 274 | +14 275 | -7 276 | -10 277 | -9 278 | -15 279 | -8 280 | +62 281 | -6 282 | +27 283 | +52 284 | +6 285 | +11 286 | +15 287 | -17 288 | +16 289 | +8 290 | +12 291 | +6 292 | -13 293 | +9 294 | -18 295 | -5 296 | -20 297 | -19 298 | -3 299 | +17 300 | +18 301 | -2 302 | -19 303 | -10 304 | +12 305 | -20 306 | +3 307 | +8 308 | +1 309 | +1 310 | +16 311 | +6 312 | +19 313 | -14 314 | +19 315 | +15 316 | -9 317 | +10 318 | +18 319 | +15 320 | -7 321 | -7 322 | -4 323 | -12 324 | +11 325 | -19 326 | +17 327 | -4 328 | +3 329 | +19 330 | +17 331 | +10 332 | +16 333 | -17 334 | +14 335 | +4 336 | +8 337 | -7 338 | +3 339 | +16 340 | +12 341 | +4 342 | +14 343 | +8 344 | -11 345 | +19 346 | -11 347 | -17 348 | +7 349 | -12 350 | +1 351 | +8 352 | +1 353 | +8 354 | -12 355 | -11 356 | -9 357 | +18 358 | -19 359 | -13 360 | -10 361 | -8 362 | +2 363 | +20 364 | +12 365 | -14 366 | +15 367 | -19 368 | +10 369 | -16 370 | -3 371 | -1 372 | +5 373 | -12 374 | -9 375 | +7 376 | -4 377 | +5 378 | -17 379 | -17 380 | +19 381 | -13 382 | +22 383 | -14 384 | +1 385 | +15 386 | -14 387 | -19 388 | -15 389 | -16 390 | +19 391 | +10 392 | -18 393 | -1 394 | +4 395 | -16 396 | +7 397 | +8 398 | -16 399 | -14 400 | -5 401 | -14 402 | -7 403 | -22 404 | +18 405 | +6 406 | +7 407 | +49 408 | +18 409 | +10 410 | +14 411 | -10 412 | +11 413 | -3 414 | -10 415 | -13 416 | +5 417 | +4 418 | +5 419 | -2 420 | +17 421 | +10 422 | -12 423 | +16 424 | +14 425 | +5 426 | +14 427 | +4 428 | -15 429 | +21 430 | +18 431 | -7 432 | +13 433 | +17 434 | -19 435 | +8 436 | +16 437 | +1 438 | -13 439 | -25 440 | -17 441 | +4 442 | -1 443 | +18 444 | +25 445 | -19 446 | -28 447 | -9 448 | -7 449 | +14 450 | -9 451 | +8 452 | -7 453 | +24 454 | +1 455 | +23 456 | -15 457 | +22 458 | +60 459 | -22 460 | +104 461 | +36 462 | -14 463 | +174 464 | +12 465 | +4 466 | +4 467 | +4 468 | -5 469 | -30 470 | -8 471 | -17 472 | +66 473 | +38 474 | -1 475 | +22 476 | -10 477 | -31 478 | -146 479 | +53 480 | -93 481 | +184 482 | -14 483 | +140 484 | -5 485 | -171 486 | +68756 487 | -2 488 | +9 489 | -8 490 | +15 491 | -10 492 | -19 493 | -6 494 | +12 495 | +3 496 | +7 497 | -8 498 | -12 499 | -6 500 | -6 501 | +1 502 | -13 503 | -5 504 | -1 505 | +14 506 | +18 507 | -1 508 | +2 509 | +12 510 | -10 511 | +16 512 | -10 513 | -15 514 | -19 515 | +10 516 | +3 517 | -18 518 | -3 519 | -5 520 | -16 521 | +10 522 | +3 523 | -11 524 | +3 525 | +3 526 | -17 527 | -16 528 | -9 529 | +14 530 | +7 531 | -14 532 | +17 533 | -12 534 | +10 535 | +11 536 | +16 537 | -6 538 | -6 539 | -21 540 | +7 541 | +12 542 | -16 543 | -2 544 | -13 545 | -17 546 | +7 547 | -9 548 | -3 549 | +9 550 | -7 551 | -1 552 | +5 553 | +18 554 | -4 555 | +16 556 | +11 557 | -1 558 | -15 559 | +14 560 | +14 561 | -10 562 | +14 563 | -7 564 | +10 565 | +6 566 | +6 567 | +5 568 | +6 569 | -4 570 | +12 571 | +4 572 | +2 573 | -19 574 | +12 575 | +3 576 | -12 577 | -1 578 | +24 579 | +16 580 | +14 581 | +5 582 | -3 583 | -11 584 | +16 585 | +18 586 | +9 587 | -13 588 | -4 589 | +2 590 | +14 591 | -6 592 | -9 593 | +12 594 | +6 595 | +15 596 | +3 597 | +10 598 | -14 599 | +17 600 | +8 601 | +8 602 | -3 603 | -7 604 | +12 605 | +7 606 | -13 607 | +19 608 | +1 609 | +10 610 | +2 611 | +17 612 | +18 613 | -7 614 | +1 615 | +5 616 | +11 617 | +20 618 | -3 619 | -11 620 | +15 621 | -12 622 | +9 623 | -10 624 | +14 625 | -17 626 | +18 627 | -9 628 | -16 629 | -19 630 | -8 631 | +4 632 | -18 633 | -17 634 | -8 635 | -14 636 | -8 637 | -8 638 | -14 639 | -7 640 | +3 641 | -18 642 | +19 643 | +1 644 | -10 645 | +7 646 | -16 647 | -11 648 | +17 649 | -18 650 | +2 651 | -3 652 | -12 653 | -2 654 | +18 655 | -8 656 | +10 657 | +10 658 | +5 659 | -16 660 | -16 661 | -7 662 | +1 663 | -4 664 | -16 665 | +15 666 | +15 667 | -23 668 | -2 669 | -23 670 | -16 671 | -2 672 | +13 673 | -8 674 | +29 675 | +38 676 | -1 677 | -4 678 | -10 679 | +7 680 | +27 681 | -8 682 | +15 683 | +13 684 | +20 685 | +19 686 | -17 687 | -8 688 | -17 689 | +7 690 | +1 691 | +3 692 | -9 693 | +14 694 | -18 695 | +17 696 | +5 697 | +17 698 | +13 699 | +15 700 | -10 701 | +6 702 | -15 703 | -19 704 | -1 705 | -12 706 | +4 707 | +6 708 | -14 709 | +23 710 | -2 711 | +13 712 | -12 713 | +20 714 | -7 715 | +22 716 | -11 717 | -8 718 | +9 719 | +17 720 | -4 721 | +1 722 | +14 723 | -5 724 | -4 725 | -18 726 | -19 727 | +38 728 | -4 729 | +18 730 | -16 731 | +9 732 | +14 733 | -5 734 | +17 735 | +13 736 | -9 737 | +4 738 | -12 739 | -2 740 | +9 741 | +13 742 | +24 743 | -7 744 | -5 745 | +8 746 | +19 747 | +19 748 | +11 749 | -16 750 | -11 751 | +7 752 | -13 753 | +16 754 | +14 755 | -10 756 | +4 757 | +19 758 | +16 759 | +4 760 | -3 761 | +8 762 | +8 763 | -7 764 | -17 765 | -18 766 | -3 767 | -6 768 | +4 769 | +13 770 | -6 771 | +17 772 | -9 773 | +14 774 | -9 775 | -17 776 | +8 777 | +5 778 | +12 779 | +5 780 | -11 781 | -1 782 | +21 783 | -1 784 | +9 785 | +13 786 | +4 787 | +6 788 | +3 789 | +5 790 | +4 791 | +10 792 | -4 793 | -9 794 | -3 795 | -14 796 | -1 797 | +7 798 | +7 799 | +18 800 | +11 801 | +16 802 | -6 803 | +10 804 | -6 805 | +15 806 | -7 807 | -5 808 | +9 809 | +15 810 | +26 811 | -16 812 | -4 813 | -23 814 | -17 815 | -14 816 | +3 817 | +3 818 | -5 819 | +1 820 | -18 821 | -4 822 | +17 823 | +1 824 | -11 825 | -17 826 | +16 827 | -11 828 | -1 829 | +19 830 | -1 831 | +7 832 | +21 833 | -26 834 | -4 835 | +19 836 | +17 837 | +12 838 | -20 839 | -14 840 | +21 841 | -20 842 | -22 843 | -23 844 | -15 845 | +3 846 | +5 847 | -18 848 | -8 849 | -17 850 | -2 851 | +10 852 | -12 853 | -7 854 | -5 855 | -15 856 | +11 857 | -17 858 | -7 859 | -10 860 | +11 861 | +14 862 | -18 863 | -6 864 | -3 865 | -6 866 | +17 867 | +15 868 | +3 869 | -7 870 | -12 871 | +25 872 | +23 873 | +13 874 | -6 875 | -10 876 | -24 877 | +7 878 | -14 879 | +8 880 | -14 881 | -2 882 | -21 883 | -10 884 | -3 885 | -20 886 | -10 887 | +2 888 | +16 889 | -12 890 | +9 891 | +9 892 | -16 893 | +17 894 | -26 895 | -18 896 | +10 897 | -7 898 | -2 899 | -16 900 | +6 901 | +18 902 | +5 903 | -30 904 | -3 905 | -18 906 | +19 907 | +12 908 | +17 909 | +35 910 | +13 911 | +16 912 | -11 913 | +17 914 | -8 915 | -2 916 | -8 917 | -5 918 | +8 919 | +29 920 | +16 921 | -1 922 | -36 923 | +8 924 | -52 925 | -6 926 | +148 927 | +18 928 | -20 929 | +15 930 | -10 931 | -10 932 | -9 933 | -6 934 | +12 935 | -5 936 | -18 937 | -9 938 | +37 939 | -16 940 | +18 941 | -20 942 | -4 943 | +26 944 | +11 945 | -18 946 | +4 947 | +11 948 | +2 949 | +20 950 | -27 951 | -15 952 | +43 953 | +50 954 | +4 955 | +17 956 | +21 957 | -1 958 | -1 959 | -11 960 | -60 961 | +137 962 | -23 963 | +52 964 | +14 965 | +11 966 | +8 967 | +24 968 | +13 969 | +86 970 | +31 971 | +321 972 | +68009 973 | -18 974 | -17 975 | +14 976 | +17 977 | -12 978 | +11 979 | +10 980 | -8 981 | +19 982 | -2 983 | +6 984 | -1 985 | -15 986 | +17 987 | -19 988 | -19 989 | -5 990 | -18 991 | -12 992 | -8 993 | -20 994 | +3 995 | -10 996 | +6 997 | -15 998 | -5 999 | -2 1000 | -137857 1001 | -------------------------------------------------------------------------------- /resources/advent_2017/day_05/input: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 0 4 | 1 5 | 0 6 | -3 7 | -3 8 | -6 9 | 0 10 | -7 11 | -9 12 | 0 13 | -2 14 | 0 15 | -8 16 | -1 17 | -15 18 | -15 19 | -4 20 | -12 21 | -19 22 | -3 23 | -12 24 | -10 25 | -3 26 | -17 27 | -17 28 | -9 29 | -18 30 | -20 31 | -1 32 | -6 33 | -29 34 | -18 35 | -5 36 | -25 37 | -13 38 | -22 39 | -33 40 | 2 41 | -39 42 | -40 43 | -33 44 | -33 45 | -27 46 | -7 47 | -44 48 | 1 49 | -20 50 | -46 51 | -41 52 | 0 53 | -19 54 | 0 55 | -10 56 | -15 57 | -21 58 | -17 59 | -52 60 | -20 61 | -45 62 | -34 63 | -30 64 | -29 65 | -40 66 | -1 67 | -18 68 | -10 69 | -19 70 | -15 71 | -64 72 | -61 73 | -53 74 | -28 75 | -45 76 | -12 77 | -73 78 | -36 79 | -36 80 | -2 81 | -30 82 | -56 83 | -63 84 | -42 85 | -8 86 | -35 87 | -32 88 | -39 89 | -22 90 | -87 91 | -45 92 | -35 93 | -74 94 | 1 95 | -5 96 | -45 97 | -16 98 | -19 99 | -48 100 | -25 101 | -94 102 | -85 103 | -75 104 | -15 105 | -79 106 | -37 107 | -82 108 | -13 109 | -85 110 | -20 111 | -52 112 | -50 113 | -85 114 | -13 115 | -70 116 | -16 117 | -86 118 | 0 119 | -68 120 | -55 121 | -15 122 | -25 123 | -31 124 | -117 125 | -91 126 | -67 127 | -114 128 | -108 129 | -50 130 | -76 131 | -116 132 | -12 133 | -27 134 | -98 135 | -115 136 | -101 137 | -124 138 | -2 139 | -4 140 | -95 141 | -41 142 | -35 143 | -110 144 | -86 145 | -4 146 | -126 147 | -67 148 | -94 149 | -81 150 | -101 151 | -93 152 | -109 153 | -71 154 | -152 155 | -110 156 | -145 157 | -28 158 | -139 159 | -106 160 | -83 161 | -58 162 | -100 163 | -1 164 | -21 165 | -112 166 | -130 167 | -102 168 | -34 169 | -80 170 | -49 171 | -11 172 | -72 173 | -82 174 | -132 175 | -36 176 | -119 177 | -127 178 | -85 179 | -66 180 | -12 181 | -43 182 | -3 183 | -86 184 | -116 185 | -125 186 | -162 187 | 0 188 | -185 189 | -39 190 | -27 191 | -159 192 | -23 193 | -71 194 | -50 195 | -119 196 | -183 197 | -56 198 | -48 199 | -113 200 | -197 201 | -199 202 | -6 203 | -92 204 | -7 205 | -39 206 | -63 207 | -67 208 | -22 209 | -126 210 | -170 211 | -67 212 | -59 213 | -114 214 | -207 215 | -13 216 | -15 217 | -168 218 | -167 219 | -15 220 | -143 221 | -128 222 | -136 223 | -115 224 | 2 225 | -113 226 | -74 227 | -104 228 | -91 229 | -157 230 | -121 231 | -126 232 | -125 233 | -112 234 | -106 235 | -194 236 | -146 237 | -165 238 | -139 239 | -97 240 | -134 241 | -133 242 | -165 243 | -237 244 | -69 245 | -10 246 | -232 247 | -100 248 | -168 249 | -53 250 | -83 251 | -149 252 | -42 253 | -71 254 | -119 255 | -185 256 | -110 257 | -92 258 | -256 259 | -19 260 | -249 261 | -147 262 | -68 263 | -205 264 | -52 265 | -212 266 | -5 267 | -167 268 | -63 269 | -264 270 | -176 271 | -180 272 | -223 273 | -15 274 | -158 275 | -2 276 | -134 277 | -268 278 | -92 279 | -193 280 | -145 281 | -141 282 | -218 283 | -99 284 | -85 285 | -213 286 | -24 287 | -82 288 | -201 289 | -109 290 | 0 291 | -152 292 | -14 293 | -168 294 | -103 295 | -232 296 | -7 297 | -115 298 | -141 299 | -273 300 | -117 301 | -201 302 | -165 303 | -265 304 | -81 305 | -64 306 | -243 307 | -123 308 | 0 309 | -24 310 | -140 311 | -235 312 | -194 313 | -11 314 | -129 315 | -128 316 | -211 317 | -59 318 | -97 319 | -40 320 | -76 321 | -104 322 | -38 323 | -312 324 | -225 325 | -93 326 | -113 327 | -108 328 | -109 329 | -22 330 | -128 331 | -250 332 | -222 333 | -262 334 | -214 335 | -34 336 | -87 337 | -176 338 | -166 339 | -33 340 | -226 341 | -198 342 | -238 343 | -159 344 | -295 345 | -245 346 | -227 347 | -211 348 | -59 349 | -237 350 | -74 351 | -92 352 | -221 353 | -118 354 | -77 355 | -160 356 | -110 357 | -260 358 | -259 359 | -25 360 | -117 361 | -120 362 | -304 363 | -273 364 | -89 365 | -354 366 | -85 367 | -339 368 | -366 369 | -46 370 | -91 371 | -280 372 | -68 373 | -62 374 | -118 375 | -178 376 | -249 377 | -281 378 | -273 379 | -360 380 | -356 381 | -150 382 | -367 383 | -47 384 | -289 385 | -51 386 | -233 387 | -158 388 | -226 389 | -372 390 | -212 391 | -139 392 | -119 393 | -238 394 | -244 395 | -39 396 | -263 397 | -239 398 | -374 399 | -257 400 | -146 401 | -347 402 | -209 403 | -350 404 | 2 405 | -403 406 | -149 407 | -381 408 | -55 409 | -114 410 | -294 411 | -106 412 | -118 413 | -222 414 | -24 415 | -259 416 | -301 417 | -357 418 | -13 419 | -137 420 | -281 421 | -88 422 | -7 423 | -276 424 | 2 425 | -7 426 | -232 427 | -337 428 | -172 429 | -181 430 | -129 431 | -51 432 | -147 433 | -310 434 | -253 435 | -396 436 | -111 437 | -386 438 | -106 439 | -240 440 | -432 441 | -94 442 | -239 443 | -334 444 | -135 445 | -196 446 | -329 447 | -228 448 | -10 449 | -438 450 | -419 451 | -86 452 | -167 453 | -56 454 | -200 455 | -69 456 | -229 457 | -90 458 | -147 459 | -160 460 | -345 461 | -7 462 | -96 463 | -251 464 | -113 465 | -53 466 | -186 467 | -426 468 | -244 469 | -185 470 | -178 471 | -267 472 | -378 473 | -368 474 | -53 475 | -424 476 | -178 477 | -179 478 | -353 479 | -242 480 | -182 481 | -423 482 | -139 483 | -49 484 | -335 485 | -225 486 | -3 487 | -13 488 | -159 489 | -245 490 | -244 491 | -359 492 | -223 493 | -380 494 | -264 495 | -383 496 | -285 497 | -322 498 | -471 499 | -7 500 | -295 501 | -84 502 | -291 503 | -92 504 | -129 505 | -175 506 | -205 507 | -49 508 | -164 509 | -262 510 | -105 511 | -364 512 | -438 513 | -283 514 | -415 515 | -323 516 | -167 517 | -501 518 | -22 519 | -428 520 | -10 521 | -156 522 | -517 523 | -385 524 | -356 525 | -396 526 | -295 527 | -372 528 | -409 529 | -311 530 | -261 531 | -262 532 | -4 533 | -41 534 | -264 535 | -436 536 | -316 537 | -22 538 | -449 539 | -444 540 | -306 541 | -324 542 | -16 543 | -431 544 | -379 545 | -476 546 | -369 547 | -198 548 | -312 549 | -393 550 | -47 551 | -277 552 | -523 553 | -402 554 | -368 555 | -312 556 | -418 557 | -21 558 | -372 559 | -86 560 | -286 561 | -475 562 | -183 563 | -405 564 | -427 565 | -404 566 | -405 567 | -446 568 | -549 569 | -296 570 | -249 571 | -243 572 | -472 573 | -450 574 | -126 575 | -260 576 | -227 577 | -25 578 | -348 579 | -122 580 | -80 581 | -330 582 | -222 583 | -389 584 | -360 585 | -250 586 | -310 587 | -544 588 | -113 589 | -556 590 | -445 591 | -457 592 | -533 593 | -447 594 | -251 595 | -373 596 | -343 597 | -391 598 | -12 599 | -567 600 | -128 601 | -332 602 | -245 603 | -252 604 | -517 605 | -101 606 | -480 607 | -401 608 | -290 609 | -394 610 | -321 611 | -533 612 | -257 613 | -102 614 | -152 615 | -251 616 | -102 617 | -507 618 | -597 619 | -175 620 | -345 621 | -442 622 | -600 623 | -306 624 | -149 625 | -151 626 | -355 627 | -71 628 | -315 629 | -35 630 | -161 631 | -404 632 | -253 633 | -526 634 | -275 635 | -339 636 | -483 637 | -315 638 | -423 639 | -116 640 | -345 641 | -507 642 | -332 643 | -27 644 | -395 645 | -634 646 | -548 647 | -205 648 | -276 649 | -213 650 | -356 651 | -413 652 | -353 653 | -89 654 | -88 655 | -649 656 | -465 657 | -580 658 | -286 659 | -607 660 | -21 661 | -35 662 | -227 663 | -415 664 | -501 665 | -343 666 | -245 667 | -94 668 | -200 669 | -376 670 | -43 671 | -585 672 | -668 673 | -623 674 | -264 675 | -574 676 | -223 677 | -628 678 | -556 679 | -100 680 | -53 681 | -88 682 | -644 683 | -285 684 | -631 685 | -418 686 | -369 687 | -477 688 | -379 689 | -199 690 | -68 691 | -323 692 | -337 693 | -318 694 | -651 695 | -255 696 | -323 697 | -38 698 | -502 699 | -356 700 | -550 701 | -555 702 | -679 703 | -170 704 | -38 705 | -516 706 | -367 707 | -687 708 | -52 709 | -23 710 | -225 711 | -451 712 | -323 713 | -637 714 | -264 715 | 0 716 | -535 717 | -67 718 | -254 719 | -580 720 | -173 721 | -301 722 | -374 723 | -120 724 | -8 725 | -197 726 | -154 727 | -173 728 | -597 729 | -525 730 | -341 731 | -278 732 | -721 733 | -360 734 | -728 735 | -607 736 | -346 737 | -491 738 | -247 739 | 2 740 | -121 741 | -505 742 | -694 743 | -706 744 | -297 745 | -4 746 | -110 747 | -187 748 | -259 749 | -414 750 | -323 751 | -637 752 | -96 753 | -157 754 | -331 755 | -521 756 | -590 757 | -390 758 | -220 759 | -100 760 | -156 761 | -302 762 | -545 763 | -322 764 | -450 765 | -236 766 | -287 767 | -605 768 | -346 769 | -467 770 | -25 771 | -382 772 | -430 773 | -682 774 | 2 775 | -261 776 | -605 777 | -635 778 | -633 779 | -553 780 | -491 781 | -226 782 | -622 783 | -191 784 | -48 785 | -92 786 | -218 787 | -548 788 | -651 789 | -672 790 | -631 791 | -764 792 | -367 793 | -108 794 | -507 795 | -790 796 | -573 797 | -282 798 | -334 799 | -280 800 | -285 801 | -105 802 | -797 803 | -228 804 | -85 805 | -102 806 | -623 807 | -304 808 | -52 809 | -278 810 | -243 811 | -681 812 | -133 813 | -606 814 | -345 815 | -354 816 | -402 817 | -6 818 | -353 819 | -447 820 | -69 821 | -432 822 | -54 823 | -486 824 | -78 825 | -774 826 | -241 827 | -625 828 | -806 829 | -425 830 | -790 831 | -381 832 | -507 833 | -755 834 | -304 835 | -362 836 | -606 837 | -256 838 | -25 839 | -341 840 | -451 841 | -12 842 | -606 843 | -738 844 | -484 845 | -167 846 | -663 847 | 1 848 | -481 849 | -788 850 | -469 851 | -388 852 | -59 853 | -105 854 | -402 855 | -523 856 | -717 857 | -234 858 | -611 859 | -543 860 | -435 861 | -383 862 | -267 863 | -217 864 | -275 865 | -610 866 | -335 867 | -411 868 | -842 869 | -131 870 | -460 871 | -527 872 | -511 873 | -761 874 | -160 875 | -660 876 | -605 877 | -817 878 | -546 879 | -286 880 | -604 881 | -204 882 | -223 883 | -558 884 | -652 885 | -542 886 | -350 887 | -527 888 | -59 889 | -782 890 | -764 891 | -529 892 | -608 893 | -688 894 | -301 895 | -715 896 | -148 897 | -492 898 | -796 899 | -285 900 | -491 901 | -702 902 | -767 903 | -191 904 | -572 905 | -712 906 | -207 907 | -589 908 | -39 909 | -278 910 | -485 911 | -273 912 | -51 913 | -560 914 | -718 915 | -790 916 | 0 917 | -194 918 | -319 919 | -171 920 | -552 921 | -247 922 | -810 923 | -737 924 | -677 925 | -853 926 | -806 927 | -565 928 | -923 929 | -427 930 | -442 931 | -375 932 | -215 933 | -706 934 | -139 935 | -396 936 | -126 937 | -170 938 | -281 939 | -544 940 | -101 941 | -271 942 | -728 943 | -485 944 | -677 945 | -442 946 | -137 947 | -78 948 | -414 949 | -546 950 | -669 951 | -609 952 | -284 953 | -488 954 | -181 955 | -534 956 | -946 957 | -191 958 | -255 959 | -413 960 | -614 961 | -329 962 | -932 963 | -528 964 | -689 965 | -246 966 | -272 967 | -395 968 | -211 969 | -702 970 | -786 971 | -595 972 | -835 973 | -870 974 | -822 975 | -507 976 | -533 977 | -147 978 | -141 979 | -385 980 | -623 981 | -745 982 | -575 983 | -225 984 | -79 985 | -736 986 | -887 987 | -649 988 | -133 989 | -500 990 | -422 991 | -810 992 | -491 993 | -480 994 | -462 995 | -16 996 | -848 997 | -740 998 | -809 999 | -9 1000 | -399 1001 | -535 1002 | -274 1003 | -165 1004 | -119 1005 | -77 1006 | -340 1007 | -597 1008 | -755 1009 | -611 1010 | -929 1011 | -50 1012 | -745 1013 | -530 1014 | -392 1015 | -77 1016 | -760 1017 | -961 1018 | -28 1019 | -507 1020 | -21 1021 | -253 1022 | -846 1023 | -996 1024 | -308 1025 | -175 1026 | -684 1027 | -315 1028 | -859 1029 | -757 1030 | -418 1031 | -591 1032 | -946 1033 | -393 1034 | -25 1035 | -917 1036 | -208 1037 | -572 1038 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 4 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 5 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial code and documentation 12 | distributed under this Agreement, and 13 | b) in the case of each subsequent Contributor: 14 | i) changes to the Program, and 15 | ii) additions to the Program; 16 | 17 | where such changes and/or additions to the Program originate from and are 18 | distributed by that particular Contributor. A Contribution 'originates' 19 | from a Contributor if it was added to the Program by such Contributor 20 | itself or anyone acting on such Contributor's behalf. Contributions do not 21 | include additions to the Program which: (i) are separate modules of 22 | software distributed in conjunction with the Program under their own 23 | license agreement, and (ii) are not derivative works of the Program. 24 | 25 | "Contributor" means any person or entity that distributes the Program. 26 | 27 | "Licensed Patents" mean patent claims licensable by a Contributor which are 28 | necessarily infringed by the use or sale of its Contribution alone or when 29 | combined with the Program. 30 | 31 | "Program" means the Contributions distributed in accordance with this 32 | Agreement. 33 | 34 | "Recipient" means anyone who receives the Program under this Agreement, 35 | including all Contributors. 36 | 37 | 2. GRANT OF RIGHTS 38 | a) Subject to the terms of this Agreement, each Contributor hereby grants 39 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 40 | reproduce, prepare derivative works of, publicly display, publicly 41 | perform, distribute and sublicense the Contribution of such Contributor, 42 | if any, and such derivative works, in source code and object code form. 43 | b) Subject to the terms of this Agreement, each Contributor hereby grants 44 | Recipient a non-exclusive, worldwide, royalty-free patent license under 45 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 46 | transfer the Contribution of such Contributor, if any, in source code and 47 | object code form. This patent license shall apply to the combination of 48 | the Contribution and the Program if, at the time the Contribution is 49 | added by the Contributor, such addition of the Contribution causes such 50 | combination to be covered by the Licensed Patents. The patent license 51 | shall not apply to any other combinations which include the Contribution. 52 | No hardware per se is licensed hereunder. 53 | c) Recipient understands that although each Contributor grants the licenses 54 | to its Contributions set forth herein, no assurances are provided by any 55 | Contributor that the Program does not infringe the patent or other 56 | intellectual property rights of any other entity. Each Contributor 57 | disclaims any liability to Recipient for claims brought by any other 58 | entity based on infringement of intellectual property rights or 59 | otherwise. As a condition to exercising the rights and licenses granted 60 | hereunder, each Recipient hereby assumes sole responsibility to secure 61 | any other intellectual property rights needed, if any. For example, if a 62 | third party patent license is required to allow Recipient to distribute 63 | the Program, it is Recipient's responsibility to acquire that license 64 | before distributing the Program. 65 | d) Each Contributor represents that to its knowledge it has sufficient 66 | copyright rights in its Contribution, if any, to grant the copyright 67 | license set forth in this Agreement. 68 | 69 | 3. REQUIREMENTS 70 | 71 | A Contributor may choose to distribute the Program in object code form under 72 | its own license agreement, provided that: 73 | 74 | a) it complies with the terms and conditions of this Agreement; and 75 | b) its license agreement: 76 | i) effectively disclaims on behalf of all Contributors all warranties 77 | and conditions, express and implied, including warranties or 78 | conditions of title and non-infringement, and implied warranties or 79 | conditions of merchantability and fitness for a particular purpose; 80 | ii) effectively excludes on behalf of all Contributors all liability for 81 | damages, including direct, indirect, special, incidental and 82 | consequential damages, such as lost profits; 83 | iii) states that any provisions which differ from this Agreement are 84 | offered by that Contributor alone and not by any other party; and 85 | iv) states that source code for the Program is available from such 86 | Contributor, and informs licensees how to obtain it in a reasonable 87 | manner on or through a medium customarily used for software exchange. 88 | 89 | When the Program is made available in source code form: 90 | 91 | a) it must be made available under this Agreement; and 92 | b) a copy of this Agreement must be included with each copy of the Program. 93 | Contributors may not remove or alter any copyright notices contained 94 | within the Program. 95 | 96 | Each Contributor must identify itself as the originator of its Contribution, 97 | if 98 | any, in a manner that reasonably allows subsequent Recipients to identify the 99 | originator of the Contribution. 100 | 101 | 4. COMMERCIAL DISTRIBUTION 102 | 103 | Commercial distributors of software may accept certain responsibilities with 104 | respect to end users, business partners and the like. While this license is 105 | intended to facilitate the commercial use of the Program, the Contributor who 106 | includes the Program in a commercial product offering should do so in a manner 107 | which does not create potential liability for other Contributors. Therefore, 108 | if a Contributor includes the Program in a commercial product offering, such 109 | Contributor ("Commercial Contributor") hereby agrees to defend and indemnify 110 | every other Contributor ("Indemnified Contributor") against any losses, 111 | damages and costs (collectively "Losses") arising from claims, lawsuits and 112 | other legal actions brought by a third party against the Indemnified 113 | Contributor to the extent caused by the acts or omissions of such Commercial 114 | Contributor in connection with its distribution of the Program in a commercial 115 | product offering. The obligations in this section do not apply to any claims 116 | or Losses relating to any actual or alleged intellectual property 117 | infringement. In order to qualify, an Indemnified Contributor must: 118 | a) promptly notify the Commercial Contributor in writing of such claim, and 119 | b) allow the Commercial Contributor to control, and cooperate with the 120 | Commercial Contributor in, the defense and any related settlement 121 | negotiations. The Indemnified Contributor may participate in any such claim at 122 | its own expense. 123 | 124 | For example, a Contributor might include the Program in a commercial product 125 | offering, Product X. That Contributor is then a Commercial Contributor. If 126 | that Commercial Contributor then makes performance claims, or offers 127 | warranties related to Product X, those performance claims and warranties are 128 | such Commercial Contributor's responsibility alone. Under this section, the 129 | Commercial Contributor would have to defend claims against the other 130 | Contributors related to those performance claims and warranties, and if a 131 | court requires any other Contributor to pay any damages as a result, the 132 | Commercial Contributor must pay those damages. 133 | 134 | 5. NO WARRANTY 135 | 136 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN 137 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR 138 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each 140 | Recipient is solely responsible for determining the appropriateness of using 141 | and distributing the Program and assumes all risks associated with its 142 | exercise of rights under this Agreement , including but not limited to the 143 | risks and costs of program errors, compliance with applicable laws, damage to 144 | or loss of data, programs or equipment, and unavailability or interruption of 145 | operations. 146 | 147 | 6. DISCLAIMER OF LIABILITY 148 | 149 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 150 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 151 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 152 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 153 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 154 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 155 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 156 | OF SUCH DAMAGES. 157 | 158 | 7. GENERAL 159 | 160 | If any provision of this Agreement is invalid or unenforceable under 161 | applicable law, it shall not affect the validity or enforceability of the 162 | remainder of the terms of this Agreement, and without further action by the 163 | parties hereto, such provision shall be reformed to the minimum extent 164 | necessary to make such provision valid and enforceable. 165 | 166 | If Recipient institutes patent litigation against any entity (including a 167 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 168 | (excluding combinations of the Program with other software or hardware) 169 | infringes such Recipient's patent(s), then such Recipient's rights granted 170 | under Section 2(b) shall terminate as of the date such litigation is filed. 171 | 172 | All Recipient's rights under this Agreement shall terminate if it fails to 173 | comply with any of the material terms or conditions of this Agreement and does 174 | not cure such failure in a reasonable period of time after becoming aware of 175 | such noncompliance. If all Recipient's rights under this Agreement terminate, 176 | Recipient agrees to cease use and distribution of the Program as soon as 177 | reasonably practicable. However, Recipient's obligations under this Agreement 178 | and any licenses granted by Recipient relating to the Program shall continue 179 | and survive. 180 | 181 | Everyone is permitted to copy and distribute copies of this Agreement, but in 182 | order to avoid inconsistency the Agreement is copyrighted and may only be 183 | modified in the following manner. The Agreement Steward reserves the right to 184 | publish new versions (including revisions) of this Agreement from time to 185 | time. No one other than the Agreement Steward has the right to modify this 186 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 187 | Eclipse Foundation may assign the responsibility to serve as the Agreement 188 | Steward to a suitable separate entity. Each new version of the Agreement will 189 | be given a distinguishing version number. The Program (including 190 | Contributions) may always be distributed subject to the version of the 191 | Agreement under which it was received. In addition, after a new version of the 192 | Agreement is published, Contributor may elect to distribute the Program 193 | (including its Contributions) under the new version. Except as expressly 194 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 195 | licenses to the intellectual property of any Contributor under this Agreement, 196 | whether expressly, by implication, estoppel or otherwise. All rights in the 197 | Program not expressly granted under this Agreement are reserved. 198 | 199 | This Agreement is governed by the laws of the State of New York and the 200 | intellectual property laws of the United States of America. No party to this 201 | Agreement will bring a legal action under this Agreement more than one year 202 | after the cause of action arose. Each party waives its rights to a jury trial in 203 | any resulting litigation. 204 | --------------------------------------------------------------------------------