day 1 puzzle 1
This commit is contained in:
		
							
								
								
									
										21
									
								
								src/day1/puzzle1.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/day1/puzzle1.typ
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| #import "/src/utils.typ": * | ||||
|  | ||||
| #let solve(input) = { | ||||
|   let lines = input.split("\n") | ||||
|   let (l1, l2) = ((), ()) | ||||
|   let reg = regex("^(\d+)\s+(\d+)$") | ||||
|   for line in lines { | ||||
|     let digits = line.match(reg) | ||||
|     l1.push(int(digits.captures.first())) | ||||
|     l2.push(int(digits.captures.last())) | ||||
|   } | ||||
|  | ||||
|   let total = l1.sorted().zip(l2.sorted()).map(((a, b)) => calc.abs(a - b)).sum() | ||||
|   return total | ||||
| } | ||||
|  | ||||
| #show-puzzle( | ||||
|   1, | ||||
|   solve, | ||||
|   example: 11 | ||||
| ) | ||||
							
								
								
									
										0
									
								
								src/day1/puzzle2.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/day1/puzzle2.typ
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/main.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										108
									
								
								src/main.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								src/main.typ
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,108 @@ | ||||
| #import "@preview/cetz:0.3.1": canvas, draw | ||||
| #import "/src/utils.typ": * | ||||
|  | ||||
| #set document( | ||||
|   title: "Advent of Code 2024", | ||||
|   author: "Lord Baryhobal", | ||||
|   date: datetime.today() | ||||
| ) | ||||
| #show: template | ||||
|  | ||||
| #align(center, text(size: 2em)[*Advent of Code*]) | ||||
| #align(center, text(size: 1.5em)[*--- 2024 ---*]) | ||||
|  | ||||
| #v(1cm) | ||||
|  | ||||
| #align(center, text(size: 1.2em)[_by Lord Baryhobal_]) | ||||
|  | ||||
| #v(2cm) | ||||
|  | ||||
| /* | ||||
| #align(center, canvas({ | ||||
|   draw.merge-path( | ||||
|     { | ||||
|       draw.line((-0.5, 0), (0.5, 0), (0.5, 1)) | ||||
|       draw.arc-through((), (1.3, 0.9), (2, 1.2)) | ||||
|       draw.arc-through((), (1.3, 1.4), (0.5, 2)) | ||||
|       draw.arc-through((), (1.35, 1.8), (1.9, 1.9)) | ||||
|       draw.arc-through((), (1.3, 2.1), (0.4, 3)) | ||||
|       draw.arc-through((), (0.9, 2.7), (1.5, 2.8)) | ||||
|       draw.arc-through((), (0.5, 3.5), (0, 4.5)) | ||||
|        | ||||
|       draw.arc-through((), (-0.5, 3.5), (-1.5, 2.8)) | ||||
|       draw.arc-through((), (-0.9, 2.7), (-0.4, 3)) | ||||
|       draw.arc-through((), (-1.3, 2.1), (-1.9, 1.9)) | ||||
|       draw.arc-through((), (-1.35, 1.8), (-0.5, 2)) | ||||
|       draw.arc-through((), (-1.3, 1.4), (-2, 1.2)) | ||||
|       draw.arc-through((), (-1.3, 0.9), (-0.5, 1)) | ||||
|     }, | ||||
|     close: true, | ||||
|     fill: gradient.linear( | ||||
|       angle: 90deg, | ||||
|       rgb("#35AA48"), | ||||
|       rgb("#2C883A") | ||||
|     ), | ||||
|     stroke: none | ||||
|   ) | ||||
|   draw.rect( | ||||
|     (-0.5, 0), | ||||
|     (0.5, 0.9), | ||||
|     fill: rgb("#63584B"), | ||||
|     stroke: none | ||||
|   ) | ||||
| }))*/ | ||||
|  | ||||
|  | ||||
| #v(1fr) | ||||
|  | ||||
| #context { | ||||
|   let stars = star-state.final() | ||||
|   let star-cnt = stars.values().sum(default: 0) | ||||
|   let first-weekday = datetime( | ||||
|     year: 2024, | ||||
|     month: 12, | ||||
|     day: 1 | ||||
|   ).weekday() | ||||
|   let cells = ([],) * (first-weekday - 1) | ||||
|  | ||||
|   for i in range(1, 26) { | ||||
|     let cell = [#i] | ||||
|     if str(i) in stars.keys() { | ||||
|       cell = stack( | ||||
|         dir: ttb, | ||||
|         spacing: 0.2em, | ||||
|         cell, | ||||
|         h(3pt) + ((emoji.star,)* stars.at(str(i))).join() | ||||
|       ) | ||||
|       cell = link(label("day-" + str(i)), cell) | ||||
|     } | ||||
|  | ||||
|     cells.push(cell) | ||||
|   } | ||||
|  | ||||
|   [*Stars: #star-cnt / 50*] | ||||
|   table( | ||||
|     columns: (1fr,)*7, | ||||
|     inset: 0.8em, | ||||
|     align: center + horizon, | ||||
|     fill: (_, y) => if y > 0 and calc.rem(y, 2) == 0 {gray.lighten(70%)}, | ||||
|     table.header([*Mon*], [*Tue*], [*Wed*], [*Thu*], [*Fri*], [*Sat*], [*Sun*]), | ||||
|     ..cells | ||||
|   ) | ||||
| } | ||||
|  | ||||
| #pagebreak() | ||||
|  | ||||
| #box( | ||||
|   inset: 1em, | ||||
|   stroke: black, | ||||
|   width: 100%, | ||||
|   columns( | ||||
|     2, | ||||
|     outline( | ||||
|       indent: 1em | ||||
|     ) | ||||
|   ) | ||||
| ) | ||||
|  | ||||
| #make-day(1, stars: 1) | ||||
							
								
								
									
										88
									
								
								src/utils.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								src/utils.typ
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,88 @@ | ||||
| #let star-cnt = counter("stars") | ||||
| #let star-state = state("stars", (:)) | ||||
|  | ||||
| #let get-input-path(day) = { | ||||
|   return "/res/inputs/day" + str(day) + ".txt" | ||||
| } | ||||
|  | ||||
| #let get-example-path(day, suffix: none) = { | ||||
|   let suffix = if suffix != none {"_" + str(suffix)} else {""} | ||||
|   return "/res/examples/day" + str(day) + str(suffix) + ".txt" | ||||
| } | ||||
|  | ||||
| #let get-input(day) = { | ||||
|   return read(get-input-path(day)) | ||||
| } | ||||
|  | ||||
| #let check-example(day, func, target-result, suffix: none) = { | ||||
|   let result = (func)(read(get-example-path(day, suffix: suffix))) | ||||
|   /*assert( | ||||
|     result == target-result, | ||||
|     message: "Expected '" + repr(target-result) + "' got '" + repr(result) + "'" | ||||
|   )*/ | ||||
|   let passes = (result == target-result) | ||||
|   let name = if suffix == none [Example] else [Example '#suffix'] | ||||
|   box( | ||||
|     inset: (x: 1.2em, y: 0.6em), | ||||
|     radius: 1.2em, | ||||
|     fill: if passes {green.lighten(20%)} else {red.lighten(20%)}, | ||||
|     if passes [#name passes] else [#name fails] | ||||
|   ) | ||||
|   h(0.6em) | ||||
| } | ||||
|  | ||||
| #let show-result(result) = { | ||||
|   box( | ||||
|     inset: (x: 1.2em, y: 0.6em), | ||||
|     radius: 1.2em, | ||||
|     fill: blue.lighten(20%), | ||||
|     text(fill: white)[Result: #result] | ||||
|   ) | ||||
| } | ||||
|  | ||||
| #let show-puzzle(puzzle, func, example: none) = { | ||||
|   if example != none { | ||||
|     if type(example) == dictionary { | ||||
|       for (suffix, result) in example.pairs() { | ||||
|         check-example(puzzle, func, result, suffix: suffix) | ||||
|       } | ||||
|     } else { | ||||
|       check-example(puzzle, func, example) | ||||
|     } | ||||
|     linebreak() | ||||
|   } | ||||
|  | ||||
|   let input = get-input(1) | ||||
|   let result = (func)(input) | ||||
|   show-result(result) | ||||
| } | ||||
|  | ||||
| #let day-template(day, puzzle1, puzzle2, stars: 0) = { | ||||
|   pagebreak(weak: true) | ||||
|   let title = [Day #day] + ((emoji.star,)*stars).join() | ||||
|   [ | ||||
|     = #title | ||||
|     #label("day-" + str(day)) | ||||
|   ] // Newline required to avoid attaching the label to the text | ||||
|   heading(level: 2)[Puzzle 1] | ||||
|   puzzle1 | ||||
|  | ||||
|   heading(level: 2)[Puzzle 2] | ||||
|   puzzle2 | ||||
| } | ||||
|  | ||||
| #let make-day(day, stars: 0) = { | ||||
|   star-state.update(s => s + (str(day): stars)) | ||||
|   star-cnt.update(v => v + stars) | ||||
|   day-template( | ||||
|     stars: stars, | ||||
|     day, | ||||
|     include("/src/day" + str(day) + "/puzzle1.typ"), | ||||
|     include("/src/day" + str(day) + "/puzzle2.typ"), | ||||
|   ) | ||||
| } | ||||
|  | ||||
| #let template(body) = { | ||||
|   set text(font: "Source Sans 3") | ||||
|   body | ||||
| } | ||||
		Reference in New Issue
	
	Block a user